当前位置: 首页 > 知识库问答 >
问题:

如何在多模块项目中配置Maven阴影插件?

竺鸿骞
2023-03-14

我一直在尝试使用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>

主模块(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)。

为什么这个阴影插件不起作用?

共有1个答案

翁鸿远
2023-03-14

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

相反,创建第三个模块。让我们称它为FinalModule。这个模块是MainModule的子模块。移动整个

文件结构:


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

FinalModulepom.xml如下所示:

最终模块(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] ------------------------------------------------------------------------

 类似资料:
  • 问题内容: 我一直在尝试使用Maven Shade插件来获取jar,但是我仍然没有成功。 这是我的项目结构: Module1(pom.xml): Module2(pom.xml): MainModule(pom.xml): 根据这段代码,我得到了2个jar文件(Module1-version.jar和Module2-version.jar)。但这不是我想要的。我希望得到1个jar文件(MainMo

  • 如何在多模块maven项目设置中正确设置插件? A(https://github.com/DataSystemsLab/GeoSpark)依赖于B(https://github.com/jiayuasu/JTSplus)是C的分支(com.livitsolutions.jts) 现在,对于我项目中的一些任务,我需要使用D(http://www.geotools.org)这取决于com的另一个版本。

  • 我是maven的新手,在使用程序集插件生成zip文件时花了大约3天的时间,引用了http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnable-binary-distribution-with-maven-assembly-plugin/My project is multi module,所以我还引用

  • 我有一个问题,为我的多模块maven项目正确设置Spring启动。 有一个模块“api”使用另一个模块“core”。Api有一个应用程序。包含spring的属性文件。邮政主机=xxx。根据Spring Boot文档,这为您提供了JavaMailSender接口的默认实现,可以自动连接。 然而,负责发送电子邮件的类驻留在“核心”包中。当我尝试构建该模块时,构建失败,因为找不到JavaMailSend

  • 问题内容: 我正在研究一个多模块Maven项目,其结构如下: war模块取决于jar模块,并将打包后将jar工件添加到webapp的lib目录中。 war模块和jar模块都使用Apache log4j进行日志记录,并共享相同的log4j配置文件(log4j.xml),该文件当前位于jar模块项目中。并且此log4j.xml将打包到jar- module.jar文件中,但是,我想将其放入war包中而

  • 我正在学习本教程https://spring.io/guides/gs/multi-module/#scratch 在根目录中,我创建了3个目录:moduleA、moduleB和moduleC 我运行了