Michel Feinstein
Michel Feinstein

Reputation: 14296

How to avoid Maven Versions on Siblings Submodules?

I have a Maven structure such as:

- Parent
  - Child 1
  - Child 2

I have defined the Parent <version>1.0-SNAPSHOT</version> and left Child 1 and Child 2 without versions, this way both children will inherit the Parent version automatically.

The problem is, I need to reference Child 1 as a dependency to Child 2, and the only way to do it is to pass Child 1 version, such as:

<dependency>
    <groupId>com.myapp</groupId>
    <artifactId>child-1</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

I wanted to avoid this, since both children will inherit the Parent version anyway, but I can't include Child 1 as:

<dependency>
    <groupId>com.myapp</groupId>
    <artifactId>child-1</artifactId>
</dependency>

Is it there a way to avoid referencing Child 1 verion on Child 2 POM?

Getting this automatically will be a lot less error-prone.

Upvotes: 1

Views: 481

Answers (1)

madhead
madhead

Reputation: 33442

Sure, you can do that.

Parent pom.xml:

<groupId>com.acme</groupId>
<artifactId>root</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child-1</module>
    <module>child-2</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.acme</groupId>
            <artifactId>child-1</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.acme</groupId>
            <artifactId>child-2</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
<dependencyManagement>

And then just reference siblings without versions:

Child 1 pom.xml:

<parent>
    <groupId>com.acme</groupId>
    <artifactId>root</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>child-1</artifactId>
<name>Child 1</name>

<dependencies>
    <dependency>
        <groupId>com.acme</groupId>
        <artifactId>child-2</artifactId>
    </dependency>
</dependencies>

Child 2 pom.xml:

<parent>
    <groupId>com.acme</groupId>
    <artifactId>root</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>child-2</artifactId>
<name>Child 2</name>

<dependencies>
    <dependency>
        <groupId>com.acme</groupId>
        <artifactId>child-1</artifactId>
    </dependency>
</dependencies>

Explicit parent version in children is not a problem as both mvn versions:set and Maven Release Plugin can handle that.

Upvotes: 2

Related Questions