在一个多模块项目中如何配置Maven shade插件?

22

我一直在尝试使用Maven Shade插件获取jar文件,但是我还没有成功。

这是我的项目结构:

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>

模块2(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>
根据这段代码,我得到了两个jar文件(Module1-version.jar和Module2-version.jar)。但这不是我想要的。我希望得到一个包含其他两个模块(Module1和Module2)的jar文件(MainModule-version.jar)。 为什么Shade插件不起作用呢?

你的Module2在上面的代码片段中被标记为“Module1”。 - ingyhere
1个回答

23

您的 MainModule 不应该生成jar文件。它只能生成pom文件。它包含所有子模块共享的配置。这就是为什么每个模块都要调用shade插件。

相反,创建第三个模块。我们称之为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] ------------------------------------------------------------------------

1
我收回之前的评论...我感到困惑和沮丧,因为我认为这种方法需要使用<parent> pom链接。幸运的是,它并不需要;即使没有它们,包含shade配置的模块在聚合pom上运行mvn install时也会输出阴影jar。 - Andy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接