微服务项目打包时将jar包放到统一目录下

这是 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值