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

1、聚合项目父级POM配置(非聚合项目直接配置到POM即可)

    <properties>
        
        <copy>truecopy>
        <localDir>E:/sk-cloud-jarlocalDir>
        <uploadToRemoteDir>falseuploadToRemoteDir>
        <remoteDir>/home/mobileremoteDir>
        <remoteIp>192.168.0.15remoteIp>
        <remoteUser>rootremoteUser>
        <remotePassword>123456remotePassword>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>ant-contribgroupId>
            <artifactId>ant-contribartifactId>
            <version>1.0b3version>
        dependency>
        
        <dependency>
            <groupId>org.apache.antgroupId>
            <artifactId>ant-jschartifactId>
            <version>1.10.7version>
        dependency>
    dependencies>

2、在module项目中pom中配置

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>

			
			<plugin>
				<artifactId>maven-antrun-pluginartifactId>
				<executions>
					<execution>
						<id>copyid>
						<phase>packagephase>
						<configuration>
							<tasks>
								<echo message="start.................................."/>
								<echo message="load maven plugin ant-contrib-1.0b3"/>
								
								<taskdef resource="net/sf/antcontrib/antlib.xml">
									<classpath>
										<pathelement location="${settings.localRepository}/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
									classpath>
								taskdef>
								
								<if>
									<equals arg1="${copy}" arg2="true"/>
									<then>
										<echo message="Copy jar to your desired path."/>
										<copy todir="${localDir}" overwrite="true">
											<fileset dir="${project.build.directory}">
												<include name="*.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>
								
								
								<if>
									<equals arg1="${project.packaging}" arg2="pom"/> 
									<then>
										<echo message="delete ${project.build.directory}"/>
										<delete dir="${project.build.directory}"/>
									then>
								if>
								
								<if>
									<equals arg1="${uploadToRemoteDir}" arg2="true"/>
									<then>
										<scp file="${project.build.directory}\${project.build.finalName}.jar"
											 todir="${remoteUser}:${remotePassword}@${remoteIp}:${remoteDir}" trust="true"/>
										
										<sshexec host="${remoteIp}"
												 username="${remoteUser}"
												 password="${remotePassword}"
												 command="ls"
												 trust="true"/>
										<echo message="end.................................."/>
									then>
								if>
							tasks>
						configuration>
						<goals>
							<goal>rungoal>
						goals>
					execution>
				executions>
			plugin>
		plugins>
	build>

3、最后

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

你可能感兴趣的:(maven,打包,springboot打包,springcloud打包)