首页 > 持续集成 > ANT(build)脚本模板示例

ANT(build)脚本模板示例

近来虽然Maven构建的项目越来越盛行,但是依然还有很多遗留(暂且称之为遗留吧)项目依然是由ANT编译的,今天老夫就整理一下自己目前公司项目用到的ANT模板,既作为老夫的学习笔记,以供将来查看,也分享出来供需要的参考,因为比较简单易懂,就不多说了,直接上代码


<?xml version="1.0" encoding="UTF-8"?>
<!--
  Ant工程build模板
  ant build file

  Example: ant -Dprofile=dev deploy
  test  IDC测试环境
  dev   本地开发环境 (default)
-->
<project name="antProject" basedir="." default="prompt">

	<property name="javac.version" value="1.6" />
	<property name="project.name" value="projectname" />
	<property name="source.encoding" value="UTF-8" />
	<property name="deploy.dir" value="/tmp/${project.name}" />

	<!-- project var set -->
	<property name="target.path" value="build" />
	<property name="target.root.path" value="${target.path}/ROOT" />
	<property name="target.root.classes" value="${target.root.path}/WEB-INF/classes" />
	
	<property name="webroot.path" value="WebRoot" />
	<property name="compileSrc" value="src" />
	<property name="compileDest" value="${target.root.classes}" />
	<property name="env.dir" value="env" />
	<property name="env.cfg" value="${env.dir}/${profile}" />
	<tstamp>
		<format property="now" pattern="yyyyMMddHHmmss" />
	</tstamp>
	<path id="compileClassPath">
		<fileset dir="${webroot.path}/WEB-INF/lib">
			<include name="*.jar" />
		</fileset>
	</path>
	<target name="prompt" description="show prompt information">
		<echo>Please choose task: clean, compile, war, deploy. ${line.separator}
			Example: ${line.separator}
			ant compile ${line.separator}
			ant -Dprofile=test deploy
		</echo>
	</target>
	<target name="clean">
	    <delete dir="${target.path}" />
	    <mkdir dir="${target.path}" />
		<delete dir="${compileDest}" />
		<mkdir dir="${compileDest}" />
		<delete dir="${target.root.classes}" />
		<mkdir dir="${target.root.classes}" />
		<echo>cleaned for ${compileDest}, ${target.root.path},${target.path}/*.war</echo>
	</target>

	<target name="compile" depends="clean,testIfProfileSet">
		<echo>starting compile the source from ${compileSrc}</echo>
		<javac srcdir="${compileSrc}" includeantruntime="false" fork="true" 
			memoryinitialsize="256m" memorymaximumsize="512m" destdir="${compileDest}" 
			classpathref="compileClassPath" encoding="${source.encoding}" 
			source="${javac.version}" target="${javac.version}" 
			debug="true" verbose="false" optimize="true" deprecation="false">
		</javac>
		<copy todir="${compileDest}">
			<fileset dir="${compileSrc}" excludes="**/*.svn,**/*.java" />
		</copy>
		<echo>compiled classes copied to ${compileDest}</echo>
		<copy todir="${target.root.path}">
			<fileset dir="${webroot.path}" excludes="**/*.svn,**/*.java" />
		</copy>
		<echo>copied to ${webroot.path} to ${compileDest}</echo>
		<echo>compile finished</echo>
	</target>
    <target name="testIfProfileSet">
        <echo>ant param profile: ${profile}</echo>
        <condition property="isProfileSet">
            <!-- <isset property="${profile}" /> -->
            <or>
                <equals arg1="${profile}" arg2="dev" />
            	<equals arg1="${profile}" arg2="test" />
                
            </or>
        </condition>
        <antcall target="profileProvided">
        </antcall>
        <antcall target="noProfile">
        </antcall>
    </target>
	
    <target name="profileProvided" if="isProfileSet">
        <copy todir="${target.root.classes}">
            <fileset dir="${env.cfg}" />
        </copy>
        <echo>copied config files ${env.cfg} to ${target.root.classes}</echo>
    </target>
    <target name="noProfile" if="isProfileSet">
        <echo>ant param profile is none, use default config files</echo>
    </target>
	
	<target name="war" depends="compile">
		<jar jarfile="${target.path}/${project.name}-${profile}${now}.war">
			<fileset dir="${target.root.path}">
				<include name="**/*" />
				<exclude name="**/*.fla" />
				<exclude name="**/*.mp3" />
				<exclude name="**/*.as" />
				<exclude name="**/*.bak" />
				<exclude name="**/*.swd" />
				<exclude name="**/*.db" />
			</fileset>
		</jar>
	</target>
	<target name="deploy" depends="compile">
		<delete dir="${deploy.dir}" />
		<mkdir dir="${deploy.dir}" />
		<copy todir="${deploy.dir}">
			<fileset dir="${target.root.path}">
			</fileset>
		</copy>
		<echo>deploy ${target.root.path} to ${deploy.dir}</echo>
	</target>
</project>

全文完,如果本文对您有所帮助,请花 1 秒钟帮忙点击一下广告,谢谢。

作 者: BridgeLi,https://www.bridgeli.cn
原文链接:http://www.bridgeli.cn/archives/168
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。
分类: 持续集成 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.

请输入正确的验证码