02.04 Maven 打包生成 Jar 到指定目录
102353 于 2026-10-17 14:14:33 +08:00 修改了此页面
maven默认打包后,jar文件都是生成在各自服务的target下,对微服务的部署不太友好,需要整个项目上传服务器,里面包含了源码,其实我们可以在打包时把所需jar包复制到指定的目录
- 在父级POM配置中加入
<properties>
<!--打包配置-->
<copy>true</copy>
<localDir>C:/joolun-jar</localDir>
......
</properties>
- 在各个module项目中pom中配置
<!--打包时复制jar包到指定文件目录-->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy</id><!--需要唯一-->
<phase>package</phase><!--当执行package操作时执行一下任务-->
<configuration>
<tasks><!--任务-->
<echo message="start.................................."/><!--打印-->
<echo message="load maven plugin ant-contrib-1.0b3"/>
<!--加载plugin ant-contrib的配置文件-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath><!--加载jar包,${settings.localRepository}的值是你maven settings文件中配置的本地仓库位置-->
<pathelement location="${settings.localRepository}/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<!--复制jar包-->
<if>
<equals arg1="${copy}" arg2="true"/><!--是否复制jar包-->
<then>
<echo message="Copy jar to your desired path."/>
<copy todir="${localDir}" overwrite="true"><!--执行复制操作,todir的值是将要复制jar包到的地方,overwrite是否重写-->
<fileset dir="${project.build.directory}"><!--${project.build.directory}值是你的target目录-->
<include name="*.jar"/><!--target目录下的jar包-->
</fileset>
</copy>
</then>
</if>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
- 配完以上代码后mvn clean install,部署所需的jar文件就都在C:/joolun-jar下了