- 参考链接:
https://maven.apache.org/pom.html
Overview
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
Basics
modelVersion
Notice that modelVersion contains 4.0.0. That is currently the only supported POM version for both Maven 2 & 3, and is always required.
groupId:artifactId:version
groupId:artifactId:version are all required fields for a minimum pom(although, groupId and version need not be explicitly defined if they are inherited from a parent - more on inheritance later).
packaging
When no packaging is declared, Maven assumes the packaging is the default: jar.
The current core packaging values are: pom, jar, maven-plugin, ejb, war, ear, rar. These define the default list of goals which execute to each corresponding build lifecycle stage for a particular package structure.
顾名思义,该元素决定了项目的打包方式。实际的情形中,如果你不声明该元素,Maven 会帮你生成一个 JAR 包;如果你定义该元素的值为 war,那你会得到一个 WAR 包;如果定义其值为 POM(比如是一个父模块),那什么包都不会生成。除此之外,Maven 默认还支持一些其他的流行打包格式,例如 ejb3 和 ear。你不需要了解具体的打包细节,你所需要做的就是告诉 Maven,”我是个什么类型的项目“,这就是约定优于配置的力量。
POM Relationships
Dependencies
Maven handling project relationships: this includes dependencies (and transitive dependencies), inheritance, and aggregation (multi-module projects).
There are times, unfortunately, when a project cannot be downloaded from the central Maven repository. For example, a project may depend upon a jar that has a closed-source license which prevents it from being in a central repository. There are three methods for dealing with this scenario:
1) Install the dependency locally using the install plugin. The method is the simplest recommended method. For example:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
Notice that an address is still required, only this time you use the command line and the install plugin will create a POM for you with the given address.
2) Create your own repository and deploy it there. This is a favorite method for companies with an intranet and need to be able to keep everyone in synch. There is a Maven goal called deploy:deploy-file which is similar to the install:install-file goal (read the plugin's goal page for more information).
3) Set the dependency scope to system and define a systemPath. This is not recommended.
scope
This element refers to the classpath of the task at hand (compiling and runtime, testing, etc.) as well as how to limit the transitivity of a dependency. There are five scopes available:
1) compile - this is the default scope, used if none is specified. Compile dependencies are available in all classpaths. Furthermore, those dependencies are propagated to dependent projects.
2) provided - this is much like compile, but indicates you expect the JDK or a container to provide it at runtime. It is only available on the compilation and test classpath, and is not transitive.
3) runtime - this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
4) test - this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. It is not transitive.
5) system - this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
exclusions
Exclusions tell Maven not to include the specified project that is a dependency of this dependency (in other words, its transitive dependency).
Exclusions contain one or more exclusion elements, each containing a groupId and artifactId denoting a dependency to exclude.
Inheritance
The packaging type required to be pom for parent and aggregation (multi-module) projects. These types define the goals bound to a set of lifecycle stages. For example, if packaging is jar, then the package phase will execute the jar:jar goal. Now we may add values to the parent POM, which will be inherited by its children.
the super pom
Just as Java objects ultimately inherit from java.lang.Object, all Project Object Models inherit from a base Super POM.
dependencyManagement
DependencyManagement is used by a POM to help manage dependency information across all of its children. If the my-parent project uses dependencyManagement to define a dependency on junit:junit:4.12, then POMs inheriting from this one can set their dependency giving the groupId=junit and artifactId=junit only and Maven will fill in the version set by the parent. The benefits of this method are obvious. Dependency details can be set in one central location, which propagates to all inheriting POMs.
在项目的 parent 层,可以通过 dependencyManagement 元素来管理 jar 包的版本,让子项目中引用一个依赖而不用显示的列出版本号。
dependencies和dependencyManagement的区别
单独使用dependencies时表示即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
dependencyManagement(一般嵌套一个dependencies一起使用)里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且 version 和 scope 都读取自父 pom; 另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
aggregation (or Multi-Module)
A pom packaged project may aggregate the build of a set of projects by listing them as modules, which are relative paths to the directories or the POM files of those projects.
You do not need to consider the inter-module dependencies yourself when listing the modules; i.e. the ordering of the modules given by the POM is not important. Maven will topologically sort the modules such that dependencies are always build before dependent modules.
properties
Maven properties are value placeholder, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example:
<project>
...
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
...
</project>
They come in five different styles:
env.X: Prefixing a variable with "env." will return the shell's environment variable. For example, ${env.PATH} contains the PATH environment variable. Note: While environment variables themselves are case-insensitive on Windows, lookup of properties is case-sensitive. In other words, while the Windows shell returns the same value for %PATH% and %Path%, Maven distinguishes between ${env.PATH} and ${env.Path}. As of Maven 2.1.0, the names of environment variables are normalized to all upper-case for the sake of reliability.
project.x: A dot (.) notated path in the POM will contain the corresponding element's value. For example:
<project><version>1.0</version></project>
is accessible via ${project.version}.settings.x: A dot (.) notated path in the settings.xml will contain the corresponding element's value. For example:
<settings><offline>false</offline></settings>
is accessible via ${settings.offline}.Java System Properties: All properties accessible via java.lang.System.getProperties() are available as POM properties, such as ${java.home}.
x: Set within a
element in the POM. The value of ` ` may be used as ${someVar}.value
Build
build元素被分为两个等级:在prject元素下的顶级build元素;在profile元素下的build元素。BaseBuild类型包含了这两个等级的构建元素共有的元素集。
【关于profile:可以在指定条件下被激活】
BaseBuild元素集
元素:defaultGoal, directory, finalName, filter
Resources
指定资源文件在project中的存放位置 元素:resources; targetPath; filtering; directory; includes; excludes; testResources
Plugins
a plugin is a collection of goals with a general common purpose
Beyond the standard coordinate of groupId:artifactId:version, there are elements which configure the plugin or this builds interaction with it:
- extensions
- inherited
- configuration
- executions id, goals, phase(指定goals将要执行的阶段,允许将任何目标绑定到生命周期的任何阶段), inherited, configuration
Plugin Management
However, this only configures plugins that are actually referenced within the plugins element in the children or in the current POM. The children have every right to override pluginManagement definitions.
参考链接:https://www.jianshu.com/p/49acf1246eff
Build元素集中只对project build可用(对profile build不可用)的两个元素
Directories set various directory structures 例如:
<build> <sourceDirectory>${basedir}/src/main/java</sourceDirectory> <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory> <outputDirectory>${basedir}/target/classes</outputDirectory> <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory> ... </build>
Extensions extensions are artifacts that activated during build. 执行构建过程中可能用到的其他工lib,在执行构建的过程中被加入到classpath中
reporting
Reporting contains the elements that correspond specifically for the site
generation phase.
<reporting>
中的配置作用于Maven的site阶段,用于生成报表。<reporting>
中也可以配置插件<plugins>
,并通过一个<plugin>
的<reportSet>
为该插件配置参数。注意,对于同时出现在<build>
和<reporting>
中的插件,<reporting>
中对该插件的配置也能够在构建过程中生效,即该插件的配置是<build>
和<reporting>
中的配置的合并。