透過maven-jar-plugin指定outputDirectory輸出路徑
可以排除某些設定文件,沒有資料夾的話會自動建立!
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <!-- 指定打包的jar包输出路径 --> <outputDirectory>D:\test</outputDirectory> <!--不打入jar包的文件类型或者路径 --> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> <exclude>**/*.yml</exclude> <exclude>static/**</exclude> <exclude>templates/**</exclude> </excludes> </configuration> </plugin>
透過maven-resources-plugin指定outputDirectory輸出路徑
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <encoding>UTF-8</encoding> <!--打成jar包后复制到的路径 --> <outputDirectory> D:\test1 </outputDirectory> <resources> <resource> <!--项目中的路径 --> <directory>src/main/resources/</directory> </resource> </resources> </configuration> </execution> <!--可配置多个提取复制路径只需要 “<id>”名字不一样即可 --> <execution> <id>copy-bulid</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <encoding>UTF-8</encoding> <outputDirectory> D:\test2 </outputDirectory> <resources> <resource> <directory>target</directory> </resource> </resources> </configuration> </execution> </executions> </plugin>
透過maven-antrun-plugin複製jar包
Maven已成為Java 工業領域事實上的建置標準,但在某些情況下,如果可以用Ant指令,還是很方便的。
借助maven-antrun-plugin 插件,可以在Maven執行時,額外執行Ant腳本如下列配置所示:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <id>install</id> <phase>install</phase> <configuration> <target> <echo message="*******************install*******************" /> <mkdir dir="${basedir}/target/classes" /> <copy todir="../target/commons" overwrite="true"> <fileset dir="${project.build.directory}" erroronmissingdir="false"> <include name="*.jar" /> </fileset> </copy> <move file="${project.build.directory}/xxxxxxx.jar" tofile="${project.build.directory}/xxx.jar" /> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> <execution> <id>clean</id> <phase>clean</phase> <configuration> <target> <echo message="*******************clean*******************" /> <delete dir="target" /> <mkdir dir="${basedir}/target/classes" /> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
c94ac33d58052ea4ba2b719493f112d4是可執行命令,可以修改maven的指令執行過程,下面的兩個execution是修改了install和clean;
8a8567de266ae3e91153035db7759a7c是列印指令;
b4d0f8a693a26aba7702f1d3a4b09dcb是建立資料夾指令;(資料夾裡面沒有東西時好像不會創建出來)
797d7b8e63f3fc0bd7a5dd27123b70d6是複製命令,其中todir是目標文件夾,overwrite是覆蓋舊文件,a1aaab7cce83a48dcd054c43d472d62a是源文件, dcf91641426a34cf32ecc36140f28baf是包含jar套件;
cf33f54f92efff0f86f9ed9ee073eee9是移動檔案或修改名稱指令
5cc62b85a20462d19109e58cc4ad0bf9是刪除指令;
#${basedir}
${project.build.directory}
${project.build.finalName} 指的是jar套件前綴名稱
方式四
#如下列設定所示:
將build.xml放到專案根路徑下,使用051b9ea6ed734e760f40cebe431f3387嵌入build.xml檔即可。<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <id>install</id> <phase>install</phase> <configuration> <target> <property name="compile_classpath" refid="maven.compile.classpath" /> <property name="runtime_classpath" refid="maven.runtime.classpath" /> <property name="test_classpath" refid="maven.test.classpath" /> <property name="plugin_classpath" refid="maven.plugin.classpath" /> <ant antfile="${basedir}/build.xml"> <target name="test" /> </ant> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>方式五
使用distributionManagement設定存放路徑
這種方式沒有通過插件,而是直接配置distributionManagement######使用deploy指令可以部署到目標資料夾,沒有資料夾的話會自動建立! ###<distributionManagement> <repository> <id>localRepository</id> <url>file:D:/testRepository</url> </repository> </distributionManagement>###擴充功能:使用maven-dependency-plugin 外掛程式將依賴套件匯出到指定資料夾######這種方式是將依賴套件輸出到指定路徑###
<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>D:\test2</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <includeScope>runtime</includeScope> </configuration> </execution> </executions> </plugin>
以上是Java之怎麼實作maven打完jar包之後將jar包放到指定位置的詳細內容。更多資訊請關注PHP中文網其他相關文章!