去年项目写的Ant脚本,还是花了很多时间。。说明Ant用的还不熟练啊。。
这个例子告诉大家,Ant+Maven,非一般的感觉..
简介:
Maven项目,一个父Maven项目,2个聚合的Maven模块
sysmanagement.service.logaudit,sysmanagement.webapp.logaudit
父Mavenpom可以通过聚合打包2个子模块,2个子模块也可以单独打包。
父Maven项目:sysmanagement
2个被聚合的Maven模块:sysmanagement.service.logaudit,sysmanagement.webapp.logaudit
功能:将Ant脚本放在父Maven项目下,和pom文件同级目录(记得好像是同级目录),ant读取pom文件配置之后,遍历2个子maven模块,ant通过调用maven命令来完成清理,编译,打包操作,然后将jar包上传到远程的linux服务器上,关闭服务,删除临时目录,然后重启服务。
注意:上传需要第三放的scp ftp jar(jsch-0.1.49.jar)文件,ant调maven插件(maven-ant-tasks-2.1.3.jar)
配置文件:
#set environment path M2_REPO=E:/MavenRepo ANT_HOME=D:/Developer/apache-ant-1.8.3 MAVEN_HOME=D:/Developer/apache-maven-3.0.4 #upload jar config SERVER=10.0.31.224 PORT=21 USERNAME=root PASSWORD=sugon123 #set operate dirs GV_DIR=/opt/gridview GV_BUNDLES_DIR=/opt/gridview/bundles/ 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=../sysmanagement.service.logaudit,../sysmanagement.webapp.logaudit #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脚本:
<?xml version="1.0" encoding="utf-8" ?> <project name="sysmanagement" default="restart_service" basedir="." xmlns:artifact="antlib: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--> <target name="init_mvn" description="init mvn"> <path id="maven-ant-tasks.classpath" path="D:/maven-ant-tasks-2.1.3.jar" /> <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib: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"> <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"> <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"> <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" > <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>