Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven duplicated groupId, artifactId and versions in submodules

Tags:

maven-2

We're optimizing our Maven configuration (from previously using ant) and I've just read the Maven by Example book from Sonatype. Duplicating configurations has gotten us in trouble in the past, so I absolutely want to avoid even the tiniest bit of this.

The above book mentions to use the built-in project.groupId and project.version properties from a parent module when referring to the other sibling submodules as dependencies:

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

That works great, I love it. But this doesn't work in the tag of the submodule pom.xml's:

<parent>
    <groupId>${project.groupId}</groupId>
    <artifactId>${project.artifactId}</artifactId>
    <version>${project.version}</version>
    <relativePath>../pom.xml</relativePath>
</parent>

I guess not a big deal, seems like I can create properties for these, but with many modules I'd really like to fully understand the best practices for these kinds of issues..

UPDATE The best way so far it seems to do this is the following. A bit ugly, but eliminates duplicate hard-coded values. parent pom.xml:

<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<packaging>pom</packaging>
<version>${version}</version>
<properties>
    <groupId>com.mycompany</groupId>
    <artifactId>mycompany</artifactId>
    <version>1.0</version>
</properties>

child pom.xml:

<parent>
    <groupId>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <version>${version}</version>
    <relativePath>../pom.xml</relativePath>
</parent>
<artifactId>child</artifactId>
like image 577
at. Avatar asked Aug 07 '10 00:08

at.


People also ask

What is groupId artifactId and version in Maven?

Maven uses a set of identifiers, also called coordinates, to uniquely identify a project and specify how the project artifact should be packaged: groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project.

How do I delete duplicate dependencies in Maven?

you can use mvn dependency:tree command to find duplicate dependencies into your project. Use the <exclusions> tag into <dependency> tag of the pom to exclude that duplicate dependencies from maven project.

What will happen if the Maven version number of POM XML file does not match with the machine used to install?

Maven won't allow any other either. Build will fail if version is not found.

Can a Maven project have multiple pom files?

Yes you can use Maven Profiles to manage this. Obviously you can tweak this approach to suit your needs however works best.


1 Answers

That works great, I love it. But this doesn't work in the tag of the submodule pom.xml's (...)

This can't work, chicken and egg problem: you can't get the coordinates of the parent to use from the parent.

I guess not a big deal, seems like I can create properties for these, but with many modules I'd really like to fully understand the best practices for these kinds of issues

Well, assuming it would work (and to my knowledge, properties are not expanded in the parent element so it won't), where would put these properties? In the parent? Same problem as above. In the child? No point. So in short, you need to hard-code these values in the parent element

Note that this is not a problem for the groupId and artifactId as they don't change; however, it's more annoying for the version and I suggest to either use the Maven Release Plugin or the Versions Maven Plugin (and its versions:update-child-modules goal).

PS: Maven 3.1 will support version-less parent element (see MNG-624).

Related answers

  • Missing artifact error in Maven
  • Maven2 cannot find parent from relative path

Why can't I get the coordinates for the parent from the parent? The relativePath should give me access to the parent pom. So it actually works if you add the groupId, artifactId and version as properties in the parent and then reference those properties FOR the parent in the child. So that there's no duplicated values, the corresponding hard-coded elements in the parent were replaced also with references to the properties. It seems like an ugly way to go about this, but it works...

No, it does NOT work, property substitution is not allowed in the parent element (for later reproducibility). That's how it is designed in Maven 2.x. See MNG-624 and the millions of threads about this on the users mailing list, e.g.:

  • Pom Parent Version Properties
  • Parent project version

You asked about the best practice, I answered: hard-code everything in the parent element. Period.

like image 188
Pascal Thivent Avatar answered Oct 06 '22 12:10

Pascal Thivent