Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can maven projects have multiple parents?

We have Java and Flex projects. We currently have 1 base pom that contains the configurations we want to use for both projects. Problem with this is: Flex projects inherit configuration, for example, for javadoc and pmd plugins, which is not desirable.

I want to clean it up and have a real base pom, and then a java-base-pom and a flex-base-pom. But how does this work in a multi-module that has both a Flex part and a Java part?

We have plugins to our own application where we use the following structure:

  • my-plugin
    • my-plugin-client (flex)
    • my-plugin-server (java)

my-plugin just contains a pom.xml with <modules/> section. I would use my-plugin pom.xml as a parent for both, but then I cannot also use the java base-pom or the flex base-pom as parent. What would be the best approach for this?

like image 537
Wim Deblauwe Avatar asked Oct 28 '09 11:10

Wim Deblauwe


People also ask

What is parent project in Maven?

A parent pom. xml file (or super POM) in Maven is used to structure the project in order to avoid redundancies and duplicate configurations by using an inheritance between different pom. xml files. If any dependency or properties are configured in both - parent and child - pom.

What is the difference between parent and dependency in Maven?

A dependency is libraries you need to get your code to compile. This can be your own code, or libraries such as Apache Commons. A parent contains information, but nothing to actually build, that is shared between a number of your projects.

What is a multi-module Maven project?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

Can a 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.


2 Answers

Even though maven projects have single parent, they can import any number of other pom's like this:

<dependencyManagement>     <dependencies>         <dependency>             <groupId>org.example</groupId>             <artifactId>my-shared-dependencies</artifactId>             <version>0.0.1-SNAPSHOT</version>             <type>pom</type>             <scope>import</scope>         </dependency>     </dependencies> </dependencyManagement> 

This has two important differences compared to a parent:

  1. Plugins defined in the imported pom won't be imported
  2. Dependencies defined in the imported pom won't be added to the current pom, it will only import dependencies into the dependency management section

However, if your parent pom has a <dependencies> section and you want to include those into your dependencies, then you can add the parent to your <dependencies> section just like a regular dependency:

<dependency>     <groupId>org.example</groupId>     <artifactId>my-shared-dependencies</artifactId>     <version>0.0.1-SNAPSHOT</version> </dependency> 

Even though the same dependency is already imported, the version tag has to be specified again. To reduce duplication, it can be stored in a property

like image 94
Peter Szanto Avatar answered Oct 06 '22 01:10

Peter Szanto


A project can have only one parent (unlike multiple inheritance in C++) but this parent can be part of a bigger parent hierarchy. As pointed out by others, you could thus have something like this:

 base-pom/ |-- flex-base-pom |   |-- my-plugin-client |   |   `-- pom.xml |   `-- pom.xml |-- java-base-pom |   |-- my-plugin-server |   |   `-- pom.xml |   `-- pom.xml  `-- pom.xml 

That said, I noticed you wrote that your actual problem is that:

flex projects inherit configuration for javadoc and pmd for example, which they do not want.

You should use the pluginManagement element to avoid this situation:

pluginManagement is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.

So, in the parent pom, configure your plugins in pluginManagement (javadoc and pmd for example), and reference them within the plugins element in the desired children (only in my-plugin-server here). This would solve your current issue.

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

Pascal Thivent