How to merge module jars to a single jar in Maven2?

前端 未结 2 717
陌清茗
陌清茗 2020-12-16 13:58

I have a maven2 project with several jar modules, build the project will get .jar archives for each module in the directory modules/XYZ/target/XYZ-x.x.x.jar

Now, if

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 14:41

    What you want to achive is called uber jar. This module has to have dependecies of all others submodules you want to package into one jar. If you create another submodule that will produce a desired artifact it can be built in reactor with all its dependencies but if it will be a separate project that you have to install all uber jar dependecies.

    | parent
    | -- submodule1
    ...
    | -- submoduleN
    | -- uberjarSubmodule
    

    Uber jar can be done by using:

    1. maven-shade-plugin - in your case you have to remember to exclude transitive dependecies from your modules

      
      ...
      
      
        
          org.apache.maven.plugins
          maven-shade-plugin
          1.2.2
          
            
              package
              
                shade
              
              
                
                  
                    classworlds:classworlds
                    junit:junit
                    jmock:jmock
                    xml-apis:xml-apis
                  
                
              
            
          
        
      
      
      ...
      
      
    2. maven-assembly-plugin - in this question you'll find a detailed answer

提交回复
热议问题