【发布时间】:2016-05-19 10:07:19
【问题描述】:
目前我有一个多模块 maven 项目,其中版本和插件在父级中定义并在模块中使用。
parent.pom 中的示例依赖:
<properties>
<junit.version>4.12</junit.version>
</properties>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
并在模块中使用:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
这工作正常,项目构建没有任何问题。现在我想将一些版本和依赖项提取到一个 independent enterprise.pom 中,以全局定义它们并在我的不同项目中使用它们。所以我创建了一个企业项目(类型 POM):
<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>
<groupId>com.myurl</groupId>
<artifactId>META-POM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencyManagement>
</project>
然后我运行 mvn clean install 将其放入我的本地存储库,它运行良好 - 我可以在那里看到它
接下来我更改了我的多模块项目以使用 META-POM 作为依赖项(以了解 META-POM 中定义的版本)并删除了 junit-property 以及依赖项
<dependency>
<groupId>com.myurl</groupId>
<artifactId>META-POM</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
当我现在尝试打包我的多模块项目时,它失败了,因为依赖项没有在 META-POM 中定义的属性。 即使我将模块中的依赖项更改为
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
它仍然给我同样的错误for junit:jar must be a valid version but is '${junit.version}'.
如何从企业 POM 派生版本属性?我想在一个地方定义它们,所以我确保在任何地方都使用相同的版本。
附:我想拥有一个独立的企业 POM,而无需在企业 POM 和所有多模块项目之间定义 parent/module
【问题讨论】:
-
您需要在 META-POM 的依赖项中添加
<scope>import</scope>。之后,您应该能够在没有版本标签的情况下使用对 junit 的依赖... -
是的,你是对的!您想将其发布为答案以便我接受吗?