SpringCloud maven-assembly-plugin 多級目錄打包的實現(xiàn)
1、spring-boot-maven-plugin
springboot默認(rèn)打包工具為spring-boot-maven-plugin
pom配置:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.gsafety.bg.enterprise.EnterpriseApplication</mainClass> <layout>ZIP</layout> <!-- 打增量包時需要includes部分, 要打全量包刪除includes --> <includes> <include> <groupId>com.gsafety.bg</groupId> <artifactId>enterprise-controller</artifactId> </include> <include> <groupId>com.gsafety.bg</groupId> <artifactId>enterprise-service</artifactId> </include> <include> <groupId>com.gsafety.bg</groupId> <artifactId>enterprise-dao</artifactId> </include> </includes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
打包后的目錄結(jié)構(gòu):
BOOT-INF內(nèi)包含目錄:lib(enterprise-service-1.0.0.jar、enterprise-dao-1.0.0.jar、enterprise-controller-1.0.0.jar)、classes、classpath.idx
2、maven-assembly-plugin
maven-assembly-plugin 插件的主要作用是允許用戶將項目輸出與它的依賴項、模塊、站點文檔、和其他文件一起組裝成一個可分發(fā)的歸檔文件,簡單的說,就是自定義打包的工具,有自己的配置文件(Assembly描述符文件)。微服務(wù)使用這個插件的概率比較高,平時普通的項目不需要這樣的實現(xiàn)方式。
pom配置:
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <configuration> <finalName>enterprise</finalName> <encoding>utf-8</encoding> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
assembly.xml
全部可設(shè)置節(jié)點可參考官網(wǎng):http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
<assembly> <id>1.0</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <!-- 項目所需lib包--> <!-- <dependencySets>--> <!-- <!–把依賴都打包進(jìn)libs文件夾–>--> <!-- <dependencySet>--> <!-- <useProjectArtifact>true</useProjectArtifact>--> <!-- <outputDirectory>libs</outputDirectory>--> <!-- <scope>runtime</scope>--> <!-- </dependencySet>--> <!-- </dependencySets>--> <fileSets> <!--打包啟動文件到deploy目錄--> <fileSet> <!--需要打包的文件所在目錄 即start.sh--> <directory>src/main/assembly/bin</directory> <!--要打包到的地址--> <outputDirectory>deploy</outputDirectory> <!--linux權(quán)限--> <fileMode>0755</fileMode> </fileSet> <!--打包可執(zhí)行jar到application-server目錄--> <fileSet> <directory>target</directory> <outputDirectory>application-server</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <!--打包src/main/resources到logs/enterprise目錄--> <fileSet> <directory>src/main/resources</directory> <outputDirectory>logs/enterprise</outputDirectory> <fileMode>0755</fileMode> <!--打包不包含src/main/resources所有文件,即生成logs/enterprise空目錄--> <excludes> <exclude>**/*</exclude> </excludes> </fileSet> </fileSets> </assembly>
打包后的目錄結(jié)構(gòu):
查看application-server文件夾內(nèi)可執(zhí)行文件解壓目錄:
發(fā)現(xiàn)與spring-boot-maven-plugin打包后的目錄不一致,明顯缺少lib內(nèi)的三個jar和其他一些文件
3、maven-assembly-plugin打包后的可執(zhí)行文件缺失lib問題
修改pom文件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.gsafety.bg.enterprise.EnterpriseApplication</mainClass> <layout>ZIP</layout> <!-- 打增量包時需要includes部分, 要打全量包刪除includes --> <includes> <include> <groupId>com.gsafety.bg</groupId> <artifactId>enterprise-controller</artifactId> </include> <include> <groupId>com.gsafety.bg</groupId> <artifactId>enterprise-service</artifactId> </include> <include> <groupId>com.gsafety.bg</groupId> <artifactId>enterprise-dao</artifactId> </include> </includes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <!-- 生成項目依賴到lib本地,并設(shè)置需要排除的jar--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>../../../lib</outputDirectory> <!-- 需要排除的jar的 groupId --> <excludeArtifactIds> enterprise-controller,enterprise-service,enterprise-dao </excludeArtifactIds> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <configuration> <finalName>enterprise</finalName> <encoding>utf-8</encoding> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
即plugins先引用spring-boot-maven-plugin 后引用maven-assembly-plugin,這樣spring-boot-maven-plugin會將enterprise-service-1.0.0.jar、enterprise-dao-1.0.0.jar、enterprise-controller-1.0.0.jar三個jar打包到lib中,打包后maven-assembly-plugin就會將其打包進(jìn)enterprise-1.0.tar.gz。
這樣enterprise-1.0.tar.gz內(nèi)就包含了啟動文件(deploy)、可執(zhí)行文件(application-server/enterprise-main-1.0.0.jar)、日志目錄(logs/enterprise),符合目前項目部署的目錄結(jié)構(gòu)。
到此這篇關(guān)于SpringCloud maven-assembly-plugin 多級目錄打包的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringCloud maven-assembly-plugin 多級目錄打包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何將復(fù)雜SQL轉(zhuǎn)換成Java對象的實例講解
轉(zhuǎn)換復(fù)雜SQL到Java代碼,我們需要確定數(shù)據(jù)庫連接方式和工具,使用JDBC的API來連接數(shù)據(jù)庫、執(zhí)行SQL語句,復(fù)雜SQL語句可以被拆分為多個步驟,每個步驟執(zhí)行一個特定的操作,通過將SQL語句拆分為多個步驟,我們可以更好地理解復(fù)雜SQL的邏輯,并且更容易將其轉(zhuǎn)換為Java代碼2024-05-05Zuul 實現(xiàn)網(wǎng)關(guān)轉(zhuǎn)發(fā)的五種方式小結(jié)
這篇文章主要介紹了Zuul 實現(xiàn)網(wǎng)關(guān)轉(zhuǎn)發(fā)的五種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java集合Map常見問題_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)整理了Java集合Map常見問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05springboot如何查找配置文件路徑的順序和其優(yōu)先級別
此文是在工作中遇到的關(guān)于springboot配置文件的問題,在網(wǎng)上查閱資料和自己測試之后記錄的,以便日后查閱。希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Java數(shù)據(jù)結(jié)構(gòu)之插入排序與希爾排序
在本篇文章,我們將為小伙伴們進(jìn)行排序概念的基本講解并具體講解其中的兩種基礎(chǔ)排序:插入排序和希爾排序,希望小伙伴們能夠從中有所收獲2023-04-04SpringBoot使用jasypt加解密密碼的實現(xiàn)方法(二)
這篇文章主要介紹了SpringBoot使用jasypt加解密密碼的實現(xiàn)方法(二),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10