通过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是覆盖旧文件,58716fea5ed9391026c51555cee2ff97是源文件,dcf91641426a34cf32ecc36140f28baf是包含jar包;
cf33f54f92efff0f86f9ed9ee073eee9是移动文件或者修改名称命令
5cc62b85a20462d19109e58cc4ad0bf9是删除命令;
${basedir}
指的是 项目根路径
${project.build.directory}
指的是 target所在目录
${project.build.finalName}
指的是 jar包前缀名
通过maven-antrun-plugin嵌入build.xml文件
如下列配置所示:
将build.xml放到项目根路径下,使用830f40901e8abad11bc27c19f7f1744a嵌入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中文网其他相关文章!