對Maven而言,POM(Poject Object,項目對象)文件就是一個項目的全部,再不需要其他的配置文件來描述項目了!當在 cmd / shell 中執行mvn、對改項目進行package或其他操作時,只要 cd 到pom文件所在的目錄即可,當然,前提是需要將mvn配置在系統路徑中。
POM結構
基本配置
引用root-pom - 這個很重要,後面會說明
開發者信息 - “猩猩苦苦”幹了半天,總要留個名吧
構建配置 - 包括必要的插件配置
依賴配置 - 核心的核心,很多人使用Maven就混亂在這裏,本文將提到如何在root-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-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>[email protected]</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>[email protected]</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
無特殊情況,同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
基礎第三方庫版本定義的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>[email protected]</email>
</developer>
</developers>
<developers>
<developer>
<name>張三</name>
<email>[email protected]</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>
依賴配置
應該對第三方的依賴和對組織內(第二方)的依賴區別而待。
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>
...
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 。
還有一些更好的實踐?希望你能告訴我:-)