以下內(nèi)容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_deployment_automation.html:
注意:Maven自動構(gòu)建和自動部署的區(qū)別在于,構(gòu)建只是編譯代碼階段,而部署是一整套代碼獲取到編譯再到打包發(fā)布的完整流程。
提示:其實本文提到的場景可能有一些矛盾,但只是出于演示效果,主要目的是實現(xiàn)Maven中利用POM進(jìn)行代碼獲取、更新、提交、編譯再到部署的過程。
一、場景
在項目開發(fā)中,通常開發(fā)階段包含下面幾個步驟:
- 將所有進(jìn)行的項目的代碼提交到SVN/Git或者代碼庫中并打上標(biāo)簽。
- 從SVN/Git下載完整的源代碼。
- 構(gòu)建應(yīng)用。
- 存儲構(gòu)建輸出的WAR或者EAR文件到一個常用的網(wǎng)絡(luò)位置下。
- 從網(wǎng)絡(luò)上獲取文件并且部署文件到生產(chǎn)站點上。
- 及時更新文檔并且更新應(yīng)用的版本號。
二、問題說明
通常情況下上面提到開發(fā)過程中會涉及到多群人。一個團(tuán)隊可能負(fù)責(zé)提交代碼,另一個團(tuán)隊負(fù)責(zé)構(gòu)建等等。很有可能由于涉及的人為操作和多團(tuán)隊環(huán)境的原因,任意步驟都可能出錯。比如,較早的構(gòu)建版本沒有在網(wǎng)絡(luò)計算機(jī)上被替換,然后部署團(tuán)隊又重新部署了較早的構(gòu)建版本。
三、解決方法
通過下面的方法自動化部署:
- 使用SubVersion/Git,源碼倉庫來管理源代碼
- 使用Maven構(gòu)建和發(fā)布項目
- 使用遠(yuǎn)程倉庫管理軟件(Jfrog或者Nexus) 來管理項目二進(jìn)制文件。
四、實現(xiàn)步驟
1、使用SCM實現(xiàn)從SVN上獲取代碼
說明:
①這里演示的效果為第一次從SVN獲取下來的代碼(也就是說本機(jī)要安裝SVN客戶端),然后在pom.xml文件中加入SCM支持,使其能通過Maven的命令去實現(xiàn)代碼的提交等操作,即不用SVN客戶端的參與了。
②如果想要實現(xiàn)第一次獲取代碼不用SVN客戶端的參與,可以這樣操作,先新建pom.xml文件,并配置SCM的節(jié)點支持SVN,然后使用SCM的checkout命令去獲取一次代碼,也就是用Maven去第一次從SVN中checkout代碼,然后在pom.xml文件中加入SCM支持,使其能通過Maven的命令去實現(xiàn)代碼的提交等操作。(也就是將第①步中用SVN客戶端獲取代碼的步驟省略了,后面都和第①步的一樣)
假設(shè)第一次已經(jīng)從SVN獲取了代碼,然后按如下方式修改項目中的pom.xml文件:
<project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>bus-core-api</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>bus-core-api</name> <url>http://maven.apache.org</url> <scm> <developerConnection>scm:svn:https://jim:1@127.0.0.1:5443/svn/Test/trunk/bus-core-api/</developerConnection> </scm> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-scm-plugin</artifactId> <version>1.9.5</version> <configuration> <connectionType>developerConnection</connectionType> </configuration> </plugin> </plugins> </build></project>
提示:通過<connection>配置SVN的地址,其中地址上包含了賬號和密碼,然后再添加maven-scm-plugin插件,并配置<connectionType>對應(yīng)上面的節(jié)點,這里配置的devloperConnection是跟<devloperConnection>對應(yīng)的。<connection>,<developerConnection>都是連接字符串,其后者是具有write權(quán)限的SCM連接。
SCM還有很多配置的細(xì)節(jié),上面精簡的是為了演示基本功能基本夠用了,如要更深入的了解,可以參考以下網(wǎng)站:
maven-scm-plugin:http://maven.apache.org/scm/maven-scm-plugin/index.html
插件使用:http://maven.apache.org/scm/maven-scm-plugin/usage.html
<scm>節(jié)點完整屬性配置:http://maven.apache.org/pom.html#SCM
插件配置源代碼管理系統(tǒng)的支持(SVN/Git等):http://maven.apache.org/scm/scms-overview.html
maven-scm-plugin命令行:http://maven.apache.org/scm/maven-scm-plugin/plugin-info.html
配置好之后可以在命令行上運行進(jìn)行測試,比如提交代碼和更新代碼的命令如下:
mvn scm:checkin -Dmessage='代碼提交日志' #代碼提交mvn scm:update #代碼更新
接下來將使用maven-release-plugin進(jìn)行版本的發(fā)布、回滾等操作,maven-release-plugin的用途是幫助自動化項目版本發(fā)布,它依賴于POM中的SCM信息。修改項目的pom.xml文件如下:
<project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>bus-core-api</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>bus-core-api</name> <url>http://maven.apache.org</url> <scm> <developerConnection>scm:svn:https://jim:1@127.0.0.1:5443/svn/Test/trunk/bus-core-api/</developerConnection> </scm> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-scm-plugin</artifactId> <version>1.9.5</version> <configuration> <connectionType>developerConnection</connectionType> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> <configuration> <username>jim</username> <password>1</password> <tagBase>https://127.0.0.1:5443/svn/Test/tags</tagBase> <releaseProfiles>release</releaseProfiles> </configuration> </plugin> </plugins> </build></project>
注意:在使用maven-release-plugin時,如果你是一個不標(biāo)準(zhǔn)的SVN目錄(沒有trunk/tags/branches)的,那么必須配置<tagBase>屬性,再配置SCM的用戶名密碼(即SVN賬號密碼)。
通過以下命令行測試:
mvn release:clean #發(fā)布前的清理mvn release:prepare #發(fā)布版本mvn release:rollback #回滾版本
注意:在運行發(fā)布版本時,如果代碼修改過沒有遷入是不能通過的。當(dāng)發(fā)布好版本之后會在上面定義的tag目錄中有一份代碼,如下所示:
maven-release-plugin插件還有很多命令,比如創(chuàng)建分支等(當(dāng)然這一切都可以使用SVN客戶端操作,但是人為操作在大型多模塊項目來說就顯得過于龐大,使用插件的方式能節(jié)省很多人為操作的時間等),可以參考以下網(wǎng)站:
maven-release-plugin:http://maven.apache.org/maven-release/maven-release-plugin/index.html
使用方法:http://maven.apache.org/maven-release/maven-release-plugin/usage.html
命令行參數(shù)(同時也是配置節(jié)點的說明):http://maven.apache.org/maven-release/maven-release-plugin/plugin-info.html
接下來是配置提交到Nexus的私有倉庫,修改pom.xml如下:
<project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>bus-core-api</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>bus-core-api</name> <url>http://maven.apache.org</url> <distributionManagement> <repository> <id>oss</id> <url>http://127.0.0.1:8081/repository/maven-releases/</url> </repository> <snapshotRepository> <id>oss</id> <url>http://127.0.0.1:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> <scm> <developerConnection>scm:svn:https://jim:1@127.0.0.1:5443/svn/Test/trunk/bus-core-api/</developerConnection> </scm> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-scm-plugin</artifactId> <version>1.9.5</version> <configuration> <connectionType>developerConnection</connectionType> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> <configuration> <username>jim</username> <password>1</password> <tagBase>https://127.0.0.1:5443/svn/Test/tags</tagBase> <releaseProfiles>release</releaseProfiles> </configuration> </plugin> </plugins> </build></project>
其中上面的id對應(yīng)%M2_HOME%\conf\settings.xml的<service>節(jié)點,配置如下:
<servers> ... <server> <id>oss</id> <username>admin</username> <password>admin123</password> </server> ...</servers>
運行以下命令進(jìn)行發(fā)布
mvn release:clean release:prepare release:perform
注意:運行命令前一定要把所有代碼都遷入,不然會報錯。當(dāng)發(fā)布完成后,id會自動更新,并且是存放在Release庫中。
測試工程:https://github.com/easonjim/5_java_example/tree/master/maventest/test6/bus-core-api