【发布时间】:2020-01-08 17:19:08
【问题描述】:
我遇到了一个问题,当且仅当定义了父 pom 时,maven 无法将属性正确替换到我的存储库 URL 中。这尤其是一个问题,因为父 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.group</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<groupId>com.group</groupId>
<artifactId>project-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>project</name>
<description>test project</description>
<properties>
<nexus.url>https://nexus.myorganization.net</nexus.url>
</properties>
<repositories>
<repository>
<id>nexus-server</id>
<url>${nexus.url}/repository/maven-releases/</url>
</repository>
</repositories>
</project>
使用这个 pom,我收到错误消息Cannot access ${nexus.url}/repository/maven-snapshots/...,很明显它没有用实际值替换属性。
如果我删除 POM 的 <parent> 部分,那么突然属性替换就可以正常工作了:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.group</groupId>
<artifactId>project-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>project</name>
<description>test project</description>
<properties>
<nexus.url>https://nexus.myorganization.net</nexus.url>
</properties>
<repositories>
<repository>
<id>nexus-server</id>
<url>${nexus.url}/repository/maven-releases/</url>
</repository>
</repositories>
<!-- adding this dependency so that Maven is forced to download from the repository -->
<dependencies>
<!-- some dependency here -->
</dependencies>
</project>
我知道它工作正常,因为我可以在 maven 的输出中看到 Downloading from nexus-server: https://nexus.myorganization.net/repository/maven-releases/... 这一行
有什么想法吗?
【问题讨论】:
-
那里不提供变量替换。这是为了使您的构建更加健壮而设计的。
-
为什么不呢?为什么只有在没有定义父 pom 时才可用?
-
从唯一位置使用的存储库应该是您的
settings.xml文件,不应更改,也不应放入 pom 文件中... -
我假设从 repo 下载父 pom 时会发生错误?这是有道理的,因为要构建整个属性列表,您需要下载父级并构建有效的 POM。因此,您无法应用属性,因为尚未下载父级,并且您无法下载父级,因为尚未应用属性。这就是我对它为什么不起作用的看法,但我没有进行任何实验来证实这个假设。
-
它在父 pom 坐标被解析后 AFTER 可用。你不能这样做。