您好,登錄后才能下訂單哦!
這篇文章運(yùn)用簡單易懂的例子給大家介紹maven 使用assembly 如何實(shí)現(xiàn)打包,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1. pom 中添加assembly 插件
要使用assembly 進(jìn)項(xiàng)編譯打包, 首先主要在pom 中的build中添加插件信息, 具體如圖下所示:
<build> <finalName>${project.artifactId}</finalName> <sourceDirectory>src/main/java</sourceDirectory> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <directory>${profile.dir}</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!-- compiler插件參數(shù)設(shè)置,指定編碼 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> <!-- 這個插件是關(guān)鍵 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!-- 這個是assembly 所在位置 --> <descriptor>src/main/assembly/assembly.xml</descriptor> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2. 創(chuàng)建assembly文件夾和assembly.xml文件
創(chuàng)建assembly文件夾和assembly.xml文件, 這個樣子創(chuàng)建主要是規(guī)范。
在pom 中已經(jīng)介紹assembly.xml 位置。
<!-- 這個是assembly 所在位置 --> <descriptor>src/main/assembly/assembly.xml</descriptor>
創(chuàng)建assembly.xml 文件后添加如下內(nèi)容:
<assembly> <formats> <!--支持 zip,tar,tar.gz,tar.bz2,jar,dir,war 等 --> <format>tar.gz</format> <format>zip</format> <format>dir</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>src/main/resources</directory> <outputDirectory>conf</outputDirectory> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>${profile.dir}</directory> <outputDirectory>conf</outputDirectory> <!-- 表示的是包含下面格式的資源文件 --> <includes> <include>*.xml</include> <include>*.properties</include> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>src/main/assembly/bin</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> </assembly>
fileMode 官方解釋:
Similar to a UNIX permission, sets the file mode of the files included. THIS IS AN OCTAL VALUE. Format: (User)(Group)(Other) where each component is a sum of Read = 4, Write = 2, and Execute = 1. For example, the value 0644 translates to User read-write, Group and Other
上述的三個fileSet 分別是將resource 下的資源打包到config 目錄下, 將assembly下的bin 啟動相關(guān)腳本打包到bin 目錄下, 將maven項(xiàng)目依賴的所有jar 包, 打包到lib 中。
具體結(jié)構(gòu)如下圖所示:
參考地址:
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
zip.xml 文件配置如下
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>release</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}\src\main\config</directory> <!-- 過濾 --> <excludes> <exclude>*.xml</exclude> </excludes> <outputDirectory>\</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory><!-- 將scope為runtime的依賴包打包到lib目錄下。 --> <scope>runtime</scope> </dependencySet> </dependencySets> </assembly>
例:
<assembly> <id>assembly</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}/resources</directory> <outputDirectory>resources</outputDirectory> <fileMode>0755</fileMode> </fileSet> <fileSet> <directory>${project.build.directory}/config</directory> <outputDirectory>config</outputDirectory> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>${project.build.directory}/bin</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> </assembly>
關(guān)于maven 使用assembly 如何實(shí)現(xiàn)打包就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。