首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

使用 Maven 2 進行團隊配置管理:建立一致開發環境的最佳實務

     編輯:Java 相關知識

對團隊而言,建立統一開發環境是必須,Maven 能有效協助建立統一環境。本文介紹如何更有效進行統一配置。

準備工作

下載必要軟體:

建立一致開發環境

共享開發環境中,較好建議保持 Maven 兩個不同配置檔案分別管理,包含共享與使用者自訂設定。共同配置置於安裝目錄,單獨開發設定保存在使用者本地目錄。

全域配置檔 settings.xml

<servers>
  <!-- 公司內部庫,所有 release 版本,serverid 對應 repository id,用於 deploy 時存取,主要保存用戶名密碼 -->
  <server>
    <id>internal</id>
    <username>${website.username}</username>
    <password>${website.pwd}</password>
    <filePermissions>664</filePermissions>
    <directoryPermissions>775</directoryPermissions>
  </server>
  <!-- 目前開發庫,用於 snapshot 庫 -->
  <server>
    <id>snapshot</id>
    <username>${website.username}</username>
    <password>${website.pwd}</password>
    <filePermissions>664</filePermissions>
    <directoryPermissions>775</directoryPermissions>
  </server>
</servers>
<profiles>
  <!-- 定義核心庫 maven 鏡像,由 maven-proxy 實現 -->
  <profile>
    <id>central-repo</id>
    <repositories>
      <repository>
        <id>central</id>
        <name>Internal Repository</name>
        <url>https://192.168.0.2:9999/repository</url>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <id>central</id>
        <name>Internal Repository</name>
        <url>https://192.168.0.2:9999/repository</url>
      </pluginRepository>
    </pluginRepositories>
  </profile>
  <!-- 定義內部庫,包含公司所有 release 版本 -->
  <profile>
    <id>internal-repo</id>
    <repositories>
      <repository>
        <id>internal</id>
        <name>Internal Repository</name>
        <url>https://192.168.0.2:8080/repo-local</url>
        <releases>
          <enabled>true</enabled>
          <updatePolicy>never</updatePolicy>
          <checksumPolicy>warn</checksumPolicy>
        </releases>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <id>internal</id>
        <name>Internal Plugin Repository</name>
        <url>https://192.168.0.2:8080/repo-local</url>
        <releases>
          <enabled>true</enabled>
          <updatePolicy>never</updatePolicy>
          <checksumPolicy>warn</checksumPolicy>
        </releases>
      </pluginRepository>
    </pluginRepositories>
  </profile>
  <!-- 定義內部開發庫,也可合併 snapshot 和 release -->
  <profile>
    <id>snapshot-repo</id>
    <repositories>
      <repository>
        <id>snapshot</id>
        <name>Internal Repository</name>
        <url>https://192.168.0.2:8080/repo-snapshot</url>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>interval:60</updatePolicy>
          <checksumPolicy>warn</checksumPolicy>
        </snapshots>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <id>snapshot</id>
        <name>Internal Plugin Repository</name>
        <url>https://192.168.0.2:8080/repo-snapshot</url>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>interval:60</updatePolicy>
          <checksumPolicy>warn</checksumPolicy>
        </snapshots>
      </pluginRepository>
    </pluginRepositories>
  </profile>
</profiles>
<!-- 激活相應配置 -->
<activeProfiles>
  <activeProfile>central-repo</activeProfile>
  <activeProfile>internal-repo</activeProfile>
  <activeProfile>snapshot-repo</activeProfile>
</activeProfiles>
<!-- 插件預設 groupId -->
<pluginGroups>
  <pluginGroup>com.mycompany.plugins</pluginGroup>
</pluginGroups>

包含共享因素:

  • 伺服器設定通常共同,只有用戶名需在使用者環境設定。使用一致定義配置共同設定
  • Profile 定義共同因素,內部開發庫,包含指定組織或部門發布產品。這些庫獨立於核心開發庫
  • 啟用的 profiles 列表,用於激活相應 profile
  • Plugin 群組:只有組織中有自訂插件,用於命令列在 pom 中定義時需要

單獨使用者設定

<settings>
  <profiles>
    <profile>
      <id>property-overrides</id>
      <properties>
        <website.username>myuser</website.username>
        <website.pwd>test</website.pwd>
      </properties>
    </profile>
  </profiles>
</settings>

建立共享開發庫

大多數組織建立內部開發庫用於配置,中央開發庫用於連接 Maven。

設定內部開發庫簡單,使用 HTTP 協定,可用既有 HTTP 伺服器或建立新服務(Apache 或 Jetty)。假設伺服器位址 192.168.0.2,連接埠 8080:

  • https://192.168.0.2:8080/repo-local(release 庫)
  • https://192.168.0.2:8080/repo-snapshot(snapshot 庫)

中央鏡像庫使用 maven-proxy 建立,也可自建鏡像,用於下載本地庫沒有的 artifact。

maven-proxy 設定

下載 maven-proxy-standalone-0.2-app.jar 與 proxy.properties。命令列執行:

java -jar maven-proxy-standalone-0.2-app.jar proxy.properties

主要配置:在 repo.list 增加相應庫,如:

repo.list=repo1.maven.org,...
# maven 中央庫
repo.repo1.maven.org.url=https://repo1.maven.org/maven2
repo.repo1.maven.org.description=maven.org
repo.repo1.maven.org.proxy=one
repo.repo1.maven.org.hardfail=false
repo.repo1.maven.org.cache.period=360000
repo.repo1.maven.org.cache.failures=true

以後所有遠端庫皆透過此方式增加。順便說明,別忘了註釋原來的 example,那是無法存取的。

其他配置:連接埠 port=9999、儲存位置 repo.local.store=target/repo、serverName=https://localhost:9999。

建立標準組織 POM

定義共同內容,包含公司結構:組織、部門、團隊。參考 Maven 自身結構。

如 SCM 設定:

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-parent</artifactId>
    <version>1</version>
  </parent>
  <groupId>org.apache.maven.scm</groupId>
  <artifactId>maven-scm</artifactId>
  <url>https://maven.apache.org/maven-scm/</url>
  ...
  <modules>
    <module>maven-scm-api</module>
    <module>maven-scm-providers</module>
    ...
  </modules>
</project>

Maven 父專案定義:

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.apache</groupId>
    <artifactId>apache</artifactId>
    <version>1</version>
  </parent>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-parent</artifactId>
  <version>5</version>
  <url>https://maven.apache.org/</url>
  ...
  <mailingLists>
    <mailingList>
      <name>Maven Announcements List</name>
      <post>announce@maven.apache.org</post>
      ...
    </mailingList>
  </mailingLists>
  <developers>
    <developer> ... </developer>
  </developers>
</project>

Maven 父 POM 包含共享元素:宣告郵件列表、開發者。大多數專案繼承 Apache 組織:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.apache</groupId>
  <artifactId>apache</artifactId>
  <version>1</version>
  <organization>
    <name>Apache Software Foundation</name>
    <url>https://www.apache.org/</url>
  </organization>
  <url>https://www.apache.org/</url>
  ...
  <repositories>
    <repository>
      <id>apache.snapshots</id>
      <name>Apache Snapshot Repository</name>
      <url>https://svn.apache.org/maven-snapshot-repository</url>
      <releases><enabled>false</enabled></releases>
    </repository>
  </repositories>
  ...
  <distributionManagement>
    <repository>...</repository>
    <snapshotRepository>...</snapshotRepository>
  </distributionManagement>
</project>

對專案自身,父 POM 很少更新。最後方式:保存父 POM 檔案在單獨版本控制區域,能 check out、修改與配置。

使用 Continuous Integration 持續整合

持續整合自動建構專案,透過一定時間,包含所有衝突在早期察覺而非發布時。也是種良好開發方式,使團隊成員產生細微互動變動,更有效支援平行開發流程。

可用 Maven Continuum 作為持續整合服務。

安裝 Continuum 簡單:

C:\mvnbook\continuum-1.0.3> bin\win32\run

透過 https://localhost:8082/continuum 驗證。

為支援 Continuum 發送郵件提醒,需相應 SMTP 服務發送訊息。預設 localhost:25,若未設定編輯檔案變更 smtp-host 設定。

設定 SVN 目錄

svn co file://localhost/C:/mvnbook/svn/proficio/trunk proficio

編輯 pom.xml 設定正確郵件地址:

<ciManagement>
  <system>continuum</system>
  <url>https://localhost:8080/continuum</url>
  <notifiers>
    <notifier>
      <type>mail</type>
      <configuration>
        <address>youremail@yourdomain.com</address>
      </configuration>
    </notifier>
  </notifiers>
</ciManagement>
...
<scm>
  <connection>scm:svn:file://localhost/c:/mvnbook/svn/proficio/trunk</connection>
  <developerConnection>scm:svn:file://localhost/c:/mvnbook/svn/proficio/trunk</developerConnection>
</scm>
...
<distributionManagement>
  <site>
    <id>website</id>
    <url>file://localhost/c:/mvnbook/repository/sites/proficio/reference/${project.version}</url>
  </site>
</distributionManagement>

提交對應 pom,執行 mvn install。返回 https://localhost:8082/continuum 可見專案列表。登入後選擇 Maven 2.0 專案增加對應專案,可增加 URL 或提交本地內容。使用本地 pom URL:file://localhost/c:mvnbook/proficio/pom.xml。提交後 Continuum 回傳成功訊息。

持續整合原則

  • 早提交、經常提交:使用者經常提交時持續整合最有效,不意味提交不正確程式碼
  • 經常執行建構:用於最快檢測失敗
  • 盡快修正失敗:失敗發生時應馬上修正
  • 建議有效版本
  • 執行 clean build
  • 執行複雜綜合測試
  • 建構所有專案結構分支
  • 持續執行專案副本
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved