Question

say I have some child poms, which share 90% in common. I want to put their common code into a common place. I can't use a common parent because these child poms needs to extend from their own parents as well. Is there other place I can put common stuff, define once, and let the child poms to share, instead of copying the common stuff multiple times in each child pom? Or if you have other opinion, please suggest as you pleased. Thank you

e.g. Project A has poms

<groupId>project-a</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>

and

<groupId>project-a</groupId>
<artifactId>parent</artifactId>
<version>2.0</version>

Project B implemented by a totally different team has this parent pom

<groupId>project-b</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
<modules>
    <module>child-extends-from-project-a-version-1.0</module>
    <module>child-extends-from-project-a-version-2.0</module>
</modules>

and some child projects that extend from Project A

<parent>
    <groupId>project-a</groupId>
    <artifactId>parent</artifactId>
    <version>1.0</version>
</parent>
<groupId>project-b.child</groupId>
<artifactId>child-extends-from-project-a-version-1.0</artifactId>
"some common stuff"

<parent>
    <groupId>project-a</groupId>
    <artifactId>parent</artifactId>
    <version>2.0</version>
</parent>
<groupId>project-b.child</groupId>
<artifactId>child-extends-from-project-a-version-2.0</artifactId>
"some common stuff"

How do I group those "common stuff" in one place without repeating in each child pom?

Was it helpful?

Solution

(I deleted my old answer, because I misread your question, sorry)

There is way, but it is a little bit cumbersome. Since the long planned Maven fragments are not yet implemented, you could use the tiles-maven-plugin:

https://github.com/maoo/maven-tiles

The Tiles plugin allows you to "import" foreign poms into your model. So you would create a separate pom for your "common stuff":

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>common</groupId>
  <artifactId>common-stuff</artifactId>
  <packaging>pom</packaging>
  <version>1.0</version>
  <name>"Common Stuff"</name>

  "Common stuff"

</project>

In your actual child POMs, you include the tiles plugin as extension (possibly in your true parent pom):

<build>
  <plugins>
    <plugin>
      <groupId>it.session.maven.plugins</groupId>
      <artifactId>tiles-maven-plugin</artifactId>
      <version>${maventiles.plugin.version}</version>
      <extensions>true</extensions>
    </plugin>
  </plugins>
</build>

Now you can import your common stuff using properties (I am not quite happy with using properties for this, but it works):

  <properties>
    <tile.1>common:common-stuff:1.0</tile.1>
  </properties>

As a result, your effective POM contains elements from your child project itself as well as from the imported common-stuff artifact.

The plugin is somewhat proprietary, but for normal usecases, it works quit well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top