關(guān)于release與snapshot的區(qū)別這里不再贅述,maven項(xiàng)目在生產(chǎn)環(huán)境部署的時(shí)候一定是需要打release包的。
maven-release-plugin 可用于構(gòu)建release版本項(xiàng)目,實(shí)現(xiàn)自動(dòng)打tag、遞增版本號(hào)、分發(fā)release版本jar包至倉庫。
pom.xml配置:
<!--git 遠(yuǎn)程倉庫配置-->
<scm>
<connection>scm:git:http://項(xiàng)目git地址</connection>
<url>項(xiàng)目git地址(不加'.git后綴')</url>
<developerConnection>scm:項(xiàng)目git地址</developerConnection>
</scm>
<!--構(gòu)建配置-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<tagNameFormat>v@{project.version}</tagNameFormat>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
</plugins>
</build>
<!--分發(fā)配置-->
<distributionManagement>
<repository>
<id>deploymentRepo</id>
<name>releases</name>
<url>http://somehost/repository/maven-releases/</url>
<uniqueVersion>true</uniqueVersion>
</repository>
<snapshotRepository>
<id>deploymentRepo</id>
<name>snapshots</name>
<url>http://somehost/repository/maven-snapshots/</url>
<uniqueVersion>true</uniqueVersion>
</snapshotRepository>
</distributionManagement>
由于會(huì)將構(gòu)建的包分發(fā)到倉庫,需要在maven的配置文件setting.xml下加入權(quán)限配置:
<server>
<id>your id</id>
<username>your username</username>
<password>your pass</password>
</server>
這里最關(guān)鍵的一點(diǎn)是scm節(jié)點(diǎn)配置,無論項(xiàng)目是使用git、svn或是其他版本控制工具,都可以在這里配置,詳細(xì)的配置可參考scms-overview。
下面是一個(gè)項(xiàng)目配置示例:
<scm>
<connection>scm:git:http://git-local.bnz.com/srv/bnz-ep.git</connection>
<url>http://git-local.bnz.com/srv/bnz-ep</url>
<developerConnection>scm:git:http://git-local.bnz.com/srv/bnz-ep.git</developerConnection>
</scm>
如果需要跳過單元測試,可以加入?yún)?shù) -Darguments="-DskipTests",直接使用-Dmaven.test.skip=true是無效的。
在執(zhí)行mvn release:perform時(shí)默認(rèn)會(huì)生成api文檔,如果默寫注釋不符合規(guī)范的話會(huì)造成構(gòu)建失敗,可以加參數(shù)-DuseReleaseProfile=false取消構(gòu)建api文檔,或則需要根據(jù)規(guī)范書寫注釋。
mvn release:prepare
mvn release:perform
如果在構(gòu)建過程中出現(xiàn)錯(cuò)誤,rollback回滾即可
mvn release:rollback