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

 他w与他说 发布于 2023-02-04 19:50

大家!我一直试图使用Maven Shade插件获取jar,但我仍然没有获得成功:(

这是我的项目结构:

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

Module1(pom.xml):


    MainModule
    com.plugintest
    1.0-SNAPSHOT

4.0.0
Module1

Module2(pom.xml):


    MainModule
    com.plugintest
    1.0-SNAPSHOT

4.0.0
Module1

MainModule(pom.xml):

com.plugintest
MainModule
pom
1.0-SNAPSHOT

    Module1
    Module2


    
        
        org.apache.maven.plugins
        maven-shade-plugin
        2.2
        
            
            package
            
                shade
            
            
        
        
    

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

请告诉我,为什么这个Shade插件不起作用?

1 个回答
  • MainModule不应该生成一个jar文件.它只能生成... pom文件.它包含所有子模块共享的配置.这就是针对每个模块调用shade插件的原因.

    而是创建第三个模块.我们称之为FinalModule.这个模块是孩子的MainModule.将整个<build>节点从MainModulepom.xml 移动到FinalModulepom.xml.

    文件结构:

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

    FinalModule pom.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] ------------------------------------------------------------------------
    

    2023-02-04 19:53 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有