一個Maven2工程的核心就是這一個pom.xml,它包含了你的工程詳細信息,如:版本、配置、依賴、測試、項目成員等等。學習maven2主要就是學習如何配置pom.xml。
一個簡單的而完全可操作的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> <!--maven2-->
<groupId>ctzj_bss</groupId> <!-- 這個項目所屬組的id,一般是項目所在的公司或組織的域名 -->
<artifactId>NRC</artifactId> <!-- 項目的id,和groupId一起組成這個項目的一個唯一id,以用在別的maven2工程中 -->
<packaging>jar</packaging> <!-- 最后這個工程的打包類型,在這里是打成jar包 -->
<version>1.0</version> <!-- 版本號 -->
<name>kenan nrc batch</name> <!-- 項目名, 區別于artifactId-->
<description>insert a nrc in kenan db</description> <!-- 項目描述,會出現在生成的工程站點上 -->
<build> <!-- 項目的構建信息 -->
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<outputDirectory>target\classes</outputDirectory>
<testOutputDirectory>target\test-classes</testOutputDirectory>
<directory>target</directory> <!-- 構建后生成文件(jar,site等)所在位置 -->
</build>
<dependencies> <!-- 項目的依賴外接包,maven的優勢之一 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting> <!-- 項目報表,有很多插件可供選擇 -->
<outputDirectory>target/site</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>
在你配置了你的pom文件后,你就可以使用如下命令來運行maven2
mvn compiler:compile 編譯你的原文件
mvn jar:jar 打包
mvn site:site 生成項目站點
接下來逐個介紹pom.xml的各個要素
developor,organization and mail lit
build
dependencies
reporting
1.developor,organization and mail list
可以在pom中加入開發人員列表,項目所屬組織信息,郵件列表,這些信息會出現在生成的項目站點上
<organization>
<name>CTZJ BSS</name>
</organization>
<developers>
<developer>
<name>Chen Lin</name>
<organization>ZJHCsoft</organization>
<email>chenlin@zjhcsoft.com</email>
</developer>
</developers>
<mailingLists>
<mailingList>
<name>${pom.name} Dev List</name>
</mailingList>
<mailingList>
<name>${pom.name} User List</name>
</mailingList>
</mailingLists>
2. build
配置項目的構建信息,主要有指定目錄,包括源代碼、測試源代碼、輸出目錄、資源目錄等.如果沒有特殊需求以上的pom已經足夠了
3. dependencies
配置項目要用到的jar包.自認為maven對jar包的管理是它的一個重要特色.下面是一個jar包的配置:
<dependency>
<groupId>concurrent</groupId>
<artifactId>concurrent</artifactId>
<version>1.3.4</version>
<scope>compile</scope>
</dependency>
groupId: 沒有特定的含義,一般來自同一組織的jar包會有相同的groupId值
artifactId: 這個jar的id,一般是這個jar包的名稱
version: jar包的版本號,一般情況下,artifactId-version.jar就是這個jar包的文件名
scope: 這個jar所影響的范圍,compile指它會在編譯源代碼時用到.還有test,provide,runtime等值
如果你不知道jar包的groupId和artifactId,可以到http://www.mavenregistry.com/ 進行查詢
一旦指定后,當你進行編譯時,它就會到你的本地資源庫
另,如果你引入本地包,則需要先行將它安裝到本地資料庫(repository),使用如下命令
mvn install:install-file -DgroupId=%groupId% -DartifactId=%artifactId% -DgeneratePom=true -Dfile=%file% -Dpackaging=jar -Dversion=%version%
4. reporting
通過使用相應的report plug-in可以生成各種各樣的report.如javadoc,junit test report,代碼檢查等
常用的常見有
<plugin>
<groupId>org.apache.maven.plugins</groupId> <!--java doc -->
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId> <!-- 代碼檢查 -->
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>config/sun_checks.xml</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId> <!-- test 結果報表 -->
<artifactId>surefire-report-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId> <!-- 源代碼的HTML化 -->
<artifactId>jxr-maven-plugin</artifactId>
</plugin>
在maven的官方網站有可以找到更多的report plugin
最后一個比較完整的pom.xml看起來像這樣:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<name>CutoverModule</name>
<groupId>ctzj_batch</groupId>
<artifactId>eai_cutover</artifactId>
<url>http://134.98.83.137:8081/scarab/issues</url>
<inceptionYear>2006</inceptionYear>
<organization>
<name>CTZJ BSS</name>
<url>http://www.zjhcsoft.com</url>
</organization>
<version>1.0</version>
<description>97 cutover batch module:invoke eai-adapter to update the phone number or value of
c4/c5</description>
<developers>
<developer>
<name>Chen Lin</name>
<organization>ZJHCSoft</organization>
<email>chenlin@zjhcsoft.com</email>
</developer>
</developers>
<mailingLists>
<mailingList>
<name>${pom.name} Dev List</name>
</mailingList>
</mailingLists>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<outputDirectory>target\classes</outputDirectory>
<testOutputDirectory>target\test-classes</testOutputDirectory>
<directory>target</directory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.1.2</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</build>
<ciManagement>
<system>continuum</system>
<url>http://localhost:8080/continuum/servlet/continuum</url>
</ciManagement>
<scm>
<connection>scm:local|E:/eclipseworkshop|EAIBatch</connection>
</scm>
<dependencies>
<dependency>
<groupId>ctzj_batch</groupId>
<artifactId>Eaiadapter</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ctzj_batch</groupId>
<artifactId>ta</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ctzj_batch</groupId>
<artifactId>grnds-common</artifactId>
<version>4.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>concurrent</groupId>
<artifactId>concurrent</artifactId>
<version>1.3.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<reporting>
<outputDirectory>target/site</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>config/sun_checks.xml</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>surefire-report-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jxr-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>