Maven 学习笔记1: POM文件的介绍

匿名 (未验证) 提交于 2019-12-03 00:17:01

本文节选翻译自apache的Maven入门教程Introduction to the POM

什么是POM

POM:Project Object Model. POM文件是Maven工程的最核心的文件。POM文件的格式是xml文件,它包含了项目的很多信息,Maven使用POM文件提供的配置信息来构建项目。Maven提供了一些默认配置:

POM提供的默认配置
含义 默认名称
build directory  target
source directory src/main/java
test source directory src/test/java

用户可以配置的Maven选项:

project dependencies(依赖), the plugins, goals, build profiles等

另外还有 project versions, description, developers, mailing lists也可以被用户指定。

Super POM

Super POM是Maven的默认POM文件。除非用户显式设定,否则所有的POM文件都会继承Super POM文件中的内容。

Super POM 文件内容

  1 <project>   2   <modelVersion>4.0.0</modelVersion>   3     4   <repositories>   5     <repository>   6       <id>central</id>   7       <name>Central Repository</name>   8       <url>https://repo.maven.apache.org/maven2</url>   9       <layout>default</layout>  10       <snapshots>  11         <enabled>false</enabled>  12       </snapshots>  13     </repository>  14   </repositories>  15    16   <pluginRepositories>  17     <pluginRepository>  18       <id>central</id>  19       <name>Central Repository</name>  20       <url>https://repo.maven.apache.org/maven2</url>  21       <layout>default</layout>  22       <snapshots>  23         <enabled>false</enabled>  24       </snapshots>  25       <releases>  26         <updatePolicy>never</updatePolicy>  27       </releases>  28     </pluginRepository>  29   </pluginRepositories>  30    31   <build>  32     <directory>${project.basedir}/target</directory>  33     <outputDirectory>${project.build.directory}/classes</outputDirectory>  34     <finalName>${project.artifactId}-${project.version}</finalName>  35     <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>  36     <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>  37     <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>  38     <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>  39     <resources>  40       <resource>  41         <directory>${project.basedir}/src/main/resources</directory>  42       </resource>  43     </resources>  44     <testResources>  45       <testResource>  46         <directory>${project.basedir}/src/test/resources</directory>  47       </testResource>  48     </testResources>  49     <pluginManagement>  50       <!-- NOTE: These plugins will be removed from future versions of the super POM -->  51       <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->  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-5</version>  60         </plugin>  61         <plugin>  62           <artifactId>maven-dependency-plugin</artifactId>  63           <version>2.8</version>  64         </plugin>  65         <plugin>  66           <artifactId>maven-release-plugin</artifactId>  67           <version>2.5.3</version>  68         </plugin>  69       </plugins>  70     </pluginManagement>  71   </build>  72    73   <reporting>  74     <outputDirectory>${project.build.directory}/site</outputDirectory>  75   </reporting>  76    77   <profiles>  78     <!-- NOTE: The release profile will be removed from future versions of the super POM -->  79     <profile>  80       <id>release-profile</id>  81    82       <activation>  83         <property>  84           <name>performRelease</name>  85           <value>true</value>  86         </property>  87       </activation>  88    89       <build>  90         <plugins>  91           <plugin>  92             <inherited>true</inherited>  93             <artifactId>maven-source-plugin</artifactId>  94             <executions>  95               <execution>  96                 <id>attach-sources</id>  97                 <goals>  98                   <goal>jar-no-fork</goal>  99                 </goals> 100               </execution> 101             </executions> 102           </plugin> 103           <plugin> 104             <inherited>true</inherited> 105             <artifactId>maven-javadoc-plugin</artifactId> 106             <executions> 107               <execution> 108                 <id>attach-javadocs</id> 109                 <goals> 110                   <goal>jar</goal> 111                 </goals> 112               </execution> 113             </executions> 114           </plugin> 115           <plugin> 116             <inherited>true</inherited> 117             <artifactId>maven-deploy-plugin</artifactId> 118             <configuration> 119               <updateReleaseInfo>true</updateReleaseInfo> 120             </configuration> 121           </plugin> 122         </plugins> 123       </build> 124     </profile> 125   </profiles> 126   127 </project>

Minimal POM

最小POM文件,一个POM文件最少要包含如下信息:

  • project root
  • modelVersion - 目前应该设置为4.0.0
  • groupId - 项目组id
  • artifactId - 项目id
  • version - 版本号

最小POM文件 example:

1 <project> 2   <modelVersion>4.0.0</modelVersion> 3   <groupId>com.mycompany.app</groupId> 4   <artifactId>my-app</artifactId> 5   <version>1</version> 6 </project>

一个POM文件需要配置其groupId, artifactId和version。 这三个值构成该项目唯一合法的artifact name(或者说 坐标)。其形式是:<groudId>:<artifactId>:<version>。以上面的最小POM文件为例,其合法的fully artifact name是 "com.mycompany.app:my-app:1"。

除了上述最小POM文件要求提供的信息外,其他的配置信息如果不提供,则Maven会采用默认配置。如项目的打包类型信息可以被 packaging type 指定。每个项目都有打包类型,一般可以指定的类型有pom, jar, war等。如果不指定,默认的打包类型是jar。

另外,最小POM文件中并未指定 repositorieshttp://repo.maven.apache.org/maven2 这个仓库下载,因为这个仓库被配置在Super POM中。

下面介绍如果使用Maven解决多项目合作的问题。如果一个大的项目下有多个子项目,则这些项目之间可以用继承(inheritance)或者融合(aggregation)的方式进行关联。

Project Inheritance

两个POM文件之间可以存在继承的关系,也就是说,一个POM文件可以复用(或者说两个POM文件之间可以合并)下列信息:

  • dependencies
  • developers and contributors
  • plugin lists (including reports)
  • plugin executions with matching ids
  • plugin configuration
  • resources

Example 1

场景

我们想在一个新项目中继承上面的这个POM, com.mycompany.app:my-app:1.我们现在有如下的一个新的项目 com.mycompany.app:my-module:1.

1 <project> 2   <modelVersion>4.0.0</modelVersion> 3   <groupId>com.mycompany.app</groupId> 4   <artifactId>my-module</artifactId> 5   <version>1</version> 6 </project>

这两个项目的目录结构如下:

1 . 2  |-- my-module 3  |   `-- pom.xml 4  `-- pom.xml

其中 my-module/pom.xmlmy-module:1的POM, 外层的pom.xml 是com.mycompany.app:my-app:1的POM

解决方法

如果我们想让com.mycompany.app:my-appmy-module:1 的parent artifact, 只需修改com.mycompany.app:my-module:1的POM文件为如下配置:

 1 <project>  2   <parent>  3     <groupId>com.mycompany.app</groupId>  4     <artifactId>my-app</artifactId>  5     <version>1</version>  6   </parent>  7   <modelVersion>4.0.0</modelVersion>  8   <groupId>com.mycompany.app</groupId>  9   <artifactId>my-module</artifactId> 10   <version>1</version> 11 </project>

我们在com.mycompany.app:my-module:1 中添加了parent标签域,指定了父项目的坐标三要素 groupId, artifactId, version。这样,my-module项目就可以继承父项目的一些properties.

此外,如果如果在子项目中省略坐标中的groupId 和 version, 则子项目会使用和父项目一样的groupId 和 version。 但子项目必须指定artifactId。

如下配置com.mycompany.app:my-module:1, 省略了子项目的groupId 和 version, 则my-module会复用parent标签中指定的父项目的groupId和version

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!