如何包含pom项目中的所有模块

Chr*_*haw 7 java maven-3 maven

我正在寻找一种方法,从另一个pom.xml包含项目中的所有模块.所以在我的情况下,我有一个包装设置为pom的父pom.它包含3个子模块,用于在另一个api模块中实现我的接口.我想在maven中动态地包含我项目中的所有子模块.

在这种情况下,我想在另一个模块中包含connector1,connector2,connector3,而不必隐式指定connector1,2,3.

connectors - packaging: pom
   connector1 - packaging: jar
   connector2 - packaging: jar
   connector3 - packaging: jar
Run Code Online (Sandbox Code Playgroud)

我尝试在我的项目中包含连接器pom,但这不起作用.我希望用pom指定父包将包含子模块,但这不起作用.有没有解决方法如何做到这一点?

更新

这更像是我的后果,因为我想简单地添加一个连接器并包含项目的所有子模块依赖关系jar.这将使pom更容易阅读.

而不必像这样注册所有子依赖项

<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>
Run Code Online (Sandbox Code Playgroud)

这只是澄清原始问题的一个例子.它不存在,如果确实有效,可能会有重新发布.

<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>
Run Code Online (Sandbox Code Playgroud)

如果我没记错的话,我正在为订购系统创建一个模块化项目,在那里我有一个内部系统会使用的通用API(REST).我正在创建一个路由系统,我可以根据订单的标准(国家,优先税等)将订单路由到单个履行中心.每个履行中心都有自己的api(连接器).

原始问题中的示例大大简化,使问题更简洁.在实际项目中,每个连接器(1,2,3)都是一个具有多个依赖关系jar的独立pom.一个用于他们的客户端api,然后一些etl代码与我原来的api匹配.

我不记得我是怎么解决这个问题的.我想我只需要包含所有子依赖项.

man*_*uti 10

一种方法是创建第四个模块,将3个模块"包装"为依赖关系.这样你可以依赖这个包装器模块.

connectors - packaging: pom
   connector1 - packaging: jar
   connector2 - packaging: jar
   connector3 - packaging: jar
   connectorWrapper - packaging: pom (depends on the above three)
Run Code Online (Sandbox Code Playgroud)

虽然明确声明每个连接器的依赖关系更有意义,尤其是它们只有三个.

替代方案:

一个更动态的方法(虽然非常过分的IMO)是让第四个模块使用自定义程序集描述符将实现模块打包到程序集中.例如,在里面connectorWrapper,你可以写一个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>
Run Code Online (Sandbox Code Playgroud)

请注意,描述符告诉程序集插件:

  1. 包括当前项目反应器中的所有模块,因此当您mvn clean package在根项目中运行时,它将包括所有模块

  2. 包括实现模块(connector模块),如include元素中指定的那样*:connector*.

当然,您需要配置程序集插件以使用此描述符connectorWrapper(或您为此包装器选择的任何其他名称):

<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>
Run Code Online (Sandbox Code Playgroud)

然后,您可以mvn install在根项目上运行以安装程序集工件,之后您可以从其他项目依赖它:

<dependencies>
    <dependency>
        <groupId>groupId</groupId>
        <artifactId>connectorWrapper</artifactId>
        <version>...</version>
        <classifier>impl-modules</classifier> <!-- note the classifier -->
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但我的目标是让其他人能够创建实现,而无需手动将它们添加为项目的另一个依赖项.虽然这是一个好主意,但这相当于将其添加到另一个项目中的依赖项列表中.我认为可能有一种方法可以使用程序集插件执行此操作,但我现在正在研究它. (2认同)