《Maven官方文档》POM文件(一)
什么是POM?
POM(project object model)包含了工程信息和工程的配置細(xì)節(jié),Maven使用POM文件來構(gòu)建工程。POM文件包含了工程中的大部分默認(rèn)值。舉個例子,target是默認(rèn)的構(gòu)建目錄,src/main/java是默認(rèn)的源碼目錄,src/test/java是默認(rèn)的測試源碼目錄,等等。
Maven2中的pom.xml就是Maven1中的project.xml。相比于在maven.xml中包含可執(zhí)行的goal,現(xiàn)在goals和plugins都可以在pom.xml中配置。當(dāng)執(zhí)行一個task或者goal時,Maven會在當(dāng)前目錄下尋找并讀取pom.xml來獲取配置信息,然后執(zhí)行g(shù)oal。
能在pom.xml中聲明的配置包括工程依賴(project dependencies),插件(plugins),可執(zhí)行的目標(biāo)(goals),構(gòu)建配置(build profiles)等等。其他信息,比如工程版本,描述,開發(fā)者,郵件列表等等也可以在pox.xml中聲明。
Super POM
Super POM是Maven的默認(rèn)POM文件,除非你顯示的聲明繼承關(guān)系,否則所有的POM文件都是在Super POM的基礎(chǔ)上的擴展,也就是說,Super POM中的的配置會被你的工程中創(chuàng)建的pom.xml繼承。Maven 2.0.x中的Super POM代碼如下:
<project><modelVersion>4.0.0</modelVersion><name>Maven Default Project</name><repositories><repository><id>central</id><name>Maven Repository Switchboard</name><layout>default</layout><url>http://repo1.maven.org/maven2</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>central</id><name>Maven Plugin Repository</name><url>http://repo1.maven.org/maven2</url><layout>default</layout><snapshots><enabled>false</enabled></snapshots><releases><updatePolicy>never</updatePolicy></releases></pluginRepository></pluginRepositories><build><directory>target</directory><outputDirectory>target/classes</outputDirectory><finalName>${artifactId}-${version}</finalName><testOutputDirectory>target/test-classes</testOutputDirectory><sourceDirectory>src/main/java</sourceDirectory><scriptSourceDirectory>src/main/scripts</scriptSourceDirectory><testSourceDirectory>src/test/java</testSourceDirectory><resources><resource><directory>src/main/resources</directory></resource></resources><testResources><testResource><directory>src/test/resources</directory></testResource></testResources></build><reporting><outputDirectory>target/site</outputDirectory></reporting><profiles><profile><id>release-profile</id><activation><property><name>performRelease</name></property></activation><build><plugins><plugin><inherited>true</inherited><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><executions><execution><id>attach-javadocs</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><groupId>org.apache.maven.plugins</groupId><artifactId>maven-deploy-plugin</artifactId><configuration><updateReleaseInfo>true</updateReleaseInfo></configuration></plugin></plugins></build></profile></profiles></project>下面是Maven 2.1.x的Super POM:
<project><modelVersion>4.0.0</modelVersion><name>Maven Default Project</name><repositories><repository><id>central</id><name>Maven Repository Switchboard</name><layout>default</layout><url>http://repo1.maven.org/maven2</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>central</id><name>Maven Plugin Repository</name><url>http://repo1.maven.org/maven2</url><layout>default</layout><snapshots><enabled>false</enabled></snapshots><releases><updatePolicy>never</updatePolicy></releases></pluginRepository></pluginRepositories><build><directory>${project.basedir}/target</directory><outputDirectory>${project.build.directory}/classes</outputDirectory><finalName>${project.artifactId}-${project.version}</finalName><testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory><sourceDirectory>${project.basedir}/src/main/java</sourceDirectory><!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... --><scriptSourceDirectory>src/main/scripts</scriptSourceDirectory><testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory><resources><resource><directory>${project.basedir}/src/main/resources</directory></resource></resources><testResources><testResource><directory>${project.basedir}/src/test/resources</directory></testResource></testResources><pluginManagement><plugins><plugin><artifactId>maven-antrun-plugin</artifactId><version>1.3</version></plugin> <plugin><artifactId>maven-assembly-plugin</artifactId><version>2.2-beta-2</version></plugin> <plugin><artifactId>maven-clean-plugin</artifactId><version>2.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version></plugin><plugin><artifactId>maven-dependency-plugin</artifactId><version>2.0</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.4</version></plugin><plugin><artifactId>maven-ear-plugin</artifactId><version>2.3.1</version></plugin><plugin><artifactId>maven-ejb-plugin</artifactId><version>2.1</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.2</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>2.2</version></plugin><plugin><artifactId>maven-javadoc-plugin</artifactId><version>2.5</version></plugin><plugin><artifactId>maven-plugin-plugin</artifactId><version>2.4.3</version></plugin><plugin><artifactId>maven-rar-plugin</artifactId><version>2.2</version></plugin> <plugin> <artifactId>maven-release-plugin</artifactId><version>2.0-beta-8</version></plugin><plugin> <artifactId>maven-resources-plugin</artifactId><version>2.3</version></plugin><plugin><artifactId>maven-site-plugin</artifactId><version>2.0-beta-7</version></plugin><plugin><artifactId>maven-source-plugin</artifactId><version>2.0.4</version></plugin> <plugin><artifactId>maven-surefire-plugin</artifactId><version>2.4.3</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>2.1-alpha-2</version></plugin></plugins></pluginManagement></build><reporting><outputDirectory>${project.build.directory}/site</outputDirectory></reporting><profiles><profile><id>release-profile</id><activation><property><name>performRelease</name><value>true</value></property></activation><build><plugins><plugin><inherited>true</inherited><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><executions><execution><id>attach-javadocs</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><groupId>org.apache.maven.plugins</groupId><artifactId>maven-deploy-plugin</artifactId><configuration><updateReleaseInfo>true</updateReleaseInfo></configuration></plugin></plugins></build></profile></profiles></project>最小化POM
一個最小化的POM文件的要求如下:
- project 標(biāo)簽作為頂層標(biāo)簽
- modelVersion – 應(yīng)該設(shè)為4.0.0
- groupId – 工程開發(fā)組的唯一id
- artifactId – 工件(artifact)或工程(projrct)的唯一id
- version – 版本號
這里是一個最小化POM的例子:
<project><modelVersion>4.0.0</modelVersion><groupId>com.mycompany.app</groupId><artifactId>my-app</artifactId><version>1</version> </project>在POM文件中需要聲明 groupId , ?artifactId 和 version 。這三個值以<groupId>:<artifactId>:<version>的形式聲明,它們組成了工程的完整名稱。比如上面的例子,完整名稱為”com.mycompany.app:my-app1″。
如第一節(jié)所說,如果配置細(xì)節(jié)沒有顯示的設(shè)置,Maven將會使用繼承自Super POM默認(rèn)配置。其中一個默認(rèn)值就是包的類型(packaging type),每個Maven工程都有一個包類型,如果沒有在POM中設(shè)置,則默認(rèn)為”jar”。
前面的Minimal POM中,其中?repositories?這個值沒有設(shè)置,如果你使用minimal POM來構(gòu)建你的工程,它將使用繼承Super POM中的?repositories?值(http://repo.maven.apache.org/maven2),當(dāng)Maven在POM中找到依賴,它就會去這個地址下載依賴包。
轉(zhuǎn)載自?并發(fā)編程網(wǎng) - ifeve.com
總結(jié)
以上是生活随笔為你收集整理的《Maven官方文档》POM文件(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7系统下升级IE11
- 下一篇: 除醛重要性美博士环保为您解答!!