SpringBoot使用Maven打包——将依赖Jar包与应用Jar包分离

SpringBoot项目使用Maven打包:

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

执行mvn clean package命令后,生成应用jar,用压缩文件查看,maven自动生成了MANIFEST.MF文件:

Manifest-Version: 1.0
Implementation-Title: github-demo
Implementation-Version: 2.1-SNAPSHOT
Start-Class: com.gitlab.example.demo.DemoApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.2.1.RELEASE
Created-By: Maven Archiver 3.4.0
Main-Class: org.springframework.boot.loader.JarLauncher

 依赖的jar也在应用jar 中,位于BOOT-INF/lib/目录下。使用java -jar 即可运行这个应用jar包。

       为了提高部署的效率,需要把应用jar包跟依赖jar包分开,当项目中没有新增依赖的jar包时,只需把应用单独打包出来就行,这样应用jar包的大小会小很多。

       这需要maven-jar-plugin插件,改变默认的打包方式;还需要maven-dependency-plugin,把依赖的jar包打包。pom文件主要部分如下:

    
        
            
            
                org.apache.maven.plugins
                maven-jar-plugin
                
                    
                        
                            true
                            
                            lib/
                        
                    
                
            
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            nothing
                            nothing
                        
                    
                
            
            
            
                org.apache.maven.plugins
                maven-dependency-plugin
                
                    
                        copy-dependencies
                        prepare-package
                        
                            copy-dependencies
                        
                        
                            ${project.build.directory}/lib
                        
                    
                
            
        
    

          目的就是为了把依赖的jar包放到target/lib目录下,在MANIFEST.MF文件中自动添加Class-Path属性,指向所依赖的jar包:

Manifest-Version: 1.0
Implementation-Title: github-demo
Implementation-Version: 2.4-SNAPSHOT
Start-Class: com.gitlab.example.demo.DemoApplication
Spring-Boot-Classes: BOOT-INF/classes/
Class-Path: lib/spring-boot-starter-web-2.2.1.RELEASE.jar lib/spring-b
 oot-starter-2.2.1.RELEASE.jar lib/spring-boot-2.2.1.RELEASE.jar lib/s
 pring-boot-autoconfigure-2.2.1.RELEASE.jar lib/spring-boot-starter-lo
 gging-2.2.1.RELEASE.jar lib/logback-classic-1.2.3.jar lib/logback-cor
 e-1.2.3.jar lib/log4j-to-slf4j-2.12.1.jar lib/log4j-api-2.12.1.jar li
 b/jul-to-slf4j-1.7.29.jar lib/jakarta.annotation-api-1.3.5.jar lib/sn
 akeyaml-1.25.jar lib/spring-boot-starter-json-2.2.1.RELEASE.jar lib/j
 ackson-databind-2.10.0.jar lib/jackson-annotations-2.10.0.jar lib/jac
 kson-core-2.10.0.jar lib/jackson-datatype-jdk8-2.10.0.jar lib/jackson
 -datatype-jsr310-2.10.0.jar lib/jackson-module-parameter-names-2.10.0
 .jar lib/spring-boot-starter-tomcat-2.2.1.RELEASE.jar lib/tomcat-embe
 d-core-9.0.27.jar lib/tomcat-embed-el-9.0.27.jar lib/tomcat-embed-web
 socket-9.0.27.jar lib/spring-boot-starter-validation-2.2.1.RELEASE.ja
 r lib/jakarta.validation-api-2.0.1.jar lib/hibernate-validator-6.0.18
 .Final.jar lib/jboss-logging-3.4.1.Final.jar lib/classmate-1.5.1.jar 
 lib/spring-web-5.2.1.RELEASE.jar lib/spring-beans-5.2.1.RELEASE.jar l
 ib/spring-webmvc-5.2.1.RELEASE.jar lib/spring-aop-5.2.1.RELEASE.jar l
 ib/spring-context-5.2.1.RELEASE.jar lib/spring-expression-5.2.1.RELEA
 SE.jar lib/slf4j-api-1.7.29.jar lib/spring-core-5.2.1.RELEASE.jar lib
 /spring-jcl-5.2.1.RELEASE.jar
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.2.1.RELEASE
Created-By: Maven Archiver 3.4.0
Main-Class: org.springframework.boot.loader.JarLauncher

这样,当打出来的应用jar包,跟依赖jar包的lib文件夹放在同一个目录下时,使用java -jar就可以运行应用jar包了。

你可能感兴趣的:(自动化部署)