这是 maven 的方式,通过maven插件实现 jar 包复制到指定目录。
在最外层 pom 文件下定义变量
<properties>
......
<copy.jar.directory>D:/jar-output</copy.jar.directory>
</properties>
在对应服务的 pom 文件下添加 plugin。
<build>
<plugins>
<!-- 把jar包拷贝到指定目录位置 -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<configuration>
<target>
<copy todir="${copy.jar.directory}">
<fileset dir="${project.build.directory}">
<include name="${project.artifactId}.jar" />
</fileset>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
网上常见的是在 configuration 中使用 tasks 标签,但是在新版本中 tasks 已经被移除了
You are using 'tasks' which has been removed from the maven-antrun-plugin. Please use 'target' and refer to the >>Major Version Upgrade to version 3.0.0<< on the plugin site.
这里使用了 target 来替代。
也可以不指定路径,直接使用最上级模块的项目位置,需要在子模块中再引入一个插件获取项目根目录位置,并设置为属性 main.basedir
。
<!-- 获取项目根目录位置 -->
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
<version>0.1</version>
<executions>
<execution>
<id>directories</id>
<goals>
<goal>highest-basedir</goal>
</goals>
<phase>initialize</phase>
<configuration>
<property>main.basedir</property>
</configuration>
</execution>
</executions>
</plugin>
<!-- 把jar包拷贝到指定目录位置 -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<configuration>
<target>
<copy todir="${main.basedir}/jar-output">
<fileset dir="${project.build.directory}">
<include name="${project.artifactId}.jar" />
</fileset>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>