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目录不存在
因为父级的packaging
是pom
,所有打包的时候并不会生成target目录,如果有哪位大佬解决了怎么配置到父级pom中,请留言
博客参考:https://blog.youkuaiyun.com/qq_33547169/article/details/83059858