2014-10-20 64 views
7

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

在这种情况下,我想将connector1,connector2,connector3包含在另一个模块中,而不必明确指定connector1,2,3。

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

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

更新

这更雷的忿怒,因为我想简单地添加一个连接器,并有该项目包含所有子模块依赖罐子。这会让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> 

这只是为了澄清原来的问题的例子。它不存在,如果它确实有效,可能会有反响。

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

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

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

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

+0

你有一个地方的示例项目(github?),我们可以得到一个更好的印象你到底想要做什么? – khmarbaise 2014-10-21 16:41:45

+1

@RobinJonsson问题不明。 *包括*在这里意味着什么?你想要什么最终结果?你的意思是你想要自动填写'connectors' POM中的''部分吗?或者别的地方?你能扩大一点吗? – Tunaki 2016-04-06 21:32:17

+1

@Tunaki我想要的结果是能够添加新的“connectorX”包,而无需将它们指定为其他地方的依赖项。假设我有另一个打包为WAR的模块。我想添加一个从WAR到“连接器”的依赖项,每当我向连接器添加一个新的“子模块”(可能是connector4)时,它都会自动打包WAR文件。我的连接器没有所需的封装格式。连接器是否作为单独的JAR或超级罐被包装并不重要。只要它们包含在我的WAR中。 – 2016-04-07 06:19:18

回答

10

一种方法是创建第四个模块,它将3个模块“包装”为依赖关系。这样你可以依靠这个包装模块。

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

尽管明确声明每个连接器的依赖关系更为明智,特别是它们只有三个。

替代解决方案:

更动态的方法(虽然非常矫枉过正IMO)是有这个第四模块封装使用定制assembly descriptor在装配实施模块。例如,里面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> 

注意,描述符告诉组装插件:

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

  2. 包括只有实现模块(connector模块如具有*:connector*include元件中所指定的。

当然,你需要配置Assembly插件使用在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> 

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

<dependencies> 
    <dependency> 
     <groupId>groupId</groupId> 
     <artifactId>connectorWrapper</artifactId> 
     <version>...</version> 
     <classifier>impl-modules</classifier> <!-- note the classifier --> 
    </dependency> 
</dependencies> 
+2

谢谢,但我的目标是让其他人能够创建实现,而不必手动将它们添加为项目的另一个依赖项。不过这是一个好主意,但这种方式相当于将它添加到另一个项目中的依赖关系列表。我认为可能有一种方法可以通过程序集插件来实现,但我现在正在研究这个问题。 – 2014-10-20 17:33:32

+0

不接受这个作为答案,因为这包括另一个项目,只是将依赖管理移动到它自己。最初的问题是,如果不主动地指定对新“连接器”的依赖性,它仍然是开放的。 – 2016-04-06 09:07:54

+1

@RobinJonsson事实上,正如OP所提到的那样,汇编插件可能是可行的。更新了我的答案。 – manouti 2016-04-06 15:01:11

1

不完全确定它是否确切y做你所需要的,但在最新的maven版本中,你可以在你的依赖关系上使用范围import

的第一步是创建一个包含所有依赖关系的POM你想在其他项目包括:

<project> 

    <groupId>com.foo</groupId> 
    <artifactId>connectors</artifactId> 
    <version>0.0.1</version> 
    <packaging>pom</packaging> 

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

     ... 
    </dependencies>  
</project> 

在项目中要包括你的连接器:

<dependency> 
    <groupId>com.foo</groupId> 
    <artifactId>connectors</artifactId> 
    <version>0.0.1</version> 
    <type>pom</type> 
    <scope>import</scope> 
</dependency> 

查看Importing Dependencies了解更多信息。

另一种方法是使用maven程序集插件并创建一个包含所有需要包含的类(单个jar包)的单个(巨大)jar; (为此,您还需要创建一次带有所有依赖项和程序集插件的pom)。

+0

不这仍然意味着我必须显式地指定所有子模块的'connectors'依赖? – 2016-04-08 06:54:34

+0

是在父pom中,您仍然需要定义需要成为部件的模块/依赖关系。我认为你的目标是让你可以在其他项目中包含连接器依赖项,而不是单独包含每个子连接器。可能误解了你。 – uniknow 2016-04-08 07:09:01

+0

没关系,看看我对原始问题的评论。问候 – 2016-04-08 08:43:52

0

我会为此write my own maven plugin。从你的声誉和问题来看,你可能会在一小时内准备好一些东西。最有可能比研究和尝试解决方案来做你想做的事更早。

相关问题