Java之maven打完jar包之后將jar包放到指定位置匯總
前言
maven打完jar包之后,默認(rèn)放置位置是target目錄
有時(shí)候項(xiàng)目需要,如何將jar包放置到指定的目錄呢?
方式一
通過(guò)maven-jar-plugin指定outputDirectory輸出路徑
可以排除某些配置文件,沒(méi)有文件夾的話會(huì)自動(dòng)創(chuàng)建!
<plugin> ?? ?<groupId>org.apache.maven.plugins</groupId> ?? ?<artifactId>maven-jar-plugin</artifactId> ?? ?<configuration> ?? ??? ?<!-- 指定打包的jar包輸出路徑 --> ?? ??? ?<outputDirectory>D:\test</outputDirectory> ?? ??? ?<!--不打入jar包的文件類(lèi)型或者路徑 --> ?? ??? ?<excludes> ?? ??? ??? ?<exclude>**/*.properties</exclude> ?? ??? ??? ?<exclude>**/*.xml</exclude> ?? ??? ??? ?<exclude>**/*.yml</exclude> ?? ??? ??? ?<exclude>static/**</exclude> ?? ??? ??? ?<exclude>templates/**</exclude> ?? ??? ?</excludes> ?? ?</configuration> </plugin>
方式二
通過(guò)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>
方式三
通過(guò)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í)行過(guò)程,下面的兩個(gè)execution是修改了install和clean;
<echo>是打印命令;
<mkdir>是創(chuàng)建文件夾命令;(文件夾里面沒(méi)有東西時(shí)好像不會(huì)創(chuàng)建出來(lái))
<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包前綴名
方式四
通過(guò)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)]有通過(guò)插件,而是直接配置distributionManagement
使用deploy命令可以部署到目標(biāo)文件夾,沒(méi)有文件夾的話會(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è)參考,也希望大家多多支持腳本之家。
- 避免多個(gè)jar通過(guò)maven打包導(dǎo)致同名配置文件覆蓋沖突問(wèn)題
- Maven打包沒(méi)有指定主類(lèi)問(wèn)題(xxx.jar中沒(méi)有主清單屬性)
- maven引入本地jar包運(yùn)行報(bào)錯(cuò)java.lang.NoClassDefFoundError解決
- maven手動(dòng)上傳jar包示例及圖文步驟過(guò)程
- gradle使用maven-publish發(fā)布jar包上傳到私有maven配置
- Maven項(xiàng)目引用第三方j(luò)ar包找不到類(lèi)ClassNotFoundException
相關(guān)文章
IDEA提示 add *** to custom tags問(wèn)題及解決
文章介紹了如何在文檔注釋中添加自定義注解(@xxx),并提供了添加和刪除注解的方法,總結(jié)了個(gè)人經(jīng)驗(yàn),希望對(duì)大家有所幫助2024-12-12
詳談ThreadLocal-單例模式下高并發(fā)線程安全
這篇文章主要介紹了ThreadLocal-單例模式下高并發(fā)線程安全,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
關(guān)于弱引用WeakReference所引用的對(duì)象的回收規(guī)則
這篇文章主要介紹了關(guān)于弱引用WeakReference所引用的對(duì)象的回收規(guī)則,如果一個(gè)弱引用實(shí)例的成員變量referent引用了一個(gè)對(duì)象obj,那么就稱這個(gè)弱引用實(shí)例對(duì)obj的引用是弱引用,被一個(gè)弱引用實(shí)例引用的對(duì)象,稱為弱引用對(duì)象,需要的朋友可以參考下2023-09-09
SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過(guò)程,SpringBoot響應(yīng)數(shù)據(jù)封裝是指在SpringBoot應(yīng)用程序中,將返回的數(shù)據(jù)進(jìn)行封裝,以便于前端頁(yè)面或其他客戶端使用,感興趣想要詳細(xì)了解可以參考下文2023-05-05
解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題
這篇文章主要介紹了解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
使用ServletInputStream()輸入流讀取圖片方式
這篇文章主要介紹了使用ServletInputStream()輸入流讀取圖片方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
springboot 加載 META-INF/spring.factories方式
這篇文章主要介紹了springboot 加載 META-INF/spring.factories方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

