2014-01-09 102 views
9

大家好!我一直在尝试使用Maven插件阴影拿到罐子,但我还是没有得到一个成功:(如何在多模块项目中配置Maven遮罩插件?

这是我的项目结构:

MainModule 
    -Module1 
    -src 
    -pom.xml 
    -Module2 
    -src 
    -pom.xml 
    -pom.xml 

模块1(pom.xml中):

<parent> 
    <artifactId>MainModule</artifactId> 
    <groupId>com.plugintest</groupId> 
    <version>1.0-SNAPSHOT</version> 
</parent> 
<modelVersion>4.0.0</modelVersion> 
<artifactId>Module1</artifactId> 

单词数(pom.xml中):

<parent> 
    <artifactId>MainModule</artifactId> 
    <groupId>com.plugintest</groupId> 
    <version>1.0-SNAPSHOT</version> 
</parent> 
<modelVersion>4.0.0</modelVersion> 
<artifactId>Module1</artifactId> 

MainModule(pom.xml中):

<groupId>com.plugintest</groupId> 
<artifactId>MainModule</artifactId> 
<packaging>pom</packaging> 
<version>1.0-SNAPSHOT</version> 
<modules> 
    <module>Module1</module> 
    <module>Module2</module> 
</modules> 
<build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <version>2.2</version> 
     <executions> 
      <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>shade</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
</build> 

根据这段代码,我得到了2个jar文件(Module1-version.jar和Module2-version.jar)。但这不是我想要的。我希望获得1个jar文件(MainModule-version.jar),其中包含另一个文件(Module1和Module2)。

请告诉我,为什么Shade Plugin不工作?

+0

您的Module2在上面的代码片段中标记为“Module1”。 ... – ingyhere

回答

11

MainModule不应该产生一个jar文件。它只能生成... pom文件。它包含跨所有子模块共享的配置。这就是为什么阴影插件被称为每个模块。

取而代之,创建第三个模块。我们称之为FinalModule。该模块是MainModule的子项。将整个<build>节点从MainModule pom.xml移动到FinalModule pom.xml。

文件结构:

 
    MainModule 
     -FinalModule 
     -src 
     -pom.xml 
     -Module1 
     -src 
     -pom.xml 
     -Module2 
     -src 
     -pom.xml 
     -pom.xml 

FinalModulepom.xml看起来是这样的:

FinalModule(pom.xml中)

<parent> 
    <groupId>com.plugintest</groupId> 
    <artifactId>MainModule</artifactId> 
    <version>1.0-SNAPSHOT</version> 
</parent> 
<artifactId>FinalModule</artifactId> 

<dependencies> 
    <dependency> 
     <groupId>com.plugintest</groupId> 
     <artifactId>Module1</artifactId> 
     <version>1.0-SNAPSHOT</version> 
    </dependency> 
    <dependency> 
     <groupId>com.plugintest</groupId> 
     <artifactId>Module2</artifactId> 
     <version>1.0-SNAPSHOT</version> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>2.2</version> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

最后,你应该得到这样的事情:

[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ FinalModule --- 
[INFO] Building jar: D:\workspaces\java\Parent\FinalModule\target\FinalModule-1.0-SNAPSHOT.jar 
[INFO] 
[INFO] --- maven-shade-plugin:2.2:shade (default) @ FinalModule --- 
[INFO] Including my:Module1:jar:1.0-SNAPSHOT in the shaded jar. 
[INFO] Including my:Module2:jar:1.0-SNAPSHOT in the shaded jar. 
[INFO] Replacing original artifact with shaded artifact. 
[INFO] Replacing D:\workspaces\java\Parent\FinalModule\target\FinalModule-1.0-SNAPSHOT.jar with D:\workspaces\java\Parent\FinalModule\target\FinalModule-1.0-SNAPSHOT-shaded.jar 
[INFO] Dependency-reduced POM written at: D:\workspaces\java\Parent\FinalModule\dependency-reduced-pom.xml 
[INFO] ------------------------------------------------------------------------ 
[INFO] Reactor Summary: 
[INFO] 
[INFO] Parent ............................................ SUCCESS [0.016s] 
[INFO] Module1 ........................................... SUCCESS [1.654s] 
[INFO] Module2 ........................................... SUCCESS [0.343s] 
[INFO] FinalModule ....................................... SUCCESS [0.953s] 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
+0

我收回了我之前的评论......我感到困惑/沮丧,因为我认为这种方法需要使用''pom链接。谢天谢地,它没有;即使没有它们,当你在聚合体上运行'mvn install'时,包含阴影配置的模块将输出阴影的jar。 – Andy

相关问题