使用Maven打包生成jar包到指定目录

maven默认打包后,jar文件都是生成在各自服务的target下,对微服务的部署不太友好,需要整个项目上传服务器,里面包含了源码,其实我们可以在打包时把所需jar包复制到指定的目录

  • 在父级POM配置中加入
    <properties>
        <!--打包配置-->
        <copy>true</copy>
        <localDir>C:/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:/jar下了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值