Apache Maven(三):POM

什么是 POM?

POM (Project Object Model) 项目对象模型。它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置细节的信息。它包含大多数项目的默认值。例如,构建项目的目录:target;java源码文件目录: src/main/java;测试java源码文件目录: src/test/java;等等。当执行任务或目标时,Maven将在当前目录中查找POM,它读取POM。获取所需要的配置信息,然后执行目标。

POM中可以指定项目的依赖,可以执行的插件或目标,构建配置文件等。其他信息,如项目版本,说明,开发人员,邮件列表等也可以指定。

Super POM

Super POM 是Maven默认的POM,除非明确设置,否则所有的POM都会扩展Super POM,这意味着您为项目创建的POM继承Super POM。下面的代码是Maven 2.0.x 的 Super POM。

  1 <project>
  2   <modelVersion>4.0.0</modelVersion>
  3   <name>Maven Default Project</name>
  4
  5   <repositories>
  6     <repository>
  7       <id>central</id>
  8       <name>Maven Repository Switchboard</name>
  9       <layout>default</layout>
 10       <url>http://repo1.maven.org/maven2</url>
 11       <snapshots>
 12         <enabled>false</enabled>
 13       </snapshots>
 14     </repository>
 15   </repositories>
 16
 17   <pluginRepositories>
 18     <pluginRepository>
 19       <id>central</id>
 20       <name>Maven Plugin Repository</name>
 21       <url>http://repo1.maven.org/maven2</url>
 22       <layout>default</layout>
 23       <snapshots>
 24         <enabled>false</enabled>
 25       </snapshots>
 26       <releases>
 27         <updatePolicy>never</updatePolicy>
 28       </releases>
 29     </pluginRepository>
 30   </pluginRepositories>
 31
 32   <build>
 33     <directory>target</directory>
 34     <outputDirectory>target/classes</outputDirectory>
 35     <finalName>${artifactId}-${version}</finalName>
 36     <testOutputDirectory>target/test-classes</testOutputDirectory>
 37     <sourceDirectory>src/main/java</sourceDirectory>
 38     <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
 39     <testSourceDirectory>src/test/java</testSourceDirectory>
 40     <resources>
 41       <resource>
 42         <directory>src/main/resources</directory>
 43       </resource>
 44     </resources>
 45     <testResources>
 46       <testResource>
 47         <directory>src/test/resources</directory>
 48       </testResource>
 49     </testResources>
 50   </build>
 51
 52   <reporting>
 53     <outputDirectory>target/site</outputDirectory>
 54   </reporting>
 55
 56   <profiles>
 57     <profile>
 58       <id>release-profile</id>
 59
 60       <activation>
 61         <property>
 62           <name>performRelease</name>
 63         </property>
 64       </activation>
 65
 66       <build>
 67         <plugins>
 68           <plugin>
 69             <inherited>true</inherited>
 70             <groupId>org.apache.maven.plugins</groupId>
 71             <artifactId>maven-source-plugin</artifactId>
 72
 73             <executions>
 74               <execution>
 75                 <id>attach-sources</id>
 76                 <goals>
 77                   <goal>jar</goal>
 78                 </goals>
 79               </execution>
 80             </executions>
 81           </plugin>
 82           <plugin>
 83             <inherited>true</inherited>
 84             <groupId>org.apache.maven.plugins</groupId>
 85             <artifactId>maven-javadoc-plugin</artifactId>
 86
 87             <executions>
 88               <execution>
 89                 <id>attach-javadocs</id>
 90                 <goals>
 91                   <goal>jar</goal>
 92                 </goals>
 93               </execution>
 94             </executions>
 95           </plugin>
 96           <plugin>
 97             <inherited>true</inherited>
 98             <groupId>org.apache.maven.plugins</groupId>
 99             <artifactId>maven-deploy-plugin</artifactId>
100
101             <configuration>
102               <updateReleaseInfo>true</updateReleaseInfo>
103             </configuration>
104           </plugin>
105         </plugins>
106       </build>
107     </profile>
108   </profiles>
109
110 </project>

下面是Maven 2.1.x 的Super POM

  1 <project>
  2   <modelVersion>4.0.0</modelVersion>
  3   <name>Maven Default Project</name>
  4
  5   <repositories>
  6     <repository>
  7       <id>central</id>
  8       <name>Maven Repository Switchboard</name>
  9       <layout>default</layout>
 10       <url>http://repo1.maven.org/maven2</url>
 11       <snapshots>
 12         <enabled>false</enabled>
 13       </snapshots>
 14     </repository>
 15   </repositories>
 16
 17   <pluginRepositories>
 18     <pluginRepository>
 19       <id>central</id>
 20       <name>Maven Plugin Repository</name>
 21       <url>http://repo1.maven.org/maven2</url>
 22       <layout>default</layout>
 23       <snapshots>
 24         <enabled>false</enabled>
 25       </snapshots>
 26       <releases>
 27         <updatePolicy>never</updatePolicy>
 28       </releases>
 29     </pluginRepository>
 30   </pluginRepositories>
 31
 32   <build>
 33     <directory>${project.basedir}/target</directory>
 34     <outputDirectory>${project.build.directory}/classes</outputDirectory>
 35     <finalName>${project.artifactId}-${project.version}</finalName>
 36     <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
 37     <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
 38     <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
 39     <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
 40     <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
 41     <resources>
 42       <resource>
 43         <directory>${project.basedir}/src/main/resources</directory>
 44       </resource>
 45     </resources>
 46     <testResources>
 47       <testResource>
 48         <directory>${project.basedir}/src/test/resources</directory>
 49       </testResource>
 50     </testResources>
 51    <pluginManagement>
 52        <plugins>
 53          <plugin>
 54            <artifactId>maven-antrun-plugin</artifactId>
 55            <version>1.3</version>
 56          </plugin>
 57          <plugin>
 58            <artifactId>maven-assembly-plugin</artifactId>
 59            <version>2.2-beta-2</version>
 60          </plugin>
 61          <plugin>
 62            <artifactId>maven-clean-plugin</artifactId>
 63            <version>2.2</version>
 64          </plugin>
 65          <plugin>
 66            <artifactId>maven-compiler-plugin</artifactId>
 67            <version>2.0.2</version>
 68          </plugin>
 69          <plugin>
 70            <artifactId>maven-dependency-plugin</artifactId>
 71            <version>2.0</version>
 72          </plugin>
 73          <plugin>
 74            <artifactId>maven-deploy-plugin</artifactId>
 75            <version>2.4</version>
 76          </plugin>
 77          <plugin>
 78            <artifactId>maven-ear-plugin</artifactId>
 79            <version>2.3.1</version>
 80          </plugin>
 81          <plugin>
 82            <artifactId>maven-ejb-plugin</artifactId>
 83            <version>2.1</version>
 84          </plugin>
 85          <plugin>
 86            <artifactId>maven-install-plugin</artifactId>
 87            <version>2.2</version>
 88          </plugin>
 89          <plugin>
 90            <artifactId>maven-jar-plugin</artifactId>
 91            <version>2.2</version>
 92          </plugin>
 93          <plugin>
 94            <artifactId>maven-javadoc-plugin</artifactId>
 95            <version>2.5</version>
 96          </plugin>
 97          <plugin>
 98            <artifactId>maven-plugin-plugin</artifactId>
 99            <version>2.4.3</version>
100          </plugin>
101          <plugin>
102            <artifactId>maven-rar-plugin</artifactId>
103            <version>2.2</version>
104          </plugin>
105          <plugin>
106            <artifactId>maven-release-plugin</artifactId>
107            <version>2.0-beta-8</version>
108          </plugin>
109          <plugin>
110            <artifactId>maven-resources-plugin</artifactId>
111            <version>2.3</version>
112          </plugin>
113          <plugin>
114            <artifactId>maven-site-plugin</artifactId>
115            <version>2.0-beta-7</version>
116          </plugin>
117          <plugin>
118            <artifactId>maven-source-plugin</artifactId>
119            <version>2.0.4</version>
120          </plugin>
121          <plugin>
122             <artifactId>maven-surefire-plugin</artifactId>
123             <version>2.4.3</version>
124          </plugin>
125          <plugin>
126            <artifactId>maven-war-plugin</artifactId>
127            <version>2.1-alpha-2</version>
128          </plugin>
129        </plugins>
130      </pluginManagement>
131   </build>
132
133   <reporting>
134     <outputDirectory>${project.build.directory}/site</outputDirectory>
135   </reporting>
136   <profiles>
137     <profile>
138       <id>release-profile</id>
139
140       <activation>
141         <property>
142           <name>performRelease</name>
143           <value>true</value>
144         </property>
145       </activation>
146
147       <build>
148         <plugins>
149           <plugin>
150             <inherited>true</inherited>
151             <groupId>org.apache.maven.plugins</groupId>
152             <artifactId>maven-source-plugin</artifactId>
153             <executions>
154               <execution>
155                 <id>attach-sources</id>
156                 <goals>
157                   <goal>jar</goal>
158                 </goals>
159               </execution>
160             </executions>
161           </plugin>
162           <plugin>
163             <inherited>true</inherited>
164             <groupId>org.apache.maven.plugins</groupId>
165             <artifactId>maven-javadoc-plugin</artifactId>
166             <executions>
167               <execution>
168                 <id>attach-javadocs</id>
169                 <goals>
170                   <goal>jar</goal>
171                 </goals>
172               </execution>
173             </executions>
174           </plugin>
175           <plugin>
176             <inherited>true</inherited>
177             <groupId>org.apache.maven.plugins</groupId>
178             <artifactId>maven-deploy-plugin</artifactId>
179             <configuration>
180               <updateReleaseInfo>true</updateReleaseInfo>
181             </configuration>
182           </plugin>
183         </plugins>
184       </build>
185     </profile>
186   </profiles>
187
188 </project>

最小的 POM

POM最低要求如下:

  • project 根节点
  • modelVersion 需要设置为4.0.0
  • groupId 项目组的标识(建议使用网站的倒序)
  • artifactId 项目名称
  • version 项目的版本信息,这里提一下(SNAPSHOT 和 RELEASE),在项目中常常看到这两个单词。SNAPSHOT 代表不稳定版,常常属于开发中的项目,而RELEASE稳定版。

下面是已个最小POM的简单例子:

1 <project>
2    <modelVersion>4.0.0</modelVersion>
3    <groupId>com.cnblogs.hello</groupId>
4    <artifactId>hello</artifactId>
5    <version>1.0.SNAPSHOT</version>
6 </project>

POM要求配置groupId,artifactId 和 version,这三个值构成了项目完全限定的项目名称,就是<groupId>:<artifactId>:<version>,至于上面的例子,其完全限定的项目名称是"com.cnblogs.hello:hello:1.0.SNAPSHOP",其本地仓库对应的路径为:com/cnblogs/hello/hello/1.0.SNAPSHOT/hello-1.0.SNAPSHOT.jar

另外,如果配置文件未指定详细信息,Maven将使用默认值。每个Maven项目都有一个packaging类型。如果没有在POM中指定,那么就会使用默认值“jar”。

此外,正如你看到的,在最小POM中,repositories存储库也没有被指定,如果使用最小POM构建项目,它将继承Super POM中的repositories配置的存储库。

继承

项目继承会合并下列元素:

  • dependencies : 依赖
  • developers 和 contributors :  开发者和贡献者
  • plugin lists (including reports) : 插件列表(包括报表)
  • plugin executions with matching ids : 匹配ids的执行插件
  • plugin configuration : 插件配置
  • resources : 资源

Super POM是项目继承的一个例子,但是您也可以通过在POM中指定父元素来引入您自己的父POM,如以下示例所示。

 1 <project>
 2   <parent>
 3     <groupId>com.cnblogs.hello</groupId>
 4     <artifactId>hello</artifactId>
 5     <version>1.0.SNAPSHOT</version>
 6   </parent>
 7   <modelVersion>4.0.0</modelVersion>
 8   <groupId>com.cnbligs.hello</groupId>
 9   <artifactId>app</artifactId>
10   <version>1.0.SNAPSHOT</version>
11 </project>

上面示例是将最小POM指定为app的父POM。指定父POM我需要使用完全限定名指定(即需要使用groupId,artifactID 和 version),通过这个设置,我们的模块现在可以继承父POM的一些属性。

或者,如果我们希望groupId 和 version 和父POM的保持一致,则可以将在POM删除groupId和version,如下所示:

1 <project>
2   <parent>
3     <groupId>com.cnblogs.hello</groupId>
4     <artifactId>hello</artifactId>
5     <version>1.0.SNAPSHOT</version>
6   </parent>
7   <modelVersion>4.0.0</modelVersion>
8   <artifactId>app</artifactId>
9 </project>

这里注意下parent中的relativePath,如果父POM不是在该POM的上一级目录,就必须配置这个属性并定位到父POM中。relativePath的默认值为../pom.xml。

聚合

项目聚合跟项目继承有些类似,但不是从POM中指定父POM,而是从父POM中指定POM。通过这样做,父项目现在知道它所有的子项目,并且对父项目执行Maven命令,那么Maven命令也会执行到所有的子项目中。要做一个项目聚合,必须执行以下操作:

  • 改变父POM的packaging属性为pom。
  • 在父POM中指定子模块(子项目)。

将如上两个POM做如下修改。

com.cnblogs.hello:app:1.0.SNAPSHOT

1 <project>
2    <modelVersion>4.0.0</modelVersion>
3    <groupId>com.cnbligs.hello</groupId>
4    <artifactId>app</artifactId>
5    <version>1.0.SNAPSHOT</version>
6 </project>

com.cnblogs.hello:hello:1.0.SNAPSHOT

 1 <project>
 2    <modelVersion>4.0.0</modelVersion>
 3    <groupId>com.cnblogs.hello</groupId>
 4    <artifactId>hello</artifactId>
 5    <version>1.0.SNAPSHOT</version>
 6    <!-- 指定为父POM,project 聚合的第一步 -->
 7    <packaging>pom</packaging>
 8
 9    <!-- 添加子模块 根据POM的相对路径进行指定。 -->
10    <modules>
11        <module>../app</module>
12    </modules>
13 </project>

现在,每当Maven命令处理hello项目时,通过的Maven命令也会针对app项目运行。

继承 和 聚合

如果你有多个Maven项目,并且他们都具有相似的配置,则可以通过抽出相似的配置并制作父项目来构建。因此,您所要做的就是让你的Maven项目继承该父项目,然后将这些配置应用与所有的这些项目。

如果你有一组构建或一起处理的项目。你可以创建一个父项目并让该父项目将这些子项目声明进来。通过这样做,你只需要构建父项目,其余的也随之而来。

当然,你也可以同时拥有继承和聚合。意思是说,你可以让你的模块指定一个父项目,同时让这个父项目指定这些Maven作为它的子模块。你只需要符合以下三条规则:

  • 在每一个子POM中指定他们的父POM是谁。
  • 在父POM中将packaging的值改为pom。
  • 在父POM中指定其所有的子项目模块(子POM)。

参考一下代码示例:

com.cnblogs.hello:app:1.0.SNAPSHOT

 1 <project>
 2    <modelVersion>4.0.0</modelVersion>
 3    <groupId>com.cnblogs.hello</groupId>
 4    <artifactId>hello</artifactId>
 5    <version>1.0.SNAPSHOT</version>
 6    <packaging>pom</packaging>
 7
 8    <modules>
 9        <module>../app</module>
10    </modules>
11 </project>

com.cnblogs.hello:app:1.0.SNAPSHOT

 1 <project>
 2    <parent>
 3      <groupId>com.cnblogs.hello</groupId>
 4      <artifactId>hello</artifactId>
 5      <version>1.0.SNAPSHOT</version>
 6      <relativePath>../pom.xml</relativePath>
 7    </parent>
 8    <modelVersion>4.0.0</modelVersion>
 9    <groupId>com.cnbligs.hello</groupId>
10    <artifactId>app</artifactId>
11    <version>1.0.SNAPSHOT</version>
12 </project>

插入变量

在某些情况下,您需要在多个不同位置使用相同的值。为了帮助确保值仅指定一次,Maven允许您在POM中使用自己的和预定义的变量。例如如下示例:

<!-- 首先在properties标签中自定义 project.version 属性 值为 1.0.SANPSHOT  在后续的代码中可以直接使用${project.version}获取值 -->
<properties>
    <project.version>1.0.SNAPSHOT</project.version>
</properties>

<!-- version可以这样写 -->
<version>${project.version}</version>

其实properties标签不仅仅是只能声明版本号,凡是需要统一声明后再引用的场合都可以使用。

原文地址:https://www.cnblogs.com/peter1018/p/9204150.html

时间: 2024-08-01 18:22:05

Apache Maven(三):POM的相关文章

Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/ maven2 was cached in the local repository, resolution will not be reattempted until the update interv

Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/ maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Origi

Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval o

pom.xml报错: Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are fo

【原创】经验分享(10)Could not transfer artifact org.apache.maven:maven. from/to central. Received fatal alert: protocol_version

maven编译工程报错 [ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.2:add-source (scala-compile-first) on project trade: Execution scala-compile-first of goal net.alchim31.maven:scala-maven-plugin:3.2.2:add-source failed: Plugin net.

maven项目建立pom.xml报无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3

一.发现问题 建立maven项目后,pom.xml在显示红叉.鼠标放上去,显示Executiondefault-testResources of goalorg.apache.maven.plugins:maven-resources-plugin:2.4.3错误. 二.原因分析 缺少maven-resources-plugin-2.4.3.jar或该文件下载不对,可到repository\org\apache\maven\plugins\maven-resources-plugin\文件夹看看

maven报错 Failure to transfer org.apache.maven.plugins:maven-compiler-plugin:pom:3.5.0 from

maven报错误,类似于: Failure to transfer org.apache.maven.plugins:maven-compiler-plugin:pom:3.5.0 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elaps

报错: eclipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin:2.3.2

错误信息出现在pom头的project标签,project标签内容是 <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_

Maven的POM详解(三)

本文译自:http://maven.apache.org/pom.html 除外 除外是明确的告诉Maven,你不想包含指定的依赖工程中的依赖(即,不包含传递性依赖).例如,maven-embedder需要maven-core,但是你不希望使用它或它的依赖,那么就可以它作为一个除外项目来添加. <project xmlns="http://maven.apache.org/POM/4.0.0"   xmlns:xsi="http://www.w3.org/2001/XM

eclipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3解决方案

pom文件提示信息: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or upd

eclipse中pom.xml org.apache.maven.plugins报错的解决

作为一个热爱前端的同学,花大把时间做java真是无奈呀 = .= ,这个月学校组织了一次实训,因为没有前端相关的选项,只好选择了以前学过一段时间的java,于是乎SpringMVC现学现用,搞得不亦乐乎.实训之前一直持排斥态度,毕竟以后想找前端方面的工作,没成想还是学到了很多东西,尤其对前后端的数据交互有了更深入的理解(毕竟是自己和自己交互,汗). 好了,这次实训的总结等结束了再写,这一次先记录一下昨天在配置maven时走的一些弯路,昨晚问了度娘和谷歌都没能得到太好的解决,希望能给以后出现同样问