Java – how to include all modules in a POM project

I'm looking for a way from another POM XML contains all the modules in the project So in my case, I have a parent POM with the wrapper set to POM It contains three sub modules for implementing my interface in another API module I want to dynamically include all sub modules in my project in Maven

In this case, I want to include connector1, connector2 and connector3 in another module without implicitly specifying connector1,2,3

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

I tried to include connector POM in my project, but it didn't work I want to use POM to specify that the parent package will contain child modules, but this does not work Is there a solution how to do this?

to update

This is more like my shadow, because I want to simply add a connector and include all the sub module dependencies of the project This will make POM easier to read

You don't have to register all child dependencies like this

<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 of clarifying the original question It does not exist. If it is effective, it may be republished

<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'm creating a modular project for the ordering system, where I have a common API (rest) that my internal system will use I'm creating a routing system. I can route orders to a single fulfillment center according to the order criteria (country, priority tax, etc.) Each fulfillment center has its own API (connector)

The examples in the original problem are greatly simplified to make the problem more concise In a real project, each connector (1,3) is an independent POM with multiple dependent jars A client API for them, and then some ETL code matches my original API

I don't remember how I solved the problem I think I just need to include all child dependencies

Solution

One way is to create a fourth module and "wrap" the three modules as dependencies So you can rely on this wrapper module

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

Although it makes more sense to explicitly declare a dependency for each connector, especially if they have only three

Alternatives:

A more dynamic approach (although very excessive IMO) is to have the fourth module package the implementation module into an assembly using a custom assembly descriptor For example, in connectorwrapper, you can 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 plug-in:

>Includes all modules in the reactor of the current project, so when you run the MVN clean package in the root project, it will contain all modules > only implementation modules (connector modules), as specified in the include element with *: connector *

Of course, you need to configure the assembly plug-in to use this descriptor in the connectorwrapper (or any 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>

You can then run MVN install on the root project to install the assembly artifact, which you can then rely on from other projects:

<dependencies>
    <dependency>
        <groupId>groupId</groupId>
        <artifactId>connectorWrapper</artifactId>
        <version>...</version>
        <classifier>impl-modules</classifier> <!-- note the classifier -->
    </dependency>
</dependencies>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>