Java之maven打完jar包之后將jar包放到指定位置匯總
前言
maven打完jar包之后,默認(rèn)放置位置是target目錄
有時(shí)候項(xiàng)目需要,如何將jar包放置到指定的目錄呢?
方式一
通過maven-jar-plugin指定outputDirectory輸出路徑
可以排除某些配置文件,沒有文件夾的話會(huì)自動(dòng)創(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包后復(fù)制到的路徑 --> ?? ??? ??? ??? ?<outputDirectory> ?? ??? ??? ??? ??? ?D:\test1 ?? ??? ??? ??? ?</outputDirectory> ?? ??? ??? ??? ?<resources> ?? ??? ??? ??? ??? ?<resource> ?? ??? ??? ??? ??? ??? ?<!--項(xiàng)目中的路徑 --> ?? ??? ??? ??? ??? ??? ?<directory>src/main/resources/</directory> ?? ??? ??? ??? ??? ?</resource> ?? ??? ??? ??? ?</resources> ?? ??? ??? ?</configuration> ?? ??? ?</execution> ?? ??? ?<!--可配置多個(gè)提取復(fù)制路徑只需要 “<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復(fù)制jar包
Maven已經(jīng)成為Java 工業(yè)領(lǐng)域事實(shí)上的構(gòu)建標(biāo)準(zhǔn),但在某些情況下,如果可以用Ant命令,還是很方便的。
借助 maven-antrun-plugin 插件,可以在Maven執(zhí)行時(shí),額外執(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í)行過程,下面的兩個(gè)execution是修改了install和clean;
<echo>是打印命令;
<mkdir>是創(chuàng)建文件夾命令;(文件夾里面沒有東西時(shí)好像不會(huì)創(chuàng)建出來)
<copy>是復(fù)制命令,其中todir是目標(biāo)文件夾,overwrite是覆蓋舊文件,<fileset dir="xxxx">是源文件,<include>是包含jar包;
<move>是移動(dòng)文件或者修改名稱命令
<delete>是刪除命令;
${basedir}
指的是 項(xiàng)目根路徑${project.build.directory}
指的是 target所在目錄${project.build.finalName}
指的是 jar包前綴名
方式四
通過maven-antrun-plugin嵌入build.xml文件
如下列配置所示:
將build.xml放到項(xiàng)目根路徑下,使用<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命令可以部署到目標(biāo)文件夾,沒有文件夾的話會(huì)自動(dòng)創(chuàng)建!
<distributionManagement> ?? ?<repository> ?? ??? ?<id>localRepository</id> ?? ??? ?<url>file:D:/testRepository</url> ?? ?</repository> </distributionManagement>
擴(kuò)展:使用maven-dependency-plugin 插件將依賴包導(dǎo)出到指定文件夾
這種方式是將依賴包輸出到指定路徑
<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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring MVC入門_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Spring MVC入門,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08SpringBoot下如何實(shí)現(xiàn)支付寶接口的使用
這篇文章主要介紹了SpringBoot下如何實(shí)現(xiàn)支付寶接口的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java簡(jiǎn)易登錄注冊(cè)功能實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Java簡(jiǎn)易登錄注冊(cè)功能實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06drools的簡(jiǎn)單入門案例場(chǎng)景分析
drools是一款由JBoss組織提供的基于Java語言開發(fā)的開源規(guī)則引擎,可以將復(fù)雜且多變的業(yè)務(wù)規(guī)則從硬編碼中解放出來,這篇文章主要介紹了drools的簡(jiǎn)單入門案例,需要的朋友可以參考下2022-05-05Java獲取Class對(duì)象的幾種方式小結(jié)
本文給大家分享了Java獲取Class對(duì)象的幾種方式,使用類名.class 語法,使用對(duì)象的 getClass()方法,使用 Class.forName()方法等多種方法,不同的方式適用于不同的場(chǎng)景,需要的朋友可以參考下2023-10-10MyBatis學(xué)習(xí)筆記(二)之關(guān)聯(lián)關(guān)系
這篇文章主要介紹了MyBatis學(xué)習(xí)筆記(二)之關(guān)聯(lián)關(guān)系 的相關(guān)資料,需要的朋友可以參考下2016-02-02