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

Java 如何在多模块项目中配置Maven shade插件?,java,maven,maven-shade-plugin,Java,Maven,Maven Shade Plugin,我一直在尝试使用Maven Shade插件获取jar,但仍然没有成功 这是我的项目结构: MainModule -Module1 -src -pom.xml -Module2 -src -pom.xml -pom.xml MainModule -FinalModule -src -pom.xml -Module1 -src -pom.xml -Modu

我一直在尝试使用Maven Shade插件获取jar,但仍然没有成功

这是我的项目结构:

MainModule
  -Module1
    -src
    -pom.xml
  -Module2
    -src
    -pom.xml
  -pom.xml
MainModule -FinalModule -src -pom.xml -Module1 -src -pom.xml -Module2 -src -pom.xml -pom.xml 模块1(pom.xml):


主模块
com.pluginest
1.0-快照
4.0.0
模块1
模块2(pom.xml):


主模块
com.pluginest
1.0-快照
4.0.0
模块1
MainModule(pom.xml):

com.pluginest
主模块
聚甲醛
1.0-快照
模块1
模块2
org.apache.maven.plugins
maven阴影插件
2.2
包裹
阴凉处
根据这段代码,我得到了2个jar文件(Module1-version.jar和Module2-version.jar)。但这不是我想要的。我希望得到一个jar文件(mainmoduleversion.jar),其中包含另一个(Module1和Module2)


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

您不应该生成jar文件。它只能生产。。。pom文件。它包含所有It子模块共享的配置。这就是针对每个模块调用shade插件的原因

相反,创建第三个模块。让我们称之为
FinalModule
。此模块是
main模块
的子模块。将整个
节点从
MainModule
pom.xml移动到
FinalModule
pom.xml

文件结构:

MainModule
  -Module1
    -src
    -pom.xml
  -Module2
    -src
    -pom.xml
  -pom.xml
MainModule -FinalModule -src -pom.xml -Module1 -src -pom.xml -Module2 -src -pom.xml -pom.xml 最后,你应该得到这样的结果:

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

在上面的代码片段中,您的Module2被标记为“Module1”。。。我收回我以前的评论…我感到困惑/沮丧,因为我认为这种方法需要使用
pom链接。谢天谢地,事实并非如此;即使没有它们,当您在聚合pom上运行
mvn install
时,包含shade config的模块也会输出shade jar。
<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] ------------------------------------------------------------------------