Maven打包项目生成可执行jar包

方法一 

将项目打包到 ${project.build.directory} 中,并将相应依赖的jar包复制到 ${project.build.directory}/lib 目录下。这样,通过 java -jar 项目名.jar 就可以启动项目了。代码中有注释插件的作用,在此不再赘述。


    
    UTF-8



    
        
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.8.0
            
                1.8
                1.8
                ${project.build.sourceEncoding}
            
        
        
        
            org.apache.maven.plugins
            maven-surefire-plugin
            2.22.1
            
                true
            
        
        
        
            org.apache.maven.plugins
            maven-jar-plugin
            3.1.0
            
                
                    
                        true
                        lib/
                        包名.启动类名称
                    
                    
                    
                        
                    
                
            
        
        
        
            org.apache.maven.plugins
            maven-dependency-plugin
            3.1.1
            
                
                    copy-dependencies
                    package
                    
                        copy-dependencies
                    
                                                        
                        ${project.build.directory}/lib
                    
                
            
        
    

方法二



    org.apache.maven.plugins
    maven-install-plugin
    2.5.2
    
        
            install-jar
            clean
            
                ${project.basedir}/src/main/resources/lib/x.jar
                default
                a
                b
                1.0.0
                jar
                true
            
            
                install-file
            
        
    




    org.apache.maven.plugins
    maven-shade-plugin
    3.2.1
    
        
            package
            
                shade
            
            
                false
                
                    
                        cn.App
                    
                      
                        META-INF/spring.handlers  
                    
                      
                        META-INF/spring.schemas  
                    
                
            
        
    

其中,插件的最新版本请到下面的网站查询:

maven-compiler-plugin:https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin

maven-surefire-plugin:https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin

maven-jar-plugin:https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin

maven-dependency-plugin:https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin

maven-shade-plugin:​​​​https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin

你可能感兴趣的:(Maven)