Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven property substitution for repository url not happening if parent pom is defined

Tags:

I'm having an issue where maven is not properly substituting properties into my repository URLs if and only if there is a parent pom defined. This is particularly a problem because the parent pom is IN the remote repository, so I need to have the parent pom defined.

minimum reproducible example:

<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>

Using this pom, I get the error message Cannot access ${nexus.url}/repository/maven-snapshots/... so clearly it is not replacing the property with the actual value.

If I remove the <parent> section of the POM, then suddenly property substitution begins working just fine:

<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>

I know its working properly because I can see in maven's output the line Downloading from nexus-server: https://nexus.myorganization.net/repository/maven-releases/...

Any ideas?

like image 974
Alex A Avatar asked Jan 08 '20 17:01

Alex A


People also ask

Why am I getting error at parent tag in POM xml?

Just go to Project Tab on Eclipse and Select Clean. Then Eclipse will automatically remove the Maven Dependencies and re-build your project. By then, the error may be gone.

Where is Maven repository URL?

m2/settings. xml or ${maven. home}/conf/settings.

Is it possible to refer a property defined in your POM xml file?

User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin.


1 Answers

Thanks to the commenters, the reason is because the property resolution does not occur until after the parent pom has been resolved, so that there are no property conflicts between current pom and parent pom.

More details provided at tickets MNG-2569 and MNG-624 (source).

like image 63
Alex A Avatar answered Sep 30 '22 21:09

Alex A