maven using parent dependencyManagement with ${project.version} causes dependency in wrong version

1.1k views Asked by At

I have the following structure with the following dependency: child1->child2.

and a parent for both which consolidates all versions in dependency management, using ${project.version}.

Folder structures:

+ parent
  + child1
  + child2

See poms below + complete example here.

Everything works find when child2's version is set to 1.0-SNAPSHOT.

When trying to change just child2's version to 2.0-SNAPSHOT, I get the following error:

Failure to find ...:child1:jar:2.0-SNAPSHOT

Why is maven trying to find version child1 2.0 and not 1.0?

Parent:

<?xml version="1.0" encoding="UTF-8"?>
<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>info.fastpace.issue.unknowversion</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>child1</module>
        <module>child2</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>info.fastpace.issue.unknowversion</groupId>
                <artifactId>child1</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>info.fastpace.issue.unknowversion</groupId>
                <artifactId>child2</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

Child1:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <artifactId>child1</artifactId>

    <parent>
        <groupId>info.fastpace.issue.unknowversion</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
</project>

Child2:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <artifactId>child2</artifactId>
    <version>2.0-SNAPSHOT</version>

    <parent>
        <groupId>info.fastpace.issue.unknowversion</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>info.fastpace.issue.unknowversion</groupId>
            <artifactId>child1</artifactId>
        </dependency>
    </dependencies>
</project>
2

There are 2 answers

3
AlikElzin-kilaka On BEST ANSWER

It seems that replacing ${project.version} in the parent pom with hard coded 1.0-SNAPSHOT solved this.

Don't know exactly why changing to hard coded values worked, but at least works now.

0
Matthew Read On

parent.version depends on the current project, and isn't automatically replaced when generating artifacts unless you use something like flatten-maven-plugin. (Maven understands the hardcoded version and manipulates it appropriate when it is the same through the project, as you've discovered.) That means that using these artifacts as dependencies in any other project will not work. It also means that if a subproject is treated as the current project (such as changing to a child's subdirectory and building from there), then it dictates what parent.version is. In your case, that would directly alter the version of managed dependency for child1; this seems to be the error that you're seeing.

The parent project cannot be all of a reactor (the root POM specifying <modules>), a parent (a POM referenced by another's <parent>), and a project BOM (a POM listing the other modules of the project in <dependencyManagement>) at the same time if you want to have separate versions. You want two or even three modules for that instead of the one, and you can't use parent.version.