POM基本項目信息
groupId:項目或者組織的唯一標誌
artifactId:項目的通用名稱
version:項目的版本
packaging:打包機制,如pom,jar,maven-plugin,ejb,war,ear,rar,par
name:用戶描述項目的名稱,無關緊要的東西,可選
url:應該是隻是寫明開發團隊的網站,無關緊要,可選
POM依賴關係
依賴關係示例
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.china.shared</groupId>
<artifactId>alibaba.apollo.webx</artifactId>
<version>2.5.0</version>
<exclusions>
<exclusion>
<artifactId>org.slf4j.slf4j-api</artifactId>
<groupId>com.alibaba.external</groupId>
</exclusion>
....
</exclusions>
......
</dependencies>
其中groupId, artifactId, version這三個組合標示依賴的具體工程,而且 這個依賴工程必需是maven中心包管理範圍內的。
dependency裏屬性介紹
type:默認爲jar類型,常用的類型有:jar,ejb-client,test-jar...,可設置plugins中的extensions值爲true後在增加 新的類型,
scope:是用來指定當前包的依賴範圍,maven的依賴範圍
optional:設置指依賴是否可選,默認爲false,即子項目默認都繼承,爲true,則子項目必需顯示的引入,與dependencyManagement裏定義的依賴類似 。
exclusions:如果X需要A,A包含B依賴,那麼X可以聲明不要B依賴,只要在exclusions中聲明exclusion.
exclusion:是將B從依賴樹中刪除,如上配置,alibaba.apollo.webx不想使用com.alibaba.external ,但是alibaba.apollo.webx是集成了com.alibaba.external,所以就需要排除掉.
parent的使用方法
如果一個工程是parent或者aggregation(即mutil-module的)的,那麼必須在packing賦值爲pom,child工程從parent繼承的包括:dependencies,developers,contributors,plugin lists,reports lists,plugin execution with matching ids,plugin configuration
parent的使用方法如下:
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
relativePath是可選的,maven會首先搜索這個地址,在搜索本地遠程repositories之前.
dependencyManagement使用方法
dependencyManagement:是用於幫助管理chidren的dependencies的。例如如果parent使用dependencyManagement定義了一個dependencyon junit:junit4.0,那麼 它的children就可以只引用 groupId和artifactId,而version就可以通過parent來設置,這樣的好處就是可以集中管理 依賴的詳情
modules:對於多模塊的project,outer-module沒有必需考慮inner-module的dependencies,當列出modules的時候,modules的順序是不重要的,因爲maven會自動根據依賴關係來拓撲排序,
modules例子如下 :
<module>my-project</module>
<module>other-project</module>
properties信息
properties:是爲pom定義一些常量,在pom中的其它地方可以直接引用。
定義方式如下:
<properties>
<file.encoding>UTF-8</file_encoding>
<java.source.version>1.5</java_source_version>
<java.target.version>1.5</java_target_version>
</properties>
使用方式 如下 :
${file.encoding}
build屬性設置
常用屬性設置
defaultGoal:默認的目標,必須跟命令行上的參數相同,如:jar:jar,或者與時期parse相同,例如install
directory:指定build target目標的目錄,默認爲$(basedir}/target,即項目根目錄下的target
finalName:指定去掉後綴的工程名字,例如:默認爲${artifactId}-${version}
filters:用於定義指定filter屬性的位置,例如filter元素賦值filters/filter1.properties,那麼這個文件裏面就可以定義name=value對,這個name=value對的值就可以在工程pom中通過${name}引用,默認的filter目錄是${basedir}/src/main/fiters/
resources:描述工程中資源的位置
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
targetPath:指定build資源到哪個目錄,默認是base directory
filtering:指定是否將filter文件(即上面說的filters裏定義的*.property文件)的變量值在這個resource文件有效,例如上面就指定那些變量值在configuration文件無效。
directory:指定屬性文件的目錄,build的過程需要找到它,並且將其放到targetPath下,默認的directory是${basedir}/src/main/resources
includes:指定包含文件的patterns,符合樣式並且在directory目錄下的文件將會包含進project的資源文件。
excludes:指定不包含在內的patterns,如果inclues與excludes有衝突,那麼excludes勝利,那些符合衝突的樣式的文件是不會包含進來的。
testResources:這個模塊包含測試資源元素,其內容定義與resources類似,不同的一點是默認的測試資源路徑是${basedir}/src/test/resources,測試資源是不部署的。
plugins配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.0</version>
<extensions>false</extensions>
<inherited>true</inherited>
<configuration>
<classifier>test</classifier>
</configuration>
<dependencies>...</dependencies>
<executions>...</executions>
</plugin>
extensions:true or false, 決定是否要load這個plugin的extensions,默認爲true.
inherited:是否讓子pom繼承,ture or false 默認爲true.
configuration:通常用於私有不開源的plugin,不能夠詳細瞭解plugin的內部工作原理,但使plugin滿足的properties
dependencies:與pom基礎的dependencies的結構和功能都相同,只是plugin的dependencies用於plugin,而pom的denpendencies用於項目本身。在plugin的dependencies主要用於改變plugin原來的dependencies,例如排除一些用不到的dependency或者修改dependency的版本等,詳細請看pom的denpendencies.
executions:plugin也有很多個目標,每個目標具有不同的配置,executions就是設定plugin的目標,
<execution>
<id>echodir</id>
<goals>
<goal>run</goal>
</goals>
<phase>verify</phase>
<inherited>false</inherited>
<configuration>
<tasks>
<echo>Build Dir: ${project.build.directory}</echo>
</tasks>
</configuration>
</execution>
id:標識符
goals:裏面列出一系列的goals元素,例如上面的run goal
phase:聲明goals執行的時期,例如:verify
inherited:是否傳遞execution到子pom裏。
configuration:設置execution下列表的goals的設置,而不是plugin所有的goals的設置.
倉庫設置
Repositories:pom裏面的倉庫與setting.xml裏的倉庫功能是一樣的。主要的區別在於,pom裏的倉庫是個性化的。
<repositories>
<repository>
<id>public</id>
<name>public</name>
<url>http://172.16.0.193:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
組件上傳遠程
<!-- 配置部署的遠程倉庫 -->
<distributionManagement>
<snapshotRepository>
<id>user-snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://172.16.0.193:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>user-release</id>
<name>User Project Release</name>
<url>http://172.16.0.193:8081/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
對應setting.xml配置
下節更精彩!可以看我首頁進羣你學習!!