maven生命周期:
maven的生命周期分为三个部分:
1.clean
pre-clean:执行需要在clean之前完成的工作,即准备clean阶段;
clean:移除所有上次构建生成的文件;
post-clean:执行在clean之后立即需要完成的工作。
2.compile
validate:顾名思义,校验;
generate-sources:生成源文件;
process-sources:复制并处理资源文件到目标目录,准备打包;
generate-sources:生成源文件;
process-sources:复制并处理资源文件到目标目录,准备打包;
compile:编译源代码;(如果执行mvn compile:编译命令,则此生命周期到这儿为止);
process-class;
generate-test-sources:生成源文件;
process-test-sources:复制并处理资源文件到目标测试目录,准备打包;
generate-test-sources:生成源文件;
process-test-sources:复制并处理资源文件到目标测试目录,准备打包;
test-compile:编译测试源代码;(如果执行mvn test:命令,则此生命周期到这儿为止);
process-test-class;
test:使用单元测试框架进行测试,测试代码不会被打包或部署;
prepare-package;
package:接收编译好的代码,打包成jar或war格式;(如果执行mvn package:命令,则此生命周期到这儿为止);
pre-integration-test;
integration-test;
post-integration-test;
verify;
install:将包安装到本地仓库让其它项目依赖;(如果执行mvn install:命令,则此生命周期到这儿为止);
deploy:将包发布到远程仓库,让其它开发人员使用。
3.site
pre-site:完成在生成站点文件之前的工作;
site:生成项目站点文档;
post-site:完成在生成站点文件之后的工作,并为部署做准备。
maven插件:
maven在执行每个生命周期的时候,都会运行相关的插件。
这里在compile的时候,使用了maven-compiler-plugin。
<build>
<plugins><!—— 插件的引入 —->
<plugin><!—- 具体某个插件的引入 —->
<groupId>
org.apache.maven.plugin
</groupId>
<artifactId>
maven-source-plugin
</artifactId>
<executions><!—- 绑定到某个生命周期中 ——>
<execution>
<phase><!—— 指定绑定生命周期 ——>
compile
</phase>
<goals><!—— 执行的目标 ——>
<!—— 将jar文件打包 ——>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<build>
插件主要是用于辅助完成生命周期,亦或指定在某个生命周期完成特定的工作,插件中也可以依赖,但在插件中的依赖只限于当前插件中使用。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
以上插件用于设定java版本,不过现在设置java版本也可以通过:
<properties>
<java.version>1.8</java.version>
</properties>
<properties>标签来指定。
maven插件现用先查,通过apache官网查询。
maven仓库:
maven的仓库分为三类:本地仓库、私有仓库和中央仓库,如果所有的jar下载都走中央仓库会使访问过大并且中央仓库速度慢。所以一般情况下都会配置一个私有仓库。
配置仓库一般有两种方式:
一、在maven项目中的pom.xml中配置:
<repositories>
<repository>
<id>nexus</id>
<name>Central Repository</name>
<url>
http://127.0.0.1:8081/nexus/content/groups/public/
</url>
</repository>
</repositories>
以上代码配置一个私有仓库,这样就会让所有依赖的jar到上的url上去下载。
二、在maven安装目录下的settings.xml文件中配置:
<mirrors>
<mirror>
<id>nexus</id>
<name>Central Repository</name>
<url>http://127.0.0.1:8081/nexus/content/groups/public/
</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
以上配置了一个镜像, <mirrorOf>*</mirrorOf>中的*表示所有的jar下载都走镜像中的url,不会去中央仓库中找。
在settings中可以配置多个仓库,然后指定启动某个仓库地址:
<profiles>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>nexus</id>
<name>dev-nexus</name>
<url>>http://127.0.0.1:8081/nexus/content/groups/public/
</url>
<releases>
<enabled>true</enabled>
</releases>
<!——snaphots默认是关闭的,需要手动开启——>
<snapshots>
<enabled>true</enabled>
</snaphots>
</repository>
</repositories>
</profile>
</profiles>
<!——某个仓库为激活状态——>
<activeProfiles>
<!——只有激活之后才生效——>
<activeProfile>dev</activeProfile>
</activeProfiles>
发布到nexus:
首先需要在pom.xml中配置:
<distributionManagement>
<repository>
<id>releases</id>
<url>http://127.0.0.1:8081/nexus/content/repositories/releases
</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://127.0.0.1:8081/nexus/content/repositories/snapshots
</url>
</snapshotRepository>
</distributionManagement>
以上代码分别表示release版和snapshots版发布到指定的nexus url。
同时发布到nexus中需要权限,即需要在settings.xml文件中配置发布的用户名和密码如:
<servers>
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
</servers>