Maven属性

Maven为了支持构建的灵活性,内置了属性Profile资源过滤三大特性。

Maven属性

Maven共有6类属性,分别为内置属性POM属性自定义属性Settings属性Java系统属性环境变量属性。正确使用这些属性可以帮助简化POM配置维护工作

内置属性

主要有两个常用内置属性:

  • ${basedir}:包含pom.xml文件的目录。即项目的根目录
  • ${version}项目的版本号

POM属性

用户可以使用POM属性引用POM文件中对应的元素值。常用POM属性:

  • ${project.build.sourceDirectory}:项目主源码目录,默认为src/main/java/
  • ${project.build.testScoreDirectory}:项目测试源码目录,默认为src/test/java/
  • ${project.build.directory}:项目构建输出目录,默认target/
  • ${project.outputDirectory}:项目代码编译输出目录,默认/target/classes/
  • ${project.testOutputDirectory}:项目测试代码编译输出目录,默认/target/test-classes/
  • ${project.groupId}:项目groupId
  • ${project.artifactId}:项目artifactId
  • ${project.version}:项目version,与${version}等价
  • ${project.build.finalName}:项目打包输出文件名称,默认为${project.artifactId}-${project.version}

自定义属性

在POM文件中的properties元素下定义的Maven属性。

Setting属性

与POM属性同理,使用setting.开头的属性引用setting.xml文件中XML元素的值。例如${setting.localRepository}引用本地仓库地址

Java系统属性

所有Java系统属性都可以使用Maven属性引用。例如${user.home}引用用户目录。

可以使用mvn help:system命令查看所有的Java系统属性

环境变量属性

所有环节变量都可以使用以env.开头的Maven属性引用。例如${env.JAVA_HOME}引用JAVA_HOMR环境变量的值。

可以使用mvn help:system命令查看所有的环境变量

Profile

为了应对环境的变化,Maven属性可以将变化的部分提取出来。针对不同环境设置不同的属性插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<profiles>
<profile>
<id>dev</id>
<properties>
<release.file>test</release.file>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>false</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<excludes>
<exclude>com.long.model.IT.**</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>product</id>
<properties>
<release.file>product</release.file>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Maven3支持pom.xml用户setting.xml全局setting.xml三种Profile。pom.xml中声明的Profile只对当前项目有效,用户setting.xml对本机上该用户的所有项目有效,全局setting.xml对本机上所有项目有效。

Maven支持多种方式激活Profile,可以通过mvn clean install -Pdev, test激活dev和test两个Profile;可以在setting文件或者POM中通过如下配置显示激活。

1
2
3
<activation>
<activeByDefault>true</activeByDefault>
</activation>

通过系统属性激活,当系统属性test存在且值等于x时激活,value可以没有,用户也可以通过命令行设置系统属性mvn clean install -Dtest=x从而激活配置。

1
2
3
4
5
6
7
8
9
10
<profiles>
<profile>
<activation>
<property>
<name>test</name>
<value>x</value>
</property>
</activation>
</profile>
</profiles>

操作系统环境激活,name、arch、version可以通过查看环境中的系统属性获取。

1
2
3
4
5
6
7
8
9
10
11
12
<profiles>
<profile>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
</profile>
</profiles>

文件存在与否激活,如下所示当x文件不存在y文件存在时激活。

1
2
3
4
5
6
7
8
9
10
<profiles>
<profile>
<activation>
<file>
<missing>x.properties</missing>
<exists>x.properties</exists>
</file>
</activation>
</profile>
</profiles>

若项目中有很多profile,其激活方式各异,用户可以通过mvn help:active-profiles命令查看当前激活的profile,也可以通过mvn help:all-profiles列出当前所有的profile