LZ最近在公司裏開發一套公司本身的項目框架,而且封裝了一些類庫,採用的是maven來構建項目,每一個項目都會涉及到版本的更新,maven項目通常的升級步驟是:html
一、經過mvn deploy命令發佈當前版本到maven倉庫
java
二、將當前版本在svn服務器上打上一個標籤web
三、修改每一個maven項目的pom.xml,改變全部版本爲新版本redis
這樣作固然沒有任何問題,可是若是版本更新速度很快,一天甚至可能發佈N個項目,那麼每次都重複這樣的步驟,那你離死也不遠了,全部LZ想到了版本的自動管理,要作這樣的工做首先須要準備工做:apache
一、搭建maven倉庫服務器
二、本地計算機安裝可以在命令行工做的svn框架
三、下載mavenmaven
四、安裝maven,而且設置環境變量編輯器
在使用過程當中,LZ也遇到了不少問題,而且網上也沒有什麼解決方案,爲了避免讓你們走不少彎路,LZ把本身遇到的一些問題提出來。svn
咱們把maven下載後,解壓到當前要使用版本管理的項目的根目錄下,並改命爲EMBEDDED,如圖:
由於當你構建的時候,maven會到該目錄下去找bin/mvn.bat,文件,而後進入EMBEDDED/bin將mvn.cmd改命爲mvn.bat,再用文本編輯器打開該文件,搜索「mvn.cmd」,改爲"mvn.bat",如:
設置環境變量:
在path中加入:
而後打開命令行,輸入mvn -version 沒有報錯說明maven安裝成功
接下來,咱們來看怎麼樣才能實現版本自動管理,要作自動版本管理,少不了maven的一個插件:maven-release-plugin,咱們將該插件加入到plugin中:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> <configuration> <tagBase>http://192.168.1.242/svn/java/sunsharp/tags/</tagBase> <username>liyi</username> <password>li897yi</password> <releaseProfiles>release</releaseProfiles> <checkModificationExcludes> <checkModificationExclude>.project</checkModificationExclude> <checkModificationExclude>EMBEDDED</checkModificationExclude> <checkModificationExclude>release.properties</checkModificationExclude> <checkModificationExclude>sunsharp-module\.project</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-dao\.classpath</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-dao\.project</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-dao\.settings</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-encrypt\.classpath</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-encrypt\.project</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-encrypt\.settings</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-http\.classpath</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-http\.project</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-mail\.classpath</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-mail\.project</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-redis\.classpath</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-redis\.project</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-upload\.classpath</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-upload\.project</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-utils\.classpath</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-utils\.project</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-web\.classpath</checkModificationExclude> <checkModificationExclude>sunsharp-module\sunsharp-web\.project</checkModificationExclude> </checkModificationExcludes> </configuration> </plugin>
<scm> <connection>scm:svn:http://192.168.1.242/svn/java/sunsharp/sunsharp/</connection> <developerConnection>scm:svn:http://192.168.1.242/svn/java/sunsharp/sunsharp/</developerConnection> <url>http://192.168.1.242/svn/java/sunsharp/sunsharp/</url> </scm>你要發佈到maven倉庫,確定須要指定maven倉庫地址:
<distributionManagement> <repository> <id>releases</id> <name>Internal Releases</name> <url> http://192.168.1.241:8081/nexus/content/repositories/releases </url> </repository> <snapshotRepository> <id>snapshots</id> <name>Internal Snapshots</name> <url> http://192.168.1.241:8081/nexus/content/repositories/snapshots </url> </snapshotRepository> </distributionManagement>若是採用插件自動發佈,是會發布到releases中去的,若是倉庫配置了登陸權限的,這裏須要在settings.xml中配置,如:
<server> <id>releases</id> <username>admin</username> <password>******</password> </server>這樣準備工做算是完成了,咱們可使用命令:
mvn release:clean 清除記錄
mvn release:prepare:準備工做,他會檢查是否有代碼沒有提交,若是沒有提交會報錯,若是版本號不是以SNAPSHOT結束的也會報錯,在執行前須要在指定的svn tag目錄下建立一個目錄,命名規則是項目名-版本號,例如LZ這裏就是sunsharp-1.0.0,不然他會提示找不到目錄
mvn release:perform 執行,他會改變pom.xml的版本號爲下一個版本,執行deploy發佈到maven倉庫的release中
mvn release:rollback 回滾,若是以爲當前有問題能夠執行回滾
具體release的用法能夠百度查找,裏面有不少參數設置,能夠設置當前版本,下一個版本,能夠設置試運行(-DtryRun=true),在測試階段最好加上這個參數,加上後執行perform命令,不會真正的提交到倉庫,等沒有報錯了再去掉這個參數,而後就能夠真正的發佈到maven倉庫中了。
LZ在使用過程當中也遇到了不少問題,經過不懈努力才得以解決,各位童鞋在使用過程當中可能也會遇到一些問題,若是不懂的能夠給我留言,我看到後會第一時間回覆。