如何从已导入的Maven依赖管理POM中访问属性?
问题背景
我通过<scope>import</scope>将一个dependency-management POM导入到另一个Maven项目中,但无法直接访问该POM里定义的属性。尝试过将其设为父POM,但目标项目已有父POM,此方案不可行;也试过properties-maven-plugin,但不清楚如何用它从外部POM读取属性。
项目结构
dependency-management POM (artifact1-0.1.pom):
<project> <packaging>pom</packaging> <groupId>group1</groupId> <artifactId>artifact1</artifactId> <name>management-pom</name> <version>0.1</version> <properties> <test.version>0.2</test.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>group2</groupId> <artifactId>artifact2</artifactId> <version>${test.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
导入该POM的目标项目POM (artifact3-0.1.pom):
<project> <packaging>war</packaging> <groupId>group3</groupId> <artifactId>artifact3</artifactId> <name>war-pom</name> <version>0.1</version> <properties> <!-- 如何访问dependency-management POM中的${test.version}? --> <maven.test.version>${test.version}</maven.test.version> </properties> <dependencies> <dependency> <groupId>group2</groupId> <artifactId>artifact2</artifactId> <scope>compile</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>group1</groupId> <artifactId>artifact1</artifactId> <version>0.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
解决方案
方法1:用properties-maven-plugin读取外部POM属性
通过maven-dependency-plugin先将dependency-management POM复制到本地构建目录,再用properties-maven-plugin读取其中的属性:
<build> <plugins> <!-- 复制dependency-management POM到构建目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-management-pom</id> <phase>initialize</phase> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>group1</groupId> <artifactId>artifact1</artifactId> <version>0.1</version> <type>pom</type> <outputDirectory>${project.build.directory}</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <!-- 读取复制后的POM属性 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <executions> <execution> <id>load-management-properties</id> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>${project.build.directory}/artifact1-0.1.pom</file> </files> </configuration> </execution> </executions> </plugin> </plugins> </build>
配置完成后,目标项目就能直接使用${test.version}属性。
方法2:提取属性到共享配置文件
将dependency-management POM中的属性移到单独的shared.properties文件,在两个POM中都通过properties-maven-plugin读取该文件:
- 在dependency-management POM的资源目录下创建
shared.properties:
test.version=0.2
- 修改dependency-management POM,添加插件读取该文件并打包资源:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <executions> <execution> <id>load-shared-props</id> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>src/main/resources/shared.properties</file> </files> </configuration> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
- 在目标项目中同样配置读取该文件(需确保能访问到
shared.properties,可通过依赖引入或直接复制):
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <executions> <execution> <id>load-shared-props</id> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>path/to/shared.properties</file> </files> </configuration> </execution> </executions> </plugin> </plugins> </build>
方法3:调整继承链(需修改现有父POM)
如果有权限修改目标项目的现有父POM,可以将dependency-management POM设为现有父POM的父POM,形成目标项目 -> 现有父POM -> dependency-management POM的继承链。这样目标项目就能直接继承到dependency-management POM中的属性。
关键说明
Maven的import scope仅会导入dependency-management节点的内容,不会继承properties、build等其他配置,因此必须通过上述方法间接获取属性。
内容的提问来源于stack exchange,提问作者John

