【问题标题】:How to configure Maven shade plugin in a multi-module project?如何在多模块项目中配置 Maven 阴影插件?
【发布时间】:2014-01-28 01:53:01
【问题描述】:

我一直在尝试使用 Maven Shade Plugin 获取 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>

主模块(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 插件不起作用?

【问题讨论】:

  • 您的 Module2 在上面的代码 sn-ps 中被标记为“Module1”。 ...

标签: java maven maven-shade-plugin


【解决方案1】:

MainModule 不应该生成 jar 文件。它只能生成... pom 文件。它包含在所有子模块之间共享的配置。这就是为什么要针对每个模块调用 shade 插件的原因。

相反,创建第三个模块。我们称之为FinalModule。该模块是MainModule 的子模块。将整个&lt;build&gt; 节点从MainModule pom.xml 移动到FinalModule pom.xml。

文件结构:

主模块 -FinalModule -src -pom.xml -模块1 -src -pom.xml -模块2 -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] ------------------------------------------------------------------------

【讨论】:

  • 我收回了我以前的 cmets...我感到困惑/沮丧,因为我认为这种方法需要使用 &lt;parent&gt; pom 链接。谢天谢地,它没有;即使没有它们,当您在聚合 pom 上运行 mvn install 时,包含阴影配置的模块也会输出阴影 jar。
猜你喜欢
  • 2011-03-10
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 1970-01-01
  • 2016-04-16
  • 2017-12-26
  • 1970-01-01
相关资源
最近更新 更多