Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include all modules from pom project

I am looking for a way to include all the modules in a project from another pom.xml. So in my case I have a parent pom with packaging set to pom. It contains 3 child modules that implement my interfaces in another api module. I want to dynamically include all the child modules in my project in maven.

In this case I want to include the connector1, connector2, connector3 in another module without having to specifiy the connector1,2,3 implicitly.

connectors - packaging: pom
   connector1 - packaging: jar
   connector2 - packaging: jar
   connector3 - packaging: jar

I tried including the connectors pom in my project but this did not work. I was hoping that specifying the parent package with pom would include the child modules but this did not work. Are there any workarounds for how to do this?

Update

This was more of peeve of mine because I wanted to simply add a single connector and have all the child module dependency jars for the project be included. This would make the pom a little simpler to read.

Instead of having to register all the child dependencies like so

<dependencies>
        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector1</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector1-api</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector1-etl</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector1-persistence</artifactId>
            <version>0.0.1</version>
        </dependency>

         <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector2</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector2-api</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector2-etl</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector2-persistence</artifactId>
            <version>0.0.1</version>
        </dependency>

       <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector2-other</artifactId>
            <version>0.0.1</version>
        </dependency>
       ...
</dependencies>

This is just an example to clarify the original question. It does not exist and would probably have reprocussions if it did work.

<dependencies>
        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector1</artifactId>
            <version>0.0.1</version>
            <type>pom</type>
            <include>submodules</include>
        </dependency>

        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>connector2</artifactId>
            <version>0.0.1</version>
            <type>pom</type>
            <include>submodules</include>
        </dependency>

</dependencies>

If I remember correctly I was creating a modular project for an ordering system where I had a common api that our internal system would use (REST). I was creating a routing system where I could route an order to a single fulfillment center based on a criteria of the order (country, priority taxes etc). Each of the fulfillment centers had their own api (connectors).

The example is greatly simplified in the original question to make it more the problem more concise. In the real project each connector (1,2,3) would have been a separate pom with multiple dependency jars. One for their client api, then some etl code to match with my original api.

I don't remember how I solved this. I think I just had to include the all the child dependencies.

like image 450
Chris Hinshaw Avatar asked Oct 20 '14 17:10

Chris Hinshaw


People also ask

How do I run a multi-module project in Maven?

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

How can I add an existing Maven project to another Maven project as a module?

in you maven top level module. You might also need to import the new maven module. Just do a regular "Import existing maven project into workspace" to get this done. Save this answer.


1 Answers

One way is to create a fourth module which "wraps" the 3 modules as dependencies. This way you could depend on this wrapper module which would.

connectors - packaging: pom
   connector1 - packaging: jar
   connector2 - packaging: jar
   connector3 - packaging: jar
   connectorWrapper - packaging: pom (depends on the above three)

Although it would make more sense to explicitly declare a dependency for each connector especially that they are only three.

Alternative solution:

A more dynamic approach (although very much an overkill IMO) is to have this fourth module package the implementation modules in an assembly using a custom assembly descriptor. For example, inside connectorWrapper, you could write an assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">

    <id>impl-modules</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <includes>
                <include>pom.xml</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
    </fileSets>
    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>*:connector*</include>
            </includes>
            <binaries>
                <includeDependencies>false</includeDependencies>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

Note that the descriptor tells the assembly plugin to:

  1. include all modules in the current project reactor, so when you run mvn clean package in the root project, it will include all modules

  2. include only implementation modules (the connector modules), as specified in the include element having *:connector*.

Of course you'll need to configure the assembly plugin to use this descriptor in the connectorWrapper (or whatever other name you choose for this wrapper):

<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptors>
                <descriptor>assembly.xml</descriptor>
            </descriptors>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

Then you can run mvn install on the root project to install the assembly artifact, after which you can depend on it from the other project:

<dependencies>
    <dependency>
        <groupId>groupId</groupId>
        <artifactId>connectorWrapper</artifactId>
        <version>...</version>
        <classifier>impl-modules</classifier> <!-- note the classifier -->
    </dependency>
</dependencies>
like image 133
M A Avatar answered Oct 09 '22 09:10

M A