maven的pom介紹及配置(四)

1.什麼是pom?web

pom(Project Object Model,項目對象模型)定義了項目的基本信息,用於描述項目是如何構建,聲明項目依賴,插件配置,倉庫配置等等。spring

2.pom配置apache

Xml代碼maven

<project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemalocation="http://maven.apache.org/POM/4.0.0  
 4             http://maven.apache.org/xsd/maven-4.0.0.xsd">  
 5     <modelversion>4.0.0</modelversion>          
 6       
 7     <!-- 座標 -->  
	   <parent> ... </parent>  
 8     <groupid>...</groupid>   
 9     <artifactid>...</artifactid>  
10     <version>...</version>  
11     <packaging>...</packaging>  
	   <!-- 倉庫依賴 -->  
12     <dependencies>...</dependencies>  
14     <dependencymanagement>...</dependencymanagement>  
	   <!-- 項目模塊配置 -->  
15     <modules>...</modules>  
	   <!-- 全局配置文件 -->  
16     <properties>...</properties>  
17       
18     <!-- 構建過程的設置 -->  
19     <build>...</build>  
20     <reporting>...</reporting>  
21       
22     <!-- 項目信息設置 -->  
23     <name>...</name>  
24     <description>...</description>  
25     <url>...</url>  
26     <inceptionyear>...</inceptionyear>  
27     <licenses>...</licenses>  
28     <organization>...</organization>  
29     <developers>...</developers>  
30     <contributors>...</contributors>  
31       
32     <!-- 環境設置 -->  
33     <issuemanagement>...</issuemanagement>  
34     <cimanagement>...</cimanagement>  
35     <mailinglists>...</mailinglists>  
36     <scm>...</scm>  
37     <prerequisites>...</prerequisites>  
38     <repositories>...</repositories>  
39     <pluginrepositories>...</pluginrepositories>  
40     <distributionmanagement>...</distributionmanagement>  
41     <profiles>...</profiles>  
42 </project>

3.pom標籤詳解ide

3.1 項目座標標籤:測試

1.  <project xmlns="[http: maven.apache.org pom 4.0.0](http: 4.0.0)" 2. xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" 3. xsi:schemalocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)

4.  [http://maven.apache.org/maven-v4\_0\_0.xsd](http://maven.apache.org/maven-v4_0_0.xsd)">

5.  <modelversion>4.0.0</modelversion>

6.  <groupid>org.codehaus.mojo</groupid>

7.  <artifactid>my-project</artifactid>

8.  <version>1.0</version>

9.  <packaging>war</packaging>

10.  

11.  groupId : 組織標識,例如:org.codehaus.mojo,在M2_REPO目錄下,將是: org/codehaus/mojo目錄。

12.  artifactId : 項目名稱,例如:my-project,在M2_REPO目錄下,將是:org/codehaus/mojo/my-project目錄。

13.  version : 版本號,例如:1.0,在M2_REPO目錄下,將是:org/codehaus/mojo/my-project/1.0目錄。

14.  packaging : 打包的格式,能夠爲:pom , jar , maven-plugin , ejb , war , ear , rar , par

15.  modelVersion:定義pom版本號,版本號有一系列的規則

3.2 依賴標籤:ui

(依賴關係列表(dependency list)是POM的重要部分,也就是咱們項目對jar包的管理)url

Xml代碼.net

1.  <dependencies>

2.  <dependency>

3.  <groupid>junit</groupid>

4.  <artifactid>junit</artifactid>

5.  <version>4.0</version>

6.  <scope>test</scope>

7.  </dependency>

8.  …

9.  </dependencies>

10.  groupId , artifactId , version :引用的座標

11.  scope : compile(default),provided,runtime,test,system &nbsp;依賴的範圍

12.  exclusions &nbsp;須要排除的依賴的jar包

3.3 繼承和聚合(子pom對父pom依賴 和 父項目對模塊的依賴)插件

1.  <project xmlns="[http: maven.apache.org pom 4.0.0](http: 4.0.0)" 2. xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" 3. xsi:schemalocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)
4.  [http://maven.apache.org/maven-v4\_0\_0.xsd](http://maven.apache.org/maven-v4_0_0.xsd)">
5.  <modelversion>4.0.0</modelversion>
6.  <groupid>org.maven.my</groupid>
7.  <artifactid>${projectName}-parent</artifactid>
8.  <version>2.0</version>
9.  <!--\-\- 定義項目有哪些子模塊 ---->
10.  <modules>
11.  <module>my-spring-web<module>
12.  <module>my-spring-service<module>
13.  <module>my-spring-common<module>
14.  <module>my-spring-dao<module>
15.  </module></module></module></module></module></module></module></module></modules>
16.

 3.4 項目構建build時標籤:

(能夠幫咱們指定 須要的maven插件,主要標籤:Resources和Plugins

Resources:用於排除或包含某些資源文件

能夠用於解決 咱們部署測試和線上 服務時,資源文件配置的隔離依賴:-Ponline | -Plocal

Xml代碼

1.  <build>
2.  <!-- 開啓資源文件過濾 -->
3.  <resources>
4.  <resource>
5.  <directory>${project.basedir}/src/main/resources</directory>
6.  <filtering>true</filtering>
7.  </resource>
8.  </resources>
9.  </build>
10.  <!-- 指定資源文件路徑 -->
11.  <profiles>
12.  <!--測試配置 -->
13.  <profile>
14.  <id>local</id>
15.  <activation>
16.  <activebydefault>true</activebydefault>
17.  </activation>
18.  <build>
19.  <filters>
20.  <filter>${project.basedir}/src/main/swap/local.properties</filter>
21.  </filters>
22.  </build>
23.  </profile>
24.  <!-- 線上配置 -->
25.  <profile>
26.  <id>online</id>
27.  <activation>
28.  <activebydefault>false</activebydefault>
29.  </activation>
30.  <build>
31.  <filters>
32.  <filter>${project.basedir}/src/main/swap/online.properties</filter>
33.  </filters>
34.  </build>
35.  </profile>

Plugins:設置構建的插件

1.  <build>
2.  …
3.  <!--\-\- 配置maven在運行時 須要依賴的插件,咱們日常能夠配jetty插件或者assemebly插件等---->
4.  <plugins>
5.  <plugin>
6.  <groupid>org.apache.maven.plugins</groupid>
7.  <artifactid>maven-jar-plugin</artifactid>
8.  <version>2.0</version>
9.  <extensions>false</extensions>
10.  <inherited>true</inherited>
11.  <configuration>
12.  <classifier>test</classifier>
13.  </configuration>
14.  <dependencies>…</dependencies>
15.  <executions>…</executions>
16.  </plugin>

maven系列文章: 

  1).maven的安裝及經常使用命令(一)

  2).maven的生命週期,插件介紹(二)

  3).maven的座標,倉庫介紹及配置(三)

  4).maven的pom介紹及配置(四)

  5).maven的依賴特性,衝突解決(五)

  6).maven的聚合模塊和pom繼承使用(六)

  7).maven的版本規範(七)</plugins></build></profiles></project xmlns="[http:></project xmlns="[http:>

相關文章
相關標籤/搜索