Edmondo
Edmondo

Reputation: 20080

Hierarchical maven project versions

I have an intricated Maven project with modules and subprojects up to four level of inheritance. Furthermore, some modules depend on modules which are not in the same "subtree" and I am wondering if I can avoid to express the version every time I have module dependency.

For example I have

a
 -b
   -c
 -d
   -e

Supposing module e depends on c, what is the best way to specify the version in a DRY manner?

If inside the pom.xml of module e I don't specify which version of the c module I want, my build will fail. Is there a smart way to handle this versioning problem?

Upvotes: 0

Views: 306

Answers (2)

khmarbaise
khmarbaise

Reputation: 97359

The best approach is to define all artifacts with their appropriate version numbers in a dependencyManagement block in the root of the project like:

a (pom.xml)
 -b
   -c
 -d
   -e

With the following:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>project</groupId>
      <artifactId>b</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>project</groupId>
      <artifactId>c</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>project</groupId>
      <artifactId>d</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>project</groupId>
      <artifactId>e</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

But with the given structure:

a (pom.xml)
 -b
   -c
 -d
   -e

a must include a list of modules (packaging pom), b(packaging pom) and d(packaging pom) as well which means a, b and d will never be used as dependencies which means you can omit them from the above block.

Upvotes: 1

Rai
Rai

Reputation: 394

groupId and artifactId denote a single project within maven, but version must be specified (or inherited from a parent pom) in order to determine what version of that project maven needs to reference. It appears redundant , but it is necessary especially if there are many different versions.

Upvotes: 2

Related Questions