文章目录
今天根据公司pom文件里的标签简要学习了一下pom各个标签的作用:
参考官方文档: http://maven.apache.org/pom.html#The_Basics
Quick 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>
The Basics
pom中包含了工程所有的必要信息以及构建过程需要的插件。
Maven Coordinates
groupId:artifactId:version
这三者是必须的字段,定义了在仓库中的位置。
groupId:定义了同一项目或者同一组织
artifactId:一般是项目的名称
version:版本
packaging
<packaging>war</packaging>
配置工程打包成pom
, jar
, maven-plugin
, ejb
, war
, ear
, rar
,默认为jar
POM Relationship
maven项目有三个关系:依赖(包括传递依赖)、继承以及聚合(多模块工程)
dependency
依赖是pom的基础
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
-
groupId,artifactId,version
依赖由maven坐标定位
如果中央库没有所需要的包:
- 用install插件在本地安装
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
-
创建私服保存
-
将依赖关系范围设定为System并填写systemPath
- scope
规定了传递依赖的范围和当前任务的classpath
- optional
当前项目是依赖包时,可选的依赖项。比如A依赖于B中不在运行时编译的代码,如果X依赖于A,那么就不需要加载B
Exclusions
exclusions指定的包,maven将会不去加载依赖包中的传递依赖包
示例代码中指定依赖maven-embedder
,但是不使用依赖maven-core
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
Inheritance
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
</project>
以下元素可以被子元素继承:groupId、version、description、url、inceptionYear、organization、licenses、developers、contributors、mailingLists、scm、issueManagement、ciManagement、properties、dependencyManagement、dependencies、repositories、pluginRepositories、build(plugin executions with matching ids、plugin configuration、etc)、reporting、profiles
以下三个属性无法被继承:
- artifactId
- name
- prerequisites
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
</project>
relativePath
:maven第一个寻找的路径
Dependency Management
为子poms配置元素和传递依赖
如果在父pom中定义了 对junit:junit:4.12
的依赖,那么在子pom中只需要给出groupId
=junit
and artifactId
=junit
,version由父pom给出。由此,依赖的详细信息只需要在中心位置设置,再继承给所有其他的poms
Aggregation(or Multi-Module)
pom可以通过将一组项目列为模块来进行项目聚合
<modules>
<module>my-project</module>
<module>another-project</module>
<module>third-project/pom-example.xml</module>
</modules>
Proporties
${X}
通过这个方式来访问设定的值
<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>
${maven.compiler.source}
env.x
:返回shell的环境变量
project.x
:<project><version>1.0</version></project>
=${project.version}
settings.x
:setting.xml中的值 用${settings.x}
来访问
java.lang.System.getProperties()
${java.home}
x
Build Setting
声明项目目录结构和管理插件等信息
Build
build包括两部分:basebuild和profile
<!-- "Project Build" contains more elements than just the BaseBuild set -->
<build>...</build>
<profiles>
<profile>
<!-- "Profile Build" contains a subset of "Project Build"s elements -->
<build>...</build>
</profile>
</profiles>
</project>
BaseBuild
basebuild中包含pom中的基本元素集
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
defaultGoal:执行的最终目标
directory:构建的目标文件夹,默认值为${basedir}/target
finalName:最终构建项目的名字
Resources
指定资源,比如代码生成
Plugin
配置插件,构建交互
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<extensions>false</extensions>
<inherited>true</inherited>
<configuration>
<classifier>test</classifier>
</configuration>
<dependencies>...</dependencies>
<executions>...</executions>
</plugin>
</plugins>
</build>
extension:是否加载此插件的扩展名
inherited:此插件是否继续应用于继承的poms
configuration:针对单个插件
如果poms声明了parent,它会继承父pom中的build/plugins or pluginManagement中的配置
dependencies:插件的依赖
executions:插件可能有多个目标,每个插件执行自己配置的目标
Plugin Management
旨在配置从此继承的plugin
子poms中只要加上groupId和artifactId就可以应用父pom中的所有配置
The Build Element Set
<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>
Directories
设置目录结构
*Directoy被设置为绝对路径时使用该路径,相对路径则基于build directory:${basedir}
Profiles
项目可以根据构建环境来更改设置。
<profiles>
<profile>
<id>test</id>
<activation>...</activation>
<build>...</build>
<modules>...</modules>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<dependencies>...</dependencies>
<reporting>...</reporting>
<dependencyManagement>...</dependencyManagement>
<distributionManagement>...</distributionManagement>
</profile>
</profiles>
Activation
配置文件可以仅在某些情况下修改pom,这些情况都是由activation来指定的