7.1 Maven 中的变量——属性
经常遇到的参数 Maven 允许你放入变量中。当您需要匹配 pom 文件不同部分的参数时,这非常有用。例如,您可以将 Java 版本、库版本、某些资源的路径放入变量中。
为此,在 中有一个特殊的部分pom.xml – <properties>
,其中声明了变量。变量的一般形式如下:
<variable-name> _ _ _ _meaning< / variable name > _
例子:
<properties>
<junit.version>5.2</junit.version>
<project.artifactId>new-app</project.artifactId>
<maven.compiler.source>1.13</maven.compiler.source>
<maven.compiler.target>1.15</maven.compiler.target>
</properties>
使用不同的语法访问变量:
$ { variable -name } _
在编写此类代码的地方,Maven 将替换变量的值。
例子:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</build>
7.2 Maven 中的预定义变量
在 pom 文件中描述项目时,可以使用预定义的变量。它们可以有条件地分为几组:
- 内置项目属性;
- 项目属性;
- 设置。
只有两个内置项目属性:
财产 | 描述 |
---|---|
${basedir} | 项目根目录在哪里pom.xml |
${版本} | 工件版本;可以使用${project.version} 或${pom.version} |
«project»
可以使用或前缀引用项目属性«pom»
。我们有四个:
财产 | 描述 |
---|---|
${project.build.directory} | «target» 项目目录 |
${project.build.outputDirectory} | «target» 编译目录。默认«target/classes» |
${项目名称} | 项目名称 |
${项目.版本} | 项目版本 |
settings.xml
可以使用前缀访问属性settings
。名称可以是任何东西——它们取自settings.xml
. 例子:
${settings.localRepository} sets the path to the local repository.
GO TO FULL VERSION