Building a fat jar using maven

后端 未结 6 2026
梦毁少年i
梦毁少年i 2020-11-21 23:51

I have a code base which I want to distribute as jar. It also have dependency on external jars, which I want to bundle in the final jar.

I heard that this can be do

6条回答
  •  耶瑟儿~
    2020-11-22 00:49

    Note: If you are a spring-boot application, read the end of answer

    Add following plugin to your pom.xml The latest version can be found at

    ...
    
        
            
                org.apache.maven.plugins
                maven-assembly-plugin
                CHOOSE LATEST VERSION HERE
                
                    
                        jar-with-dependencies
                    
                
                
                    
                        assemble-all
                        package
                        
                            single
                        
                    
                
            
        
    
    ...
    

    After configuring this plug-in, running mvn package will produce two jars: one containing just the project classes, and a second fat jar with all dependencies with the suffix "-jar-with-dependencies".

    if you want correct classpath setup at runtime then also add following plugin

    
        org.apache.maven.plugins
        maven-jar-plugin
        
            
                
                    true
                    fully.qualified.MainClass
                
            
        
    
    

    For spring boot application use just following plugin (choose appropriate version of it)

    
        org.springframework.boot
        spring-boot-maven-plugin
        
            true
            ${start-class}
        
        
            
                
                    repackage
                
            
        
    
    

提交回复
热议问题