Can maven projects have multiple parents?

后端 未结 6 712
我在风中等你
我在风中等你 2020-12-01 04:06

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 c

相关标签:
6条回答
  • 2020-12-01 04:43

    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 the 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

    0 讨论(0)
  • 2020-12-01 04:47

    The only way is to have base-pom as parent of java-base-pom and flex-base-pom.

    I have similar structure for my spring projects:

    base-pom (basic configuration - eclipse, reports, repositories, etc)
    |
    + spring-base-pom (spring definitions)
      |
      + spring-jar-base-pom (jar specific definitions)
      |
      + spring-war-base-pom (spring web and servlet dependencies)
        |
        + spring-webapp-base_pom (spring web mvc dependencies)
    
    0 讨论(0)
  • 2020-12-01 04:54

    Just image that pom.xml are in fact Java classes: you can have only one parent (or extends a class), but this parent can also have another parent, and so on.

    As I explained here, you must distinguish the parent and aggregation principles in Maven, which means that my-plugin would be considered as an aggregation project, not necessarily a parent project for both my-plugin-client and my-plugin-parent.

    So to summarize:

    my-plugin will define the base pom for all your projects. Then, you create two new pom projects: java-base-pom and flex-base-pom. They have both my-plugin as parent. Now, my-plugin-client will have java-base-pom as parent, while my-plugin-server will use flex-base-pom for his parent.

    This way, my-plugin-client will inherit all properties defined in the my-plugin pom.xml, and also from java-base-pom project.

    0 讨论(0)
  • 2020-12-01 04:54

    You can achieve multiple inheritance with profiles:

    You create (multiple) profiles in the root pom, and auto activate any variation of these profiles achieves multiple inheritance of maven configuration.

    0 讨论(0)
  • 2020-12-01 04:55

    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.

    0 讨论(0)
  • 2020-12-01 04:57

    I've cross this exact proble also, and the best solution I found was to use Inheritance and Aggregation as suggest in this question : does maven support multiple parents (multiple inheritance) ?

    You can have an aggregator pom that is not the parent of the projects it aggregates.

    and explain in the Maven Documentation

    Inheritance and aggregation create a nice dynamic to control builds through a single, high-level POM (...) Conversely, a POM project may aggregate projects that do not inherit from it.

    From this I had my POMs inheritance (pom-master contains communes configurations, and each children the specifics ones) :

    pom-master
      |-- pom-java
      |-- pom-flex
    

    and so my project can get the specifics for each modules configurations as wished :

    project (aggregate project-flex & project-java)
      |-- project-java
      |      `-- pom.xml => parent = pom-java
      |-- project-flex
      |       `-- pom.xml ==> parent = pom-flex
      `-- pom.xml => parent = pom-master
    
    

    Hope it will help others as well :)

    0 讨论(0)
提交回复
热议问题