Java之maven打完jar包之后將jar包放到指定位置匯總
前言
maven打完jar包之后,默認放置位置是target目錄
有時候項目需要,如何將jar包放置到指定的目錄呢?
方式一
通過maven-jar-plugin指定outputDirectory輸出路徑
可以排除某些配置文件,沒有文件夾的話會自動創(chuàng)建!
<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已經(jīng)成為Java 工業(yè)領(lǐng)域事實上的構(gòu)建標準,但在某些情況下,如果可以用Ant命令,還是很方便的。
借助 maven-antrun-plugin 插件,可以在Maven執(zhí)行時,額外執(zhí)行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>
<execution>是可執(zhí)行命令,可以修改maven的命令執(zhí)行過程,下面的兩個execution是修改了install和clean;
<echo>是打印命令;
<mkdir>是創(chuàng)建文件夾命令;(文件夾里面沒有東西時好像不會創(chuàng)建出來)
<copy>是復制命令,其中todir是目標文件夾,overwrite是覆蓋舊文件,<fileset dir="xxxx">是源文件,<include>是包含jar包;
<move>是移動文件或者修改名稱命令
<delete>是刪除命令;
${basedir}
指的是 項目根路徑${project.build.directory}
指的是 target所在目錄${project.build.finalName}
指的是 jar包前綴名
方式四
通過maven-antrun-plugin嵌入build.xml文件
如下列配置所示:
將build.xml放到項目根路徑下,使用<ant antfile="${basedir}/build.xml">嵌入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設(shè)置存放路徑
這種方式?jīng)]有通過插件,而是直接配置distributionManagement
使用deploy命令可以部署到目標文件夾,沒有文件夾的話會自動創(chuàng)建!
<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>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring MVC入門_動力節(jié)點Java學院整理
這篇文章主要介紹了Spring MVC入門,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08MyBatis學習筆記(二)之關(guān)聯(lián)關(guān)系
這篇文章主要介紹了MyBatis學習筆記(二)之關(guān)聯(lián)關(guān)系 的相關(guān)資料,需要的朋友可以參考下2016-02-02