美文网首页Spring我爱编程
ant+maven一键打包springboot上传服务器发布

ant+maven一键打包springboot上传服务器发布

作者: 邵云涛 | 来源:发表于2018-04-14 22:50 被阅读60次
告诉大家,Ant + Maven 非一般的感觉..

在网上找了很多资料一直不齐全,最后借鉴前人经验整理如下

简介:

Maven项目,一个父Maven项目,2个子Maven模块
父Mavenpom可以通过一起打包2个子模块,2个子模块也可以单独打包。
**父Maven项目:
springcloudmanagement
**2个子Maven模块:
springcloudmanagement.service.A
springcloudmanagement.webapp.B

功能:

将Ant脚本放在父Maven项目下,和pom文件同级目录(记得好像是同级目录),ant读取pom文件配置之后,遍历2个子maven模块,ant通过调用maven命令来完成清理,编译,打包操作,然后将jar包上传到远程的linux服务器上,关闭服务,删除临时目录,然后重启服务。

Action:
上传需要第三方的scp ftp jar(jsch-0.1.42.jar)文件
ant调maven插件(maven-ant-tasks-2.1.3.jar)
原生打包方式(spring-boot-loader-1.4.1.RELEASE.jar)

<target name="build"  description="make jar file">
        <!--jar操作,jarfile指定jar包存放路径,basedir为编译后的class的目录-->
        <jar jarfile="${jarfilename}" basedir="target/classes" compress="false">
            <!--为jar包指定manifest,当然,如果jar包不需要打成runnable的形式,manifest可以不要-->
            <!--<fileset dir="target/classes"  includes="**/*"/>-->
            <zipfileset src="lib/spring-boot-loader-1.4.1.RELEASE.jar" />
            <manifest>
                <!--指定main-class-->
                <attribute name="Main-Class" value="org.springframework.boot.loader.JarLauncher"/>
                <!--指定Class-Path-->
                <attribute name="Start-Class" value="${main.class}" />
            </manifest>
        </jar>
    </target>

项目结构如下图:

image.png

配置文件build.properties :

#set environment path
MAVEN_HOME=F:/shaoytsh_data/For_Java/apache-maven-3.3.9
M2_REPO=E:/JAVA/maven
ANT_HOME=F:/Program Files/apache-ant-1.9.6
#upload jar config  
SERVER=10.0.31.224  
PORT=21  
USERNAME=root  
PASSWORD=sugon123  
#set operate dirs
GV_DIR=/opt/gridview  
GV_BUNDLES_DIR=/usr/realse/test/  
DEST_DIR=${GV_DIR}/application/  
#DEST_DIR=${GV_DIR}/core_platform/
#DEST_DIR=${GV_DIR}/lazystart/
RM_ROOT_RUNNER_DIR=${GV_DIR}/runner  
RM_BIN_RUNNER_DIR=${GV_DIR}/bin/runner  
SUB_POM_DIRS=shaoytsh_springCloud,shaoytsh-antPackage-Service,
#set service commands
CMD_SERVICE_STOP=service gridview_platform stop  
CMD_SERVICE_START=service gridview_platform start  
CMD_SERVIEC_RESTART=service gridview_platform restart   

Ant脚本build.xml:

 <?xml version="1.0" encoding="utf-8" ?>  
  <project name="sysmanagement" default="restart_service" basedir="."   
  xmlns:artifact="uri:org.apache.maven.artifact.ant">  
  <description>Sysmanagement logaduit</description>  
  <!--定义外部属性文件和目录-->  
  <property file="build.properties" />  
  <property name="temp_dir" value="temp" />  
  <property name="copy_dir" value="target" />  
  <!--输出配置信息-->  
  <target name="show_property" description="show build.properties conf">  
  <echo> ================== build.properties ================ </echo>  
  <echo>PROJECT_NAME=${ant.project.name}</echo>  
  <echo>GV_DIR=${GV_DIR}</echo>  
  <echo>DEST_DIR=${DEST_DIR}</echo>  
  <echo>RM_ROOT_RUNNER_DIR=${RM_ROOT_RUNNER_DIR}</echo>  
  <echo>RM_BIN_RUNNER_DIR=${RM_BIN_RUNNER_DIR}</echo>  
  </target>  

  <!-- Ant调用Maven-->  
<!-- 自由的使用maven的各种命令了,比如clean install,记住这里的arg一次只能写一个,不能写到一起去;
另外,fork=true一定要加上,由于这个artifact:mvn这个task是集成java task,
因而fork="true"表示在一个新的jvm中去执行maven任务,
之前的折腾了的问题就是因为没有这句,导致build成功后出现一个权限异常。-->
  <target name="init_mvn" description="init mvn">  
  <path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar" />  
  <typedef resource="org/apache/maven/artifact/ant/antlib.xml"  
  uri="uri:org.apache.maven.artifact.ant"  
  classpathref="maven-ant-tasks.classpath" />  
  <echo>init mvn finished..</echo>  
  </target>  
  <!--调用maven的defalut生命周期的install阶段-->  
  <target name="clean" description="mvn clean" depends="init_mvn">  
  <artifact:mvn mavenHome="${MAVEN_HOME}" fork="true">  
<!--加入jvmarg  为了防止可能出现编译和打包失败的可能性 -->
<jvmarg value="-Dmaven.multiModuleProjectDirectory=${M2_REPO}"/>
  <arg value="install"/>  
  </artifact:mvn>  
  <echo>mvn clean finished..</echo>  
  </target>  
  <!-- 调用maven的defalut生命周期的package阶段-- -->  
  <target name="package" description="mvn package" depends="init_mvn, clean">  
  <artifact:mvn mavenHome="${MAVEN_HOME}" fork="true">  
<jvmarg value="-Dmaven.multiModuleProjectDirectory=${M2_REPO}"/>
  <arg value="package" />  
  </artifact:mvn>  
  <echo>mvn package finished..</echo>  
  </target>  
 <target name="install" description="mvn install" depends="init_mvn, clean, package">  
  <artifact:mvn mavenHome="${MAVEN_HOME}" fork="true">  
<jvmarg value="-Dmaven.multiModuleProjectDirectory=${M2_REPO} -Dmaven.main.skip=true"/>
  <arg value="install"/>  
  </artifact:mvn>  
  <echo>mvn install finished..</echo>  
  </target>  
  <!-- 打印maven的模块聚合模块信息,并遍历子模块下的所有目录,调用loop_copy_jar这个Task -->  
  <target name="show_modules" depends="install">  
  <taskdef resource="net/sf/antcontrib/antlib.xml"/>  
  <!-- 引入父pom文件,读取pom文件的属性 -->  
  <artifact:pom id="mypom" file="pom.xml" />  
  <echo>The build directory is ${mypom.build.directory}</echo>  
  <echo>${mypom.modules}</echo>  
  <mkdir dir="${temp_dir}"/>  
  <foreach list="${SUB_POM_DIRS}" target="loop_copy_jars" param="var" />  
  </target>  
  <!-- 拷贝文件 -->  
  <target name="loop_copy_jars" description="copy jars to local dir"  depends="install">  
  <echo message="var = ${var}"/>  
  <echo message="dir = ${var}/${copy_dir}" />  
  <copy todir="${temp_dir}" includeemptydirs="false" overwrite="true">  
  <fileset dir="${var}/${copy_dir}">  
  <exclude name="*sources.jar"/>  
  <exclude name="classes/"/>  
  </fileset>  
  </copy>  
</target>  
<!-- 接着scp ftp上传到linux的指定目录-->
 <target name="ftp_upload" description="upload jars to remote dir" 
depends="shutdown_service, delete_runner_dir">  
 <echo message="begin upload jars to remote server..."/>  
<scp todir="${USERNAME}:${PASSWORD}@${SERVER}:${GV_BUNDLES_DIR}" trust="true">  
  <fileset dir="${temp_dir}">  
 <include name="*.jar"/>  
  </fileset>  
 </scp>  
</target>  
 <!-- 执行shell命令,关闭服务-->  
<target name="shutdown_service" description="shutdown service" depends="show_modules">  
 <echo message="begin shutdown service..." />  
<sshexec host="${SERVER}" username="${USERNAME}" password="${PASSWORD}"   
  command="${CMD_SERVICE_STOP}" trust="true"/>  
 <echo message="shutdown service finished..." />  
<echo message="delete ${RM_BIN_RUNNER_DIR} and ${RM_ROOT_RUNNER_DIR} dirs from remote server" />  
 </target>  
 <!-- 执行shell命令,删除临时目录 -->  
<target name="delete_runner_dir" description="delete runner dirs" depends="shutdown_service">  
<echo message="begin delete ${RM_BIN_RUNNER_DIR} and ${RM_ROOT_RUNNER_DIR} dirs from remote server..." />  
<sshexec host="${SERVER}" username="${USERNAME}" password="${PASSWORD}"   
 command="rm -fr ${RM_BIN_RUNNER_DIR}" trust="true"/>  
 <sshexec host="${SERVER}" username="${USERNAME}" password="${PASSWORD}"   
 command="rm -fr ${RM_ROOT_RUNNER_DIR}" trust="true"/>  
  <echo message="delete runner dirs finished..." />  
 </target>  
<!-- 执行shell命令,重启服务-->  
<target name="restart_service" description="restart service" depends="ftp_upload">
 <echo message="begin restart service..." />  
<sshexec host="${SERVER}" username="${USERNAME}" password="${PASSWORD}"   
command="${CMD_SERVIEC_RESTART}" trust="true"/>  
 <echo message="restart service finished..." />  
 <delete dir="${temp_dir}" />  
 <echo message="delete local temp dir ${temp_dir} finished..." />  
 </target>  
</project>

有疑惑,请点击这里联系我: shyttsyj@126.com

相关文章

网友评论

    本文标题:ant+maven一键打包springboot上传服务器发布

    本文链接:https://www.haomeiwen.com/subject/kterkftx.html