仓库(nexus)
A-->core
B-->service
C-->action
本地仓库、私有仓库、中央仓库
私服 nexus sonatype
nexus command:start stop restart install uninstall
默认端口号 8081
localhost:8081/nexus
默认用户名和密码 admin/admin123
安装nexus的前提是,服务器上需要安装maven,否则,就算安装了nexus,也无法访问
mvn:deploy 打包到仓库
配置私有仓库:
nexusProfile
nexus
nexus repository
http://192.168.190.128:8081/repository/maven-public/
true
true
...
nexusProfile
当本地仓库无法提供服务时,仍然会去中央仓库下载;
如果不想让去中央仓库下载,可以配置镜像
central
*
Maven Repository Switchboard
http://192.168.190.128:8081/repository/maven-public/
索引文件
发布项目到nexus中
pom文件
user-release
user release resp
http://192.168.190.128:8081/repository/maven-releases/
user-snapshots
user snapshots resp
http://192.168.190.128:8081/repository/maven-snapshots/
settings.xml
user-release
deployment
deployment123
user-snapshots
deployment
deployment123
nexus管理
创建仓库、创建角色、创建用户
生命周期和插件
三套生命周期
- clean
- pre-clean 执行一些需要在clean之前完成的工作
- clean 移出所有上一次构建生成的文件
- post-clean 执行一些需要在clean之后立刻完成的工作
- compile
- validate
- generate-sources
- process-sources
- generate-resources
- process-resources 复制并处理资源文件,至目标文件,准备打包
- compile 编译项目的源代码
- process-classes
- generate-test-sources
- process-test-sources
- generate-test-resources
- process-test-resources 复制并处理资源文件,至目标测试目录
- test-compile 编译测试源代码
- process-test-classes
- test 使用合适的单元测试框架运行测试。这些测试代码不会被打包部署
- prepare-package
- package 打包成jar或者war或者其他格式的分发包
- pre-integration-test
- integration-test
- post-integration-test
- verify
- install 将打好的包安装到本地仓库,供其他项目使用
- deploy 将打好的包安装到远程仓库,供其他项目使用
- site 生成站点
- pre-site
- site 生成项目的站点文件
- post-site
- site-deploy 发布生成的站点文档
compile 插件源码
如果希望将源文件打包的话,需要使用source插件
org.apache.maven.plugins
maven-source-plugin
3.0.1
compile
jar
test-jar
/absolute/path/to/the/output/directory
filename-of-generated-jar-file
false
插件的基础
指定编译的jdk版本
org.apache.maven.plugins
maven-compiler-plugin
3.8.0
true
true
${JAVA_1_4_HOME}/bin/javac
1.8
plugin 也可以继承
org.apache.maven.plugins
maven-compiler-plugin
3.8.0
1.8
true
插件的应用
rar插件
org.apache.maven.plugins
maven-rar-plugin
2.4
package
rar
true
sql插件
org.codehaus.mojo
sql-maven-plugin
3.0.0-SNAPSHOT
mysql
mysql-connector-java
5.1.24
create-db
compile
execute
create database IF NOT EXISTS itat_maven_test
init-table
test-compile
execute
src/main/resources/init.sql
插件也有groupId、artifactId,多了目标 生命周期
测试
测试相关的类和资源放在test包下
执行mvn test时,默认只会执行如下三类测试用例
- Test**
- **Test
- **TestCase
如果不是以上三种命名规则,可以通过surefire插件来配置需要执行的测试类
org.apache.maven.plugins
maven-surefire-plugin
2.22.0
**/Hello*
true
在没有网络的情况下,可以通过help插件来查找帮助信息
example
mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-compiler-plugin -Dversion=3.8.0
[root@eth-01 ~]# mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-compiler-plugin -Dversion=3.8.0
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom (5 KB at 1.2 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/18/maven-plugins-18.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/18/maven-plugins-18.pom (13 KB at 19.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom (23 KB at 43.0 KB/sec)
...
[INFO] org.apache.maven.plugins:maven-compiler-plugin:3.8.0
Name: Apache Maven Compiler Plugin
Description: The Compiler Plugin is used to compile the sources of your
project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-compiler-plugin
Version: 3.8.0
Goal Prefix: compiler
This plugin has 3 goals:
compiler:compile
Description: Compiles application sources
compiler:help
Description: Display help information on maven-compiler-plugin.
Call mvn compiler:help -Ddetail=true -Dgoal= to display
parameter details.
compiler:testCompile
Description: Compiles application test sources.
For more information, run 'mvn help:describe [...] -Ddetail'
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2:32.142s
[INFO] Finished at: Mon Sep 17 20:07:25 CST 2018
[INFO] Final Memory: 12M/30M
[INFO] ------------------------------------------------------------------------
手动跳过测试
mvn clean package -DskipTests=true
指定测试类
mvn clean package -Dtest=Hello.java
生成测试覆盖率
cobertura
命令方式:mvn cobertura
在target/site下会有cobertura
插件方式
org.codehaus.mojo
cobertura-maven-plugin
html
xml
cobertura-report
cobertura
test
发布web项目
手动方式
命令:mvn clean package
会在target下生成XX.war,然后将xx.war复制到tomcat/webapp下,启动tomcat打包后自动copy
maven copy plugin
com.github.goldin
copy-maven-plugin
0.2.5
copy-war
package
copy
${catalina.home}/webapp
${project.build.directory}
user-web.war
- 自动扫描变化,并打包
jetty插件
org.eclipse.jetty
jetty-maven-plugin
10
/hello
8080
60000