Maven自動部署(SCM-SVN/Git)(maven-scm-plugin/maven-release-plugin插件的使用)


以下內容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_deployment_automation.html

注意:Maven自動構建和自動部署的區別在於,構建只是編譯代碼階段,而部署是一整套代碼獲取到編譯再到打包發布的完整流程。

提示:其實本文提到的場景可能有一些矛盾,但只是出於演示效果,主要目的是實現Maven中利用POM進行代碼獲取、更新、提交、編譯再到部署的過程。

一、場景

在項目開發中,通常開發階段包含下面幾個步驟:

  • 將所有進行的項目的代碼提交到SVN/Git或者代碼庫中並打上標簽。
  • SVN/Git下載完整的源代碼。
  • 構建應用。
  • 存儲構建輸出的WAR或者EAR文件到一個常用的網絡位置下。
  • 從網絡上獲取文件並且部署文件到生產站點上。
  • 及時更新文檔並且更新應用的版本號。

二、問題說明

通常情況下上面提到開發過程中會涉及到多群人。一個團隊可能負責提交代碼,另一個團隊負責構建等等。很有可能由於涉及的人為操作和多團隊環境的原因,任意步驟都可能出錯。比如,較早的構建版本沒有在網絡計算機上被替換,然后部署團隊又重新部署了較早的構建版本。

三、解決方法

通過下面的方法自動化部署:

  • 使用SubVersion/Git,源碼倉庫來管理源代碼
  • 使用Maven構建和發布項目
  • 使用遠程倉庫管理軟件(Jfrog或者Nexus) 來管理項目二進制文件。

四、實現步驟

1、使用SCM實現從SVN上獲取代碼

說明:

①這里演示的效果為第一次從SVN獲取下來的代碼(也就是說本機要安裝SVN客戶端),然后在pom.xml文件中加入SCM支持,使其能通過Maven的命令去實現代碼的提交等操作,即不用SVN客戶端的參與了。

②如果想要實現第一次獲取代碼不用SVN客戶端的參與,可以這樣操作,先新建pom.xml文件,並配置SCM的節點支持SVN,然后使用SCM的checkout命令去獲取一次代碼,也就是用Maven去第一次從SVN中checkout代碼,然后在pom.xml文件中加入SCM支持,使其能通過Maven的命令去實現代碼的提交等操作。(也就是將第①步中用SVN客戶端獲取代碼的步驟省略了,后面都和第①步的一樣)

假設第一次已經從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>對應上面的節點,這里配置的devloperConnection是跟<devloperConnection>對應的。<connection>,<developerConnection>都是連接字符串,其后者是具有write權限的SCM連接。

SCM還有很多配置的細節,上面精簡的是為了演示基本功能基本夠用了,如要更深入的了解,可以參考以下網站:

maven-scm-plugin:http://maven.apache.org/scm/maven-scm-plugin/index.html

插件使用:http://maven.apache.org/scm/maven-scm-plugin/usage.html

<scm>節點完整屬性配置:http://maven.apache.org/pom.html#SCM

插件配置源代碼管理系統的支持(SVN/Git等):http://maven.apache.org/scm/scms-overview.html

maven-scm-plugin命令行:http://maven.apache.org/scm/maven-scm-plugin/plugin-info.html

配置好之后可以在命令行上運行進行測試,比如提交代碼和更新代碼的命令如下:

mvn scm:checkin -Dmessage="代碼提交日志" #代碼提交
mvn scm:update #代碼更新

接下來將使用maven-release-plugin進行版本的發布、回滾等操作,maven-release-plugin的用途是幫助自動化項目版本發布,它依賴於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時,如果你是一個不標准的SVN目錄(沒有trunk/tags/branches)的,那么必須配置<tagBase>屬性,再配置SCM的用戶名密碼(即SVN賬號密碼)。

通過以下命令行測試:

mvn release:clean #發布前的清理
mvn release:prepare #發布版本
mvn release:rollback #回滾版本

注意:在運行發布版本時,如果代碼修改過沒有遷入是不能通過的。當發布好版本之后會在上面定義的tag目錄中有一份代碼,如下所示:

maven-release-plugin插件還有很多命令,比如創建分支等(當然這一切都可以使用SVN客戶端操作,但是人為操作在大型多模塊項目來說就顯得過於龐大,使用插件的方式能節省很多人為操作的時間等),可以參考以下網站:

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

命令行參數(同時也是配置節點的說明):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對應%M2_HOME%\conf\settings.xml的<service>節點,配置如下:

<servers>
    ...
    <server> <id>oss</id> <username>admin</username> <password>admin123</password> </server>
    ...
</servers>

運行以下命令進行發布

mvn release:clean release:prepare release:perform

注意:運行命令前一定要把所有代碼都遷入,不然會報錯。當發布完成后,id會自動更新,並且是存放在Release庫中。

 

測試工程:https://github.com/easonjim/5_java_example/tree/master/maventest/test6/bus-core-api


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM