詳解Spring Boot使用Maven自定義打包方式
前言:本文將告訴你如何將程序Jar與與依賴Jar及配置文件分離打包,以下列舉了兩種不同Maven打包方式,其打包效果一致!
一、第一種Maven打包方式,將jar及resources下全部配置文件,拷貝到指定目錄:
<!--配置項(xiàng)--><properties> <!--自定義配置--> <project.jar.output.directory>E:/IDEAFile/file-copy/target/project</project.jar.output.directory></properties><build> <plugins> <!--項(xiàng)目依賴的jar文件,放置默認(rèn)配置目錄下--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 設(shè)置jar的入口類 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.filecopy.FileCopyApplication</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 使用maven-resources-plugin插件復(fù)制resources目錄下所有文件到指定的路徑--> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/project</outputDirectory> <resources><resource> <directory>src/main/resources</directory> <filtering>true</filtering></resource> </resources> </configuration> </execution> </executions> </plugin> <!--使用maven-antrun-plugin插件將jar復(fù)制到指定的目錄下--> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <!-- 在maven進(jìn)行package的時(shí)候執(zhí)行--> <phase>package</phase> <configuration> <tasks><!--todir:是將要復(fù)制jar包到的地方,overwrite:是否重寫--><copy todir='${project.jar.output.directory}' overwrite='true'> <!--獲取父目錄下的target文件夾中的jar--> <fileset dir='${project.build.directory}'> <include name='*.jar'/> </fileset></copy> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
第二種Maven打包方式使用 assembly插件,將jar及配置文件進(jìn)行壓縮打包到指定目錄:
<plugins> <!-- 項(xiàng)目依賴的jar文件,放置默認(rèn)配置目錄下--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 設(shè)置jar的入口類--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.filecopy.FileCopyApplication</mainClass> </manifest> </archive> </configuration> </plugin> <!--assembly插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <!--指定壓縮包名稱--> <finalName>project</finalName> <!--指定assembly配置文件配置--> <descriptors> <descriptor>/assembly/assembly.xml</descriptor> </descriptors> <!--打包tar.gz輸出target文件夾中--> <outputDirectory>${project.build.directory}</outputDirectory> <appendAssemblyId>false</appendAssemblyId> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins>
assembly文件:
<assembly xmlns='http://maven.apache.org/ASSEMBLY/2.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd'> <id>leaves</id> <formats> <!--壓縮文件形式 可選 zip tar.gz等 --> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <!-- 項(xiàng)目文件處理 --> <fileSets> <!--配置文件輸出位置根目錄文件夾下--> <fileSet> <directory>${basedir}/src/main/resources</directory> <includes> <include>**</include> </includes> <filtered>true</filtered> <outputDirectory>${file.separator}</outputDirectory> </fileSet> <!-- 項(xiàng)目代碼生成的jar文件放在根目錄 --> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>${file.separator}</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets></assembly>
到此這篇關(guān)于Spring Boot使用Maven自定義打包方式的文章就介紹到這了,更多相關(guān)Spring Boot Maven自定義打包內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式2. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))3. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)4. XML入門的常見問題(四)5. HTML5 Canvas繪制圖形從入門到精通6. XML入門的常見問題(一)7. JavaWeb Servlet中url-pattern的使用8. 微信開發(fā) 網(wǎng)頁(yè)授權(quán)獲取用戶基本信息9. XML解析錯(cuò)誤:未組織好 的解決辦法10. asp批量添加修改刪除操作示例代碼
