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

前端之家收集整理的这篇文章主要介绍了java – 如何包含pom项目中的所有模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一种方法,从另一个pom.xml包含项目中的所有模块.所以在我的情况下,我有一个父pom,包装设置为pom.它包含3个子模块,用于在另一个api模块中实现我的接口.我想在maven中动态地包含我项目中的所有子模块.

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

  1. connectors - packaging: pom
  2. connector1 - packaging: jar
  3. connector2 - packaging: jar
  4. connector3 - packaging: jar@H_301_5@
  5. 我尝试在我的项目中包含连接器pom,但这不起作用.我希望用pom指定父包将包含子模块,但这不起作用.有没有解决方法如何做到这一点?

  6. 更新

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

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

  9. <dependencies>
  10.         <dependency>
  11.             <groupId>com.foo</groupId>
  12.             <artifactId>connector1</artifactId>
  13.             <version>0.0.1</version>
  14.         </dependency>
  15.         <dependency>
  16.             <groupId>com.foo</groupId>
  17.             <artifactId>connector1-api</artifactId>
  18.             <version>0.0.1</version>
  19.         </dependency>
  20.         <dependency>
  21.             <groupId>com.foo</groupId>
  22.             <artifactId>connector1-etl</artifactId>
  23.             <version>0.0.1</version>
  24.         </dependency>
  25.         <dependency>
  26.             <groupId>com.foo</groupId>
  27.             <artifactId>connector1-persistence</artifactId>
  28.             <version>0.0.1</version>
  29.         </dependency>
  30.          <dependency>
  31.             <groupId>com.foo</groupId>
  32.             <artifactId>connector2</artifactId>
  33.             <version>0.0.1</version>
  34.         </dependency>
  35.         <dependency>
  36.             <groupId>com.foo</groupId>
  37.             <artifactId>connector2-api</artifactId>
  38.             <version>0.0.1</version>
  39.         </dependency>
  40.         <dependency>
  41.             <groupId>com.foo</groupId>
  42.             <artifactId>connector2-etl</artifactId>
  43.             <version>0.0.1</version>
  44.         </dependency>
  45.         <dependency>
  46.             <groupId>com.foo</groupId>
  47.             <artifactId>connector2-persistence</artifactId>
  48.             <version>0.0.1</version>
  49.         </dependency>
  50.        <dependency>
  51.             <groupId>com.foo</groupId>
  52.             <artifactId>connector2-other</artifactId>
  53.             <version>0.0.1</version>
  54.         </dependency>
  55.        ...
  56. </dependencies>@H_301_5@ 
  57.  

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

  58.   
  59.  
    <dependencies>
  60.         <dependency>
  61.             <groupId>com.foo</groupId>
  62.             <artifactId>connector1</artifactId>
  63.             <version>0.0.1</version>
  64.             <type>pom</type>
  65.             <include>submodules</include>
  66.         </dependency>
  67.         <dependency>
  68.             <groupId>com.foo</groupId>
  69.             <artifactId>connector2</artifactId>
  70.             <version>0.0.1</version>
  71.             <type>pom</type>
  72.             <include>submodules</include>
  73.         </dependency>
  74. </dependencies>@H_301_5@ 
  75.  

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

  76.  

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

  77.  

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

解决方法

一种方法是创建第四个模块,将3个模块“包装”为依赖关系.这样你可以依赖这个包装器模块.
  1. connectors - packaging: pom
  2. connector1 - packaging: jar
  3. connector2 - packaging: jar
  4. connector3 - packaging: jar
  5. connectorWrapper - packaging: pom (depends on the above three)@H_301_5@
  6. 虽然为每个连接器显式声明一个依赖项更有意义,特别是它们只有三个.

  7. 替代方案:

  8. 一个更动态的方法(虽然非常过分的IMO)是让第四个模块使用自定义assembly descriptor将实现模块打包到一个程序集中.例如,在connectorWrapper中,你可以编写一个assembly.xml

  9. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
  10.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  12.     <id>impl-modules</id>
  13.     <formats>
  14.         <format>jar</format>
  15.     </formats>
  16.     <includeBaseDirectory>false</includeBaseDirectory>
  17.     <fileSets>
  18.         <fileSet>
  19.             <directory>${project.basedir}</directory>
  20.             <includes>
  21.                 <include>pom.xml</include>
  22.             </includes>
  23.             <useDefaultExcludes>true</useDefaultExcludes>
  24.         </fileSet>
  25.     </fileSets>
  26.     <moduleSets>
  27.         <moduleSet>
  28.             <useAllReactorProjects>true</useAllReactorProjects>
  29.             <includes>
  30.                 <include>*:connector*</include>
  31.             </includes>
  32.             <binaries>
  33.                 <includeDependencies>false</includeDependencies>
  34.             </binaries>
  35.         </moduleSet>
  36.     </moduleSets>
  37. </assembly>@H_301_5@ 
  38.  

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

  39.  

    >包括当前项目反应器中的所有模块,因此当您在根项目中运行mvn clean包时,它将包含所有模块
    >仅包含实现模块(连接器模块),具有*:connector *的include元素中指定.

  40.  

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

  41.   
  42.  
    <plugins>
  43.     <plugin>
  44.         <artifactId>maven-assembly-plugin</artifactId>
  45.         <configuration>
  46.             <descriptors>
  47.                 <descriptor>assembly.xml</descriptor>
  48.             </descriptors>
  49.         </configuration>
  50.         <executions>
  51.             <execution>
  52.                 <id>make-assembly</id>
  53.                 <phase>package</phase>
  54.                 <goals>
  55.                     <goal>single</goal>
  56.                 </goals>
  57.             </execution>
  58.         </executions>
  59.     </plugin>
  60. </plugins>@H_301_5@ 
  61.  

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

  62.   
  63.  
    <dependencies>
  64.     <dependency>
  65.         <groupId>groupId</groupId>
  66.         <artifactId>connectorWrapper</artifactId>
  67.         <version>...</version>
  68.         <classifier>impl-modules</classifier> <!-- note the classifier -->
  69.     </dependency>
  70. </dependencies>@H_301_5@

猜你在找的Java相关文章