SpringCloud使用Maven打包生成jar包到指定目录和上传到远程服务器

1、聚合项目父级POM配置(非聚合项目直接配置到POM即可)
    <properties>
        <!--打包配置-->
        <copy>true</copy>
        <localDir>E:/sk-cloud-jar</localDir>
        <uploadToRemoteDir>false</uploadToRemoteDir>
        <remoteDir>/home/mobile</remoteDir>
        <remoteIp>192.168.0.15</remoteIp>
        <remoteUser>root</remoteUser>
        <remotePassword>123456</remotePassword>
    </properties>

    <dependencies>
        <!--能在pom里面写if判断的依赖-->
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
        </dependency>
        <!--使用scp功能-->
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.10.7</version>
        </dependency>
    </dependencies>
2、在module项目中pom中配置
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<!--复制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>
								<!--打印-->
								<echo message="pom type:${project.packaging}"/>
								<echo message="target path:${project.build.directory}"/>
								<echo message="maven local repository:${settings.localRepository}"/>
								<echo message="if pom type equals pom,delete ant generate target and antrun folder"/>
								<echo message="${project.build.finalName}"></echo>
								<!--
                                因为 maven-antrun-plugin 执行后,会在你的项目中生成一个target/antrun/build-main.xml,
                                在packageing=pom的项目下也会生成一个这样的文件和文件目录,个人觉得很烦,索引引入ant-contrib依赖,
                                如果你觉得不烦,可以不添加ant-contrib依赖,下边的if标签也不能使用.
                                -->
								<!--删除-->
								<if><!--if 标签-->
									<equals arg1="${project.packaging}" arg2="pom"/> <!--判断当前pom文件的packageing是否是pom类型,-->
									<then><!--如果是pom类型则删除 该项目下的target目录-->
										<echo message="delete ${project.build.directory}"/>
										<delete dir="${project.build.directory}"/>
									</then>
								</if>
								<!--上传文件
                                    file: 文件路径或者文件名称
                                        ${project.build.directory} 指向target目录
                                        ${project.build.finalName} 打包名称
                                    todir: 目标服务器ip地址和文件路径
                                -->
								<if>
									<equals arg1="${uploadToRemoteDir}" arg2="true"/>
									<then>
										<scp file="${project.build.directory}\${project.build.finalName}.jar"
											 todir="${remoteUser}:${remotePassword}@${remoteIp}:${remoteDir}" trust="true"/>
										<!--连接虚拟机  file: 文件路径或者文件名称 command 执行命令 -->
										<sshexec host="${remoteIp}"
												 username="${remoteUser}"
												 password="${remotePassword}"
												 command="ls"
												 trust="true"/>
										<echo message="end.................................."/>
									</then>
								</if>
							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
3、最后

执行打包 maven -> package 就可以了
目前在父级pom中配置这个插件会报 父级的项目 target目录不存在 因为父级的packagingpom,所有打包的时候并不会生成target目录,如果有哪位大佬解决了怎么配置到父级pom中,请留言
博客参考:https://blog.csdn.net/qq_33547169/article/details/83059858

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值