淺談Maven中的POM.XML

我們先看一個簡單的例子:

複製代碼
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3     <!-- maven model version -->
 4     <modelVersion>4.0.0</modelVersion>
 5     <!-- project group id & artifact id -->
 6     <groupId>com.freesoft.mvn-webapp</groupId>
 7     <artifactId>mvnwebapp</artifactId>
 8     <!-- packing type -->
 9     <packaging>war</packaging>
10     <!-- version -->
11     <version>1.0-SNAPSHOT</version>
12     <name>mvnwebapp Maven Webapp</name>
13     <url>http://maven.apache.org</url>
14 
15 
16     <dependencies>
17 
18         <!-- JUnit -->
19         <dependency>
20             <groupId>junit</groupId>
21             <artifactId>junit</artifactId>
22             <version>4.11</version>
23             <scope>test</scope>
24         </dependency>
25 
26     </dependencies>
27 
28 
29     <build>
30         <finalName>mvnwebapp</finalName>
31         <pluginManagement>
32             <plugins>
33                 <plugin>
34                     <groupId>org.apache.tomcat.maven</groupId>
35                     <artifactId>tomcat7-maven-plugin</artifactId>
36                     <version>2.1</version>
37                     <configuration>
38                         <tomcat-url>http://localhost:8080/manager/html</tomcat-url>
39                         <server>tomcat_localtest</server>
40                     </configuration>
41                 </plugin>
42             </plugins>
43         </pluginManagement>
44     </build>
45 
46     <properties>
47         <struts.version>2.3.15</struts.version>
48         <mysql.version>5.1.29</mysql.version>
49         <hibernate.version>4.3.1.Final</hibernate.version>
50     </properties>
51 </project>
複製代碼

 

下面分段講解。


1. 基本信息

 

modelVersion Maven模塊版本,目前我們一般都取值4.0.0
groupId 整個系統的名稱。
artifactId 子模塊名稱。
packaging 打包類型,可取值:jar,war等等,這個配置用於package的phase,具體可以參見package運行的時候啓動的plugin,後面有機會我們會講述如何配置打包的插件。

 

2. dependencies

依賴關係。實際上pom之間存在好三種關係:繼承、依賴、聚合。我們先講依賴,這也是最重要的關係。

 

groupId 依賴項的groupId
artifactId 依賴項的artifactId
version 依賴項的版本
scope 依賴項的適用範圍:
  • compile,缺省值,適用於所有階段,會隨着項目一起發佈。
  • provided,類似compile,期望JDK、容器或使用者會提供這個依賴。如servlet.jar。
  • runtime,只在運行時使用,如JDBC驅動,適用運行和測試階段。
  • test,只在測試時使用,用於編譯和運行測試代碼。不會隨項目發佈。
  • system,類似provided,需要顯式提供包含依賴的jar,Maven不會在Repository中查找它。
之前例子裏的junit就只用在了test中。
exclusions 排除項目中的依賴衝突時使用。

 

2.1 關於排除依賴衝突

我們可能經常會遇到這樣一個問題:我們的項目有兩個依賴項:A & B,而且A和B同時依賴了C,但不是同一個版本。那麼我們怎麼辦呢?


2.1.1 添加檢查插件

複製代碼
1 <reporting>
2         <plugins>
3             <plugin>
4                 <groupId>org.apache.maven.plugins</groupId>
5                 <artifactId>maven-project-info-reports-plugin</artifactId>
6             </plugin>
7         </plugins>
8     </reporting>
複製代碼


然後運行:mvn project-info-reports:dependencies,來查看依賴項報告。


2.1.2 去除依賴項

如果我們需要在某個dependency中去除某個依賴項,直接這樣即可:

複製代碼
 1 <!-- Struts2 -->
 2         <dependency>
 3             <groupId>org.apache.struts</groupId>
 4             <artifactId>struts2-core</artifactId>
 5             <version>${struts.version}</version>
 6             <exclusions>
 7                 <exclusion>
 8                     <groupId>org.freemarker</groupId>
 9                     <artifactId>freemarker</artifactId>
10                 </exclusion>
11             </exclusions>
12         </dependency>
複製代碼

3. 繼承

我的repository下面有個例子就直接拿來用了:

複製代碼
1 <modelVersion>4.0.0</modelVersion>
2   <parent>
3     <groupId>com.thoughtworks.xstream</groupId>
4     <artifactId>xstream-parent</artifactId>
5     <version>1.4.3</version>
6   </parent>
7   <artifactId>xstream</artifactId>
8   <packaging>jar</packaging>
9   <name>XStream Core</name>
複製代碼

其中的parent表示父pom是com.thoughtworks.xstream的xstream-parent的1.4.3版本。繼承關係比較簡單,這裏不做過多介紹。


4. 聚合

我們可以通過一個大的項目來整合各個小的模塊:

1 <modules>
2         <module>my-app</module>
3     </modules>

5. 屬性

屬性表述類似於EL表達式,ANT中也同樣有,所以我們的properties字段可以這樣使用:

1         <!-- mysql -->
2         <dependency>
3             <groupId>mysql</groupId>
4             <artifactId>mysql-connector-java</artifactId>
5             <version>${mysql.version}</version>
6         </dependency>

6. 構建

6.1 plugin

Plugin的配置如下:

複製代碼
 1 <pluginManagement>
 2             <plugins>
 3                 <plugin>
 4                     <groupId>org.apache.tomcat.maven</groupId>
 5                     <artifactId>tomcat7-maven-plugin</artifactId>
 6                     <version>2.1</version>
 7                     <configuration>
 8                         <tomcat-url>http://localhost:8080/manager/html</tomcat-url>
 9                         <server>tomcat_localtest</server>
10                     </configuration>
11                 </plugin>
12             </plugins>
13         </pluginManagement>
複製代碼

我們可以看到同樣要喲偶groupId、artifactId、version還有一些配置參數。


6.2 resource

指定你在Build時需要的資源文件:

複製代碼
 1 <resources>
 2             <resource>
 3                 <targetPath>WEB-INF/resource</targetPath>
 4                 <!-- 不對文件中的表達式進行處理 -->
 5                 <filtering>false</filtering>
 6                 <directory>${basedir}/src/test/resources</directory>
 7                 <includes>
 8                     <include>include.xml</include>
 9                 </includes>
10                 <excludes>
11                     <exclude>exclude.xml</exclude>
12                 </excludes>
13             </resource>
14         </resources>
複製代碼

 


 

0
0

<div class="postDesc">posted on <span id="post-date">2016-10-17 14:08</span> <a href="http://www.cnblogs.com/sharpest/">Sharpest</a> 閱讀(<span id="post_view_count">7342</span>) 評論(<span id="post_comment_count">0</span>)  <a href="https://i.cnblogs.com/EditPosts.aspx?postid=5969566" rel="nofollow">編輯</a> <a href="#" onclick="AddToWz(5969566);return false;">收藏</a></div>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章