先赞后看,此生必赚
前言
maven打完jar包之后,默认放置位置是target目录
有时候项目需要,如何将jar包放置到指定的目录呢?
方式一:通过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><
<execution>是可执行命令,可以修改maven的命令执行过程,下面的两个execution是修改了install和clean;
<echo>是打印命令;
<mkdir>是创建文件夹命令;(文件夹里面没有东西时好像不会创建出来)
<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设置存放路径
这种方式没有通过插件,而是直接配置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>
参考文献:
1.Referencing the Maven Classpaths
2.利用Maven插件将依赖包、jar/war包及配置文件输出到指定目录
3.maven-antrun-plugin的使用
4.centos7 搭建nexus 仓库配置和maven发布jar包到仓库