With Maven, how can I build a distributable that has my project's jar and all of the dependent jars?

前端 未结 3 1312

I have a project (of type \'jar\') that (obviously) builds a jar. But that project has many dependencies. I\'d like Maven to build a \"package\" or \"assembly\" that cont

3条回答
  •  失恋的感觉
    2020-12-01 04:33

    Here is my solution to create a distributable .zip (or .tar.gz / .tar.bz2) including all dependencies in a lib folder. It will:

    1. Create a jar with a Manifest including the dependencies of the lib directory as the classpath and the main class to run when executing the jar.
    2. Copy all dependent jars to the target/lib directory.
    3. Create the distributable `zip with the main jar and all dependent jars of the lib directory.

    Excerpt from pom.xml:

    
    
        org.apache.maven.plugins
        maven-jar-plugin
        
            
                
                    true
                    lib/
                    full.path.to.MainClass
                
            
        
    
    
        maven-dependency-plugin
        
            
                package
                
                    copy-dependencies
                
                
                    ${project.build.directory}/lib
                
            
        
    
    
        org.apache.maven.plugins
        maven-assembly-plugin
        
            
                package
                
                    attached
                
                
                    
                        src/main/resources/dist.xml
                    
                
            
        
    
    

    dist.xml:

    
        bin
        
            zip
            tar.gz
        
        
            
                ${project.basedir}
                /
                
                    README*
                    LICENSE*
                    NOTICE*
                
            
            
                ${project.build.directory}
                /
                
                    *.jar
                
            
            
                ${project.build.directory}/lib
                lib
                
                    *.jar
                
            
            
                ${project.build.directory}/site
                docs
            
        
    
    

    The dist.xml was derived from the bin descriptor format here: http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#bin

提交回复
热议问题