Spring boot打包jar分離lib和resources方法實(shí)例
為什么要配置、依賴文件分離:
1.在傳統(tǒng)jar包中,所有文件都打包到一個(gè)jar包中,jar非常臃腫,在進(jìn)行服務(wù)更新部署時(shí)非常不便,而且傳輸不穩(wěn)定時(shí)導(dǎo)致傳輸失敗。如果過(guò)實(shí)行文件分離、在依賴不改變的情況下,僅僅上傳更新后的 編譯文件是非常方便的。
如果要修改一些配置文件:properties、xml,靜態(tài)文件等可以直接在服務(wù)器上編輯。
那么怎么實(shí)行配置、依賴文件分離呢?
插件介紹
- maven-jar-plugin 這個(gè)插件式專門(mén)用來(lái)打包用的,可以配置需要打包進(jìn)去的文件,程序的入口類等。
- maven-resources-plugin 這個(gè)插件是用來(lái)拷貝資源文件的。
- maven-maven-dependency-plugin 這個(gè)插件是用來(lái)拷貝依賴庫(kù)的。
- maven-assembly-plugin 可以說(shuō)包含了以上插件的功能,但是可以做到更精細(xì)的控制。
- spring-boot-maven-plugin 這個(gè)不用說(shuō),springboot 項(xiàng)目最重要的插件,整個(gè)項(xiàng)目的打包處理過(guò)程還是要依附于它。
打包成可執(zhí)行jar,不僅僅局限SpringBoot項(xiàng)目(主入口函數(shù)存在)
maven-jar-plugin 插件打包jar
在pom文件中配置,但是這樣 依賴的jar并不會(huì)打進(jìn)來(lái)(后面會(huì)有解決方法),適用不需要依賴文件的項(xiàng)目。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<!--是否添加依賴-->
<addClasspath>true</addClasspath>
<!--設(shè)置啟動(dòng)類-->
<mainClass>xxx.xxx.Main</mainClass>
</manifest>
</archive>
<!--設(shè)置生成jar輸出位置-->
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
maven-assembly-plugin 插件打包jar
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!--不添加AssemblyId-->
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<!--配置打包的時(shí)候一并打包依賴jar-->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--入口類-->
<mainClass>xxx.xxx.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!--綁定生命周期-->
<phase>package</phase>
<goals>
<!--執(zhí)行assembly -->
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
打包SpringBoot 項(xiàng)目
方案一、
<plugins>
<!--打包jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--MANIFEST.MF 中 Class-Path 加入前綴-->
<classpathPrefix>lib/</classpathPrefix>
<!--jar包不包含唯一版本標(biāo)識(shí)-->
<useUniqueVersions>false</useUniqueVersions>
<!--指定入口類-->
<mainClass>xxx.xxx.Application</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入資源文件目錄-->
<Class-Path>/resources</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}/dis</outputDirectory>
</configuration>
</plugin>
<!--拷貝依賴 copy-dependencies-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/dis/lib/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--拷貝資源文件 copy-resources-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>${project.build.directory}/dis/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--spring boot repackage,依賴 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<!--使用外部配置文件,jar包里沒(méi)有資源文件-->
<addResources>true</addResources>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
方案二
這里依賴assembly.xml 描述文件
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<outputDirectory>${project.build.directory}/dist/</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打包成jar文件,并指定lib文件夾以及resources資源文件夾 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>xxx.xxx.Application</mainClass>
<!--依賴前綴-->
<classpathPrefix>lib/</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Class-Path>resources/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>distribution</id>
<!--輸出格式 zip 最終結(jié)果生成zip -->
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<!--設(shè)置需要輸出文件-->
<fileSets>
<fileSet>
<directory>src/main/resources/</directory>
<outputDirectory>/resources</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<!--依賴包的輸出目錄-->
<outputDirectory>/lib</outputDirectory>
<scope>runtime</scope>
<excludes>
<exclude>${project.groupId}:${project.artifactId}</exclude>
</excludes>
</dependencySet>
<dependencySet>
<!--jar包的輸出目錄-->
<outputDirectory>/</outputDirectory>
<includes>
<include>${project.groupId}:${project.artifactId}</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合ElasticSearch實(shí)踐
本篇文章主要介紹了SpringBoot整合ElasticSearch實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
java實(shí)現(xiàn)的順時(shí)針/逆時(shí)針打印矩陣操作示例
這篇文章主要介紹了java實(shí)現(xiàn)的順時(shí)針/逆時(shí)針打印矩陣操作,涉及java基于數(shù)組的矩陣存儲(chǔ)、遍歷、打印輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
Spring Boot假死診斷實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于Spring Boot假死診斷的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Activiti explorer.war示例工程使用過(guò)程圖解
這篇文章主要介紹了Activiti explorer.war示例工程使用過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

