本文不想討論Maven是什麼、能做什麼、我們選擇Maven有什麼好處……本文只是提出在公司大規模開發環境中使用Maven技術的一個案例、一次實踐、一種思路,供大家借鑑和參考。至於是不是“最佳”?當然不是!因為沒有“最佳”,只有“更佳”:-)
POM,不得不說一下
對Maven而言,POM(Poject Object,專案物件)檔案就是一個專案的全部,再不需要其他的配置檔案來描述專案了!當在 cmd / shell 中執行mvn、對改專案進行package或其他操作時,只要 cd 到pom檔案所在的目錄即可,當然,前提是需要將mvn配置在系統路徑中。
POM結構
POM檔案通常只包括 5 個部分,建議按照這個順序配置:
基本配置
引用root-pom – 這個很重要,後面會說明
開發者資訊 – “猩猩苦苦”幹了半天,總要留個名吧
構建配置 – 包括必要的外掛配置
依賴配置 – 核心的核心,很多人使用Maven就混亂在這裡,本文將提到如何在root-pom上做文章並解決之
除了以上5個部分,專案還可適當增加1個 <modules> 結點,其他結點沒有強力的理由,建議謹慎增加。
整體示例
view plaincopy to clipboardprint?
<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.sample</groupId> 
    <artifactId>sample-commons-mvc</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>sample-commons-mvc</name> 
    <!– 引用 Root POM –> 
    <parent> 
        <groupId>com.sample</groupId> 
        <artifactId>sample-root-pom</artifactId> 
        <version>1.0-SNAPSHOT</version> 
    </parent> 
    <!– 開發者 –> 
    <developers> 
        <developer> 
            <name>zhangsan</name> 
            <email>zhangsan@sample.com</email> 
        </developer> 
    </developers> 
      
    <!– 構建配置(只新增本專案特有的) –> 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-jar-plugin</artifactId> 
                <configuration> 
                    <archive> 
                        <manifestEntries> 
                            <Rose>applicationContext</Rose> 
                        </manifestEntries> 
                    </archive> 
                </configuration> 
            </plugin> 
        </plugins> 
    </build> 
    <!– 依賴配置 –> 
    <dependencies> 
        <!– 第三方依賴,不需要指定版本號,不需要寫scope –> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>javax.servlet</groupId> 
            <artifactId>servlet-api</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>net.user</groupId> 
            <artifactId>user-api</artifactId> 
        </dependency> 
        <!– 對組織內的依賴,需要指定版本號,以及可能的scope –> 
        <dependency> 
            <groupId>com.sample</groupId> 
            <artifactId>sample-core</artifactId> 
            <version>1.0-SNAPSHOT</version> 
        </dependency> 
    </dependencies> 
</project> 
<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.sample</groupId>
 <artifactId>sample-commons-mvc</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>sample-commons-mvc</name>
 <!– 引用 Root POM –>
 <parent>
  <groupId>com.sample</groupId>
  <artifactId>sample-root-pom</artifactId>
  <version>1.0-SNAPSHOT</version>
 </parent>
 <!– 開發者 –>
 <developers>
  <developer>
   <name>zhangsan</name>
   <email>zhangsan@sample.com</email>
  </developer>
 </developers>
 
 <!– 構建配置(只新增本專案特有的) –>
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
     <archive>
      <manifestEntries>
       <Rose>applicationContext</Rose>
      </manifestEntries>
     </archive>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <!– 依賴配置 –>
 <dependencies>
  <!– 第三方依賴,不需要指定版本號,不需要寫scope –>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
  </dependency>
  <dependency>
   <groupId>net.user</groupId>
   <artifactId>user-api</artifactId>
  </dependency>
  <!– 對組織內的依賴,需要指定版本號,以及可能的scope –>
  <dependency>
   <groupId>com.sample</groupId>
   <artifactId>sample-core</artifactId>
   <version>1.0-SNAPSHOT</version>
  </dependency>
 </dependencies>
</project>
基本配置
view plaincopy to clipboardprint?
<modelVersion>4.0.0</modelVersion> 
<groupId>com.sample</groupId> 
<artifactId>sample-xxx-yyy</artifactId> 
<version>1.0-SNAPSHOT</version> 
<packaging>jar/war/pom</packaging> 
<name>sample1/sample2-xxx-yyy</name> 
<url>http://repos.sample.com</url> 
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>sample-xxx-yyy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar/war/pom</packaging>
<name>sample1/sample2-xxx-yyy</name>
<url>http://repos.sample.com</url>
說明:
artifactId
sample-xxx-yyy,即:主工程名-第一功能說名-第二功能說明
version
如果只是對組織內使用的話,建議固定用一個版本:例如 1.0-SNAPSHOT
packaging
如上所示3選1,一般是 jar 或 war ,部分基礎的 root-pom 可以為 pom
name
無特殊情況,同artifactId
引用Root POM
view plaincopy to clipboardprint?
<parent> 
    <groupId>com.sample</groupId> 
    <artifactId>sample-root-pom</artifactId> 
    <version>1.0-SNAPSHOT</version> 
</paren 
 <parent>
  <groupId>com.sample</groupId>
  <artifactId>sample-root-pom</artifactId>
  <version>1.0-SNAPSHOT</version>
 </paren
這是很重要的一點:各個工程公用的配置,都可以統一的歸於這個 root-pom,例如第三方的庫的版本號、必需的本組織(第二方)依賴庫……這樣做的可以有效的避免依賴庫版本的混亂 ,好好設計這個 root-pom 吧,依據總體規劃的複雜度,你仍然可以對 root-pom 再進行分層,例如:
基礎第三方庫版本定義的pom
view plaincopy to clipboardprint?
<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.sample</groupId> 
    <artifactId>sample-3rd-commons-versions</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>pom</packaging> 
    <name>sample-3rd-commons-versions</name> 
    <url>http://maven.apache.org</url> 
    <properties> 
        <junit-version>4.6</junit-version> 
        <spring-mock-version>2.0.8</spring-mock-version> 
        <el-api-version>1.0</el-api-version> 
        <activation-version>1.1</activation-version> 
        <aopalliance-version>1.0</aopalliance-version> 
        <servlet-api-version>2.4</servlet-api-version> 
        <jsp-api-version>2.0</jsp-api-version> 
… 
<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.sample</groupId>
 <artifactId>sample-3rd-commons-versions</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>sample-3rd-commons-versions</name>
 <url>http://maven.apache.org</url>
 <properties>
  <junit-version>4.6</junit-version>
  <spring-mock-version>2.0.8</spring-mock-version>
  <el-api-version>1.0</el-api-version>
  <activation-version>1.1</activation-version>
  <aopalliance-version>1.0</aopalliance-version>
  <servlet-api-version>2.4</servlet-api-version>
  <jsp-api-version>2.0</jsp-api-version>
…  
基礎第三方庫的pom
view plaincopy to clipboardprint?
<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> 
    <parent> 
        <groupId>com.sample</groupId> 
        <artifactId>sample-3rd-commons-versions</artifactId> 
        <version>1.0-SNAPSHOT</version> 
    </parent> 
    <groupId>com.sample</groupId> 
    <artifactId>sample-core</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <dependencies> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
            <version>${junit-version}</version> 
            <scope>test</scope> 
        </dependency> 
        <dependency> 
            <groupId>commons-logging</groupId> 
            <artifactId>commons-logging</artifactId> 
            <version>${commons-logging-version}</version> 
        </dependency> 
… 
<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>
 <parent>
  <groupId>com.sample</groupId>
  <artifactId>sample-3rd-commons-versions</artifactId>
  <version>1.0-SNAPSHOT</version>
 </parent>
 <groupId>com.sample</groupId>
 <artifactId>sample-core</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>${junit-version}</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>${commons-logging-version}</version>
  </dependency>
開發者
簡單地寫上負責人以及主要參與人員的名字資訊,他人通過pom可以檢視作者。
view plaincopy to clipboardprint?
<developers> 
 <developer> 
  <name>張三</name> 
  <email>san.zhang@sample.com</email> 
 </developer> 
</developers> 
 <developers>
  <developer>
   <name>張三</name>
   <email>san.zhang@sample.com</email>
  </developer>
 </developers>
構建配置
可以在相應的 root-pom 中配置一些常用的外掛,如:
規定編譯的原始檔使用的是UTF-8編碼
只編譯 src/main/java 位置的原始檔
將 src/main/java 中的 xml, properties, java 檔案打包到jar中
……
某個專案特有的,如:要在MANIFEST檔案中加入一個屬性,應在該專案的 pom 中單獨配置: 
view plaincopy to clipboardprint?
<build> 
 <plugins> 
  <plugin> 
   <groupId>org.apache.maven.plugins</groupId> 
   <artifactId>maven-jar-plugin</artifactId> 
   <configuration> 
    <archive> 
     <manifestEntries> 
      <Name>value</Name> 
     </manifestEntries> 
    </archive> 
   </configuration> 
  </plugin> 
 </plugins> 
</build> 
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
     <archive>
      <manifestEntries>
       <Name>value</Name>
      </manifestEntries>
     </archive>
    </configuration>
   </plugin>
  </plugins>
 </build>
 
依賴配置
應該對第三方的依賴和對組織內(第二方)的依賴區別而待。
對第三方的依賴,不要寫版本號,不需要寫scope,因為這應該在相應的 root-pom 中指定:
view plaincopy to clipboardprint?
<dependencies> 
    <dependency> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>javax.servlet</groupId> 
        <artifactId>servlet-api</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>net.user</groupId> 
        <artifactId>user-api</artifactId> 
    </dependency> 
.. 
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
  </dependency>
  <dependency>
   <groupId>net.user</groupId>
   <artifactId>user-api</artifactId>
  </dependency>

 
對組織內的依賴,建議統一一個固定版本號(1.0-SNAPSHOT),這會減少很多協作上的麻煩:
view plaincopy to clipboardprint?
…  
        <dependency> 
            <groupId>com.sample</groupId> 
            <artifactId>sample-core</artifactId> 
            <version>1.0-SNAPSHOT</version> 
        </dependency> 
        <dependency> 
            <groupId>com.sample.xxx</groupId> 
            <artifactId>xxx-adapter</artifactId> 
            <version>1.0-SNAPSHOT</version> 
        </dependency> 
        <dependency> 
            <groupId>com.sample.ccc</groupId> 
            <artifactId>ccc-util</artifactId> 
            <version>1.0-SNAPSHOT</version> 
        </dependency> 
    </dependencies> 

  <dependency>
   <groupId>com.sample</groupId>
   <artifactId>sample-core</artifactId>
   <version>1.0-SNAPSHOT</version>
  </dependency>
  <dependency>
   <groupId>com.sample.xxx</groupId>
   <artifactId>xxx-adapter</artifactId>
   <version>1.0-SNAPSHOT</version>
  </dependency>
  <dependency>
   <groupId>com.sample.ccc</groupId>
   <artifactId>ccc-util</artifactId>
   <version>1.0-SNAPSHOT</version>
  </dependency>
 </dependencies>
一些其他建議
<distributionManagement> 及 <repository> 的配置,放在合適的 root-pom 中定義吧。
在相應的 root-pom 中配置 maven 的預設結構、build/resources/resouce 等配置項。還可以包括諸如 .svn 檔案不會被打到jar中等細節。
在相應的 root-pom 中配置 defaultGoal ,建議為 install,即 $ mvn 等價於 $mvn install 。
還有一些更好的實踐?希望你能告訴我:-)
< type=”text/javascript”>