How to configure Maven shade plugin in a multi-module project?

前端 未结 1 1838
太阳男子
太阳男子 2021-02-01 21:09

I have been trying to get jar using Maven Shade Plugin, but I still don\'t get a success.

This is my project structure:

MainModule
  -Module1
    -src
             


        
1条回答
  •  佛祖请我去吃肉
    2021-02-01 21:51

    You MainModule is not supposed to produce a jar file. It can produce only... pom files. It contains configuration shared across all it child modules. This is why the shade plugin is called against each modules.

    Instead, create a third module. Let's call it FinalModule. This module is a child of MainModule. Move the whole node from MainModule pom.xml to FinalModule pom.xml.

    File structure:

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

    The FinalModule pom.xml looks like this:

    FinalModule (pom.xml)

    
        com.plugintest
        MainModule
        1.0-SNAPSHOT
    
    FinalModule
    
    
        
            com.plugintest
            Module1
            1.0-SNAPSHOT
        
        
            com.plugintest
            Module2
            1.0-SNAPSHOT
        
    
    
    
        
            
                org.apache.maven.plugins
                maven-shade-plugin
                2.2
                
                    
                        package
                        
                            shade
                        
                    
                
            
        
    
    

    In the end, you should get something like this:

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

    0 讨论(0)
提交回复
热议问题