[自动运维]ant脚本打包,上传文件到指定服务器,并部署

时间:2023-03-09 08:35:45
[自动运维]ant脚本打包,上传文件到指定服务器,并部署
1、根节点使用,表示根目录为当前目录,默认启动的target为build,项目名称为othersysm,
    <project basedir="." default="build" name="othersysm">
</project>
2、每一个target为一个执行命令,如果有依赖关系,则写为:
<target depends="build-project" name="build"/>
    表示要运行target,需要先运行build-project,根据此依赖关系,我们可以认为project节点中配置的default就是最后一个运行脚本
3、定义变量和路径,使用定义的变量的方法为${变量名称}
<property name="ibslib.location" value="../ibslib"/><property name="debuglevel" value="source,lines,vars"/><property name="target" value="6"/><property name="source" value="6"/>
3、定义jar包路径,并命名,fileset表示引用外部包,pathelement表示内部
 <path id="othersysm.classpath">
<pathelement path="{classpath}"/>
<fileset dir="${ibslib.location}/lib/logging" includes="**/*.jar,**/*.zip"/>
<pathelement location="bin"/> </classpath>

4、常用操作,新建文件夹使用mkdir,和拷贝文件使用copy

<target name="init">
  <echo>
+=======================================+
| init
+=======================================+
</echo>
  <mkdir dir="test"/>
  <copy includeemptydirs="false" todir="test"><!--忽略空文件夹,并且拷贝到指定目录-->
<fileset dir="src" excludes="**/*.launch, **/*.java"/><!--忽略指定文件-->
  </copy>
</target>

5、编译java文件,编译命令为:

        debuglevel:调试等级
        destdir:编译后的目标文件夹
        source:源文件版本
        target:目标文件版本
        encoding:编码
        includeantruntime :指出是否应在类路径中包括 Ant 运行时程序库,默认为 yes
<target depends="init" name="build-project">
  <echo message="${ant.project.name}: ${ant.file}"/><!-- 打印ant脚本目录 -->
  <javac debug="true" debuglevel="${debuglevel}" destdir="test" source="${source}" target="${target}" encoding="UTF-8" includeantruntime="false">
    <src path="src"/><!-- 源文件目录 -->
    <classpath refid="othersysm.classpath"/><!-- classpath目录 -->
  </javac>
</target>

6、在ant文件中该执行其他文件:

    antfile:需要运行的build文件
    inheritAll 是否共享参数
    target:运行的ant的target
    output:输出的日志文件
    如果将参数传递,可以采用property
<ant antfile="${web.location}/build.xml" inheritAll="false" target="genwar" output="output.log"><property name="currentMode" value="${currentMode}"/><property name="targetMode" value="${targetMode}"/></ant>

7、file文件,表示读取properties文件,可以获取此文件中的属性值

<property file="${basedir}/build.properties"/>
8、替换文件中的指定值
<replaceregexp byline="true">
<regexp pattern="正则表达式"/>
<substitution expression="将要替换的值"/>
<fileset dir="${unpack.war.dir}/WEB-INF" includes="web.xml"/>
</replaceregexp>
9、将文件夹打包为war包
<war destfile="${uibs.location}/web.war" webxml="WebContent/WEB-INF/web.xml"><fileset dir="WebContent" includes="**"/></war>

10、通过scp命令将上传war包文件到指定机器的指定目录

<scp file="${uibs.location}/${server.name}.war" todir="${wls.username}:${wls.password}@${remote.host}:${deploy.location}" trust="true"/>

11、远程ssh,执行服务器命令,执行启动脚本

<sshexec host="${remote.host}"
username="${remote.username}"
password="${remote.password}"
command="ls"
trust="true"/>