You can use Maven properties in a pom.xml file or in any resource that is being processed by the Maven Resource plugin’s filtering features. A property is always surrounded by ${ and }. For example, to reference the project.version
property, one would write:
1.0
There are some implicit properties available in any Maven project, these implicit properties are:
project.*
project.*
prefix to reference values in a Maven POM.settings.*
settings.*
prefix to reference values from your Maven Settings in ~/.m2/settings.xml.env.*
PATH
and M2_HOME
can be referenced using the env.*
prefix.System.getProperty()
method can be referenced as a Maven property.In addition to the implicit properties listed above, a Maven POM, Maven Settings, or a Maven Profile can define a set of arbitrary, user-defined properties. The following sections provide some detail on the various properties available in a Maven project.
9.2.1. Maven Project Properties
When a Maven Project Property is referenced, the property name is referencing a property of the Maven Project Object Model (POM). Specifically, you are referencing a property of the org.apache.maven.model.Model
class which is being exposed as the implicit variable project
. When you reference a property using this implicit variable, you are using simple dot notation to reference a bean property of the Model
object. For example, when you reference ${project.version}, you are really invoking the getVersion()
method on the instance of Model
that is being exposed as project
.
The POM is also represented in the pom.xml document present in all Maven projects. Anything in a Maven POM can be referenced with a property. A complete reference for the POM structure is available at http://maven.apache.org/ref/3.0.3/maven-model/maven.html. The following list shows some common property references from the Maven project.
project.groupId
and project.version
groupId
and version
identifiers. When you are declaring interdependencies between two modules which share the same groupId
and version
, it is a good idea to use a property reference for both:<dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>sibling-project</artifactId> <version>${project.version}</version> </dependency> </dependencies>
project.artifactId
project.artifactId
in your POM file like this:<build> <finalName>${project.artifactId}</finalName> </build>
project.name
and project.description
project.build.*
If you are ever trying to reference output directories in Maven, you should never use a literal value like target/classes. Instead you should use property references to refer to these directories.
- project.build.sourceDirectory
- project.build.scriptSourceDirectory
- project.build.testSourceDirectory
- project.build.outputDirectory
- project.build.testOutputDirectory
- project.build.directory
sourceDirectory
, scriptSourceDirectory
, and testSourceDirectory
provide access to the source directories for the project. outputDirectory
and testOutputDirectory
provide access to the directories where Maven is going to put bytecode or other build output. directory
refers to the directory which contains all of these output directories.
project.baseUri
${project.baseUri}
property. If your project is stored in the directory /tmp/simple, ${project.baseUri}
will resolve to file:/private/tmp/simple/.For a full list of properties available on the Maven Model
object, take a look at the JavaDoc for the maven-model
project here http://maven.apache.org/ref/3.0.3/maven-model/apidocs/index.html. Once you load this JavaDoc, take a look at the Model
class. From this Model
class JavaDoc, you should be able to navigate to the POM property you wish to reference. If you needed to reference the output directory of the build, you can use the Maven Model JavaDoc to see that the output directory is referenced via model.getBuild().getOutputDirectory()
; this method call would be translated to the Maven property reference ${project.build.outputDirectory}.
For more information about the Maven Model module, the module which defines the structure of the POM, see the Maven Model project page at http://maven.apache.org/ref/3.0.3/maven-model.
9.2.2. Maven Settings Properties
You can also reference any properties in the Maven Local Settings file which is usually stored in ~/.m2/settings.xml. This file contains user-specific configuration such as the location of the local repository and any servers, profiles, and mirrors configured by a specific user.
A full reference for the Local Settings file and corresponding properties is available here http://maven.apache.org/ref/3.0.3/maven-settings/settings.html.
9.2.3. Environment Variable Properties
Environment variables can be referenced with the env.* prefix. Some interesting environment variables are listed in the following list:
env.PATH
PATH
in which Maven is running. The PATH
contains a list of directories used to locate executable scripts and programs.env.HOME
env.JAVA_HOME
env.M2_HOME
While they are available, you should always use the Java System properties if you have the choice. If you need a user’s home directory use ${user.home} instead of ${env.HOME}. If you do this, you’ll end up with a more portable build that is more likely to adhere to the Write-Once-Run-Anywhere (WORA) promise of the Java platform.
9.2.4. Java System Properties
Maven exposes all properties from java.lang.System
. Anything you can retrieve from System.getProperty()
you can reference in a Maven property. The following table lists available properties:
Table 9.1. Java System Properties
System Property |
Description |
|
Java Runtime Environment version |
|
Java Runtime Environment vendor |
|
Java vendor URL |
|
Java installation directory |
|
Java Virtual Machine specification version |
|
Java Virtual Machine specification vendor |
|
Java Virtual Machine specification name |
|
Java Virtual Machine implementation version |
|
Java Virtual Machine implementation vendor |
|
Java Virtual Machine implementation name |
|
Java Runtime Environment specification version |
|
Java Runtime Environment specification vendor |
|
Java Runtime Environment specification name |
|
Java class format version number |
|
Java class path |
|
Path of extension directory or directories |
|
Operating system name |
|
Operating system architecture |
|
Operating system version |
|
File separator ("/" on UNIX, "\" on Windows) |
|
Path separator (":" on UNIX, ";" on Windows) |
|
Line separator ("\n" on UNIX and Windows) |
|
User’s account name |
|
User’s home directory |
|
User’s current working |
9.2.5. User-defined Properties
In addition to the implicit properties provided by the POM, Maven Settings, environment variables, and the Java System properties, you have the ability to define your own arbitrary properties. Properties can be defined in a POM or in a Profile. The properties set in a POM or in a Maven Profile can be referenced just like any other property available throughout Maven. User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin. Here’s an example of defining some arbitrary properties in a Maven POM.
User-defined Properties in a POM.
<project> ... <properties> <arbitrary.property.a>This is some text</arbitrary.property.a> <hibernate.version>3.3.0.ga</hibernate.version> </properties> ... <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>${hibernate.version}</version> </dependency> </dependencies> ... </project>
The previous example defines two properties: arbitrary.property.a
and hibernate.version
. The hibernate.version
is referenced in a dependency declaration. Using the period character as a separator in property names is a standard practice throughout Maven POMs and Profiles. The next example shows you how to define a property in a profile from a Maven POM.
User-defined Properties in a Profile in a POM.
<project> ... <profiles> <profile> <id>some-profile</id> <properties> <arbitrary.property>This is some text</arbitrary.property> </properties> </profile> </profiles> ... </project>
The previous example demonstrates the process of defining a user-defined property in a profile from a Maven POM. For more information about user-defined properties and profiles, see Chapter 5, Build Profiles.
英文原文:http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-properties.html
相关推荐
【标题】:“maven笔记:maven-overlay-实战笔记” 在 Maven 的世界里,"overlay" 是一个重要的概念,主要用于Web项目的构建和部署。Maven overlay 技术允许你将多个项目的输出“重叠”在一起,形成一个新的项目,...
**Maven教程:基础篇——尚硅谷学习笔记 2022年** Maven是一个强大的Java项目管理和构建工具,由Apache软件基金会开发。它通过提供一个标准的项目对象模型(Project Object Model,POM)来简化项目的构建过程,并...
### 黑马Maven笔记详解 #### Maven简介与特点 Maven是一款强大的项目管理和构建工具,作为Apache基金会下的一个开源项目,它完全采用Java语言编写,主要用于管理Java项目。Maven通过标准化项目构建过程和依赖管理...
【尚硅谷Maven课程笔记代码资源】是一份全面学习Maven的资料集合,它涵盖了从基础到高级的各种知识点,旨在帮助开发者深入理解并熟练运用Maven进行自动化构建。该资源包含课件、源码和相关的笔记,使得学习过程更加...
读书笔记:maven实战学习笔记
读书笔记:Maven 实战读书笔记
Maven的安装: (首先保证JDK版本在1.6以上) 1: 通过配置MAVEN_HOME 和 %% %MAVEN_HOME%\bin 然后进行mvn -version 测试 掌握 -Xms 与 -Xmx的相关配置 2: Maven目录分析: 2.1: bin: 含有mvn运行的脚本 2.2...
### Maven核心知识点解析 #### Maven概述与价值 Maven是一个项目管理和综合工具,主要用于Java项目的构建、依赖管理和项目信息管理。通过Maven,开发者可以轻松地处理项目中的多个痛点问题,提升开发效率。 #####...
maven学习笔记maven学习笔记maven学习笔记
### 黑马Maven笔记第二天知识点总结 #### Maven的核心优势 **1. 统一管理JAR包** - **节省空间**:Maven通过中央仓库管理所有项目的依赖,避免了重复下载相同JAR包的问题,有效减少了硬盘空间的占用。 - **依赖...
读书笔记:Maven 实践学习按理阅读《Maven实战》笔记。
读书笔记:maven实战
读书笔记:Maven 实战
读书笔记:maven in action(maven实战)
读书笔记:learn about maven from maven 实战
读书笔记:maven实战源码
读书笔记:maven实战学习
读书笔记:Maven实战练习
POM.xml文件是这个模型的XML表示,定义了项目属性和构建指令。 2. **坐标(Coordinates)**:每个Maven项目都有唯一的标识,由groupId、artifactId和version组成。例如,`com.example:my-app:1.0.0`,其中`...