How can I create an executable jar without dependencies using Maven?

后端 未结 2 1057
旧时难觅i
旧时难觅i 2020-12-30 03:32

I want to package it not in a single executable jar for distribution. I need an executable to be something like main.jar and all dependencies to be in libs/*.jar

How

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-30 04:00

    I prefer this a bit modified solution. Create Your executable jar with classpath set and copy all dependencies to given directory.

    You don't need any additional files.

    Dependencies are being copied during install phase.

    
        
            
                org.apache.maven.plugins
                maven-jar-plugin
                
                    
                        
                            true
                            lib/
                            fully.qualified.MainClass
                        
                    
                
            
    
            
                org.apache.maven.plugins
                maven-dependency-plugin
                
                    
                        install
                        
                            copy-dependencies
                        
                        
                            ${project.build.directory}/lib
                            
                            
                            runtime
                            provided
                            
                        
                    
                
            
        
    
    

    I updated the answer to copy only runtime dependencies as described here - copying the right dependencies. Test and provided dependencies are excluded.

    It turns out that if you want to exclude dependencies from both the test and provided scopes, you need to exclude the provided scope and include the runtime scope.

提交回复
热议问题