Ant 使用小结

以前的项目中都没有用到过ant,都是手动打war,复制copy。最近的项目中ant 配置一堆 一堆的,在做的时候只是处于能看懂的状况。昨天写了个小的例子,mark一下。

 

完成的功能:编译,打jar包,打war包,连同开发jdk,tomcat 部署

项目目录



 

bat脚本:

@echo off

setlocal
set PROJECT_HOME=%~dp0%..
set ANT_HOME=%PROJECT_HOME%\lib\ant

set JAVA_HOME=%PROJECT_HOME%\lib\sun\jdk1.6.0_35
set args=%*
set PATH=%ANT_HOME%\bin;%PATH%
call %ANT_HOME%\bin\ant.bat %args%
exit /b %ERRORLEVEL%

 

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project default="init" name="Project testApp" basedir=".">
	<echo message="Using Java ${java.version}" />
	<property name="codeProject" location="../testApp" />
	<property name="lib" location="../lib" />
	<property name="deploy" location="../deploy" />
	<property name="srcDir" location="${codeProject}/src" />
	<property name="buildDir" location="../work/build" />
	<property name="distDir" location="../work/dist" />
	<property name="projectName" value="TestApp" />
	<path id="project.classpath">
		<fileset dir="${lib}/sun/jdk1.6.0_35/jre/lib">
			<include name="/*.jar" />
		</fileset>
		<fileset dir="${lib}/tomcat/lib">
			<include name="/*.jar" />
		</fileset>
		<fileset dir="${codeProject}/WebContent/WEB-INF/lib">
			<include name="/*.jar" />
		</fileset>
	</path>
	<target name="init" description="Initializ">
		<tstamp />
		<mkdir dir="${buildDir}" />
		<mkdir dir="${distDir}" />
		<mkdir dir="${deploy}" />
		<echo message="init" />
	</target>
	<target name="compile" depends="init">
		<javac srcdir="${srcDir}" destdir="${buildDir}"
			includeantruntime="false">
			<classpath refid="project.classpath" />
		</javac>
		<echo message="compile" />
	</target>

	<target name="dist" depends="compile">
		<jar destfile="${distDir}/${projectName}-${DSTAMP}.jar" basedir="${buildDir}">
			<manifest>
				<attribute name="Built-By" value="${user.name}" />
				<attribute name="Main-Class" value="package.Main" />
			</manifest>
		</jar>
		<jar destfile="${distDir}/${projectName}-src-${DSTAMP}.jar"
			basedir="${srcDir}" />
	</target>
	<target name="build" depends="dist">
		<copy todir="${deploy}/sun">
			<fileset dir="${lib}/sun" includes="**/*" />
		</copy>
		<copy todir="${deploy}/tomcat">
			<fileset dir="${lib}/tomcat" includes="**/*" />
		</copy>
		<copy todir="${distDir}">
			<fileset dir="${codeProject}/WebContent/WEB-INF/lib" includes="**/*" />
		</copy>
       <war warfile="${deploy}/tomcat/webapps/${projectName}.war" webxml="${codeProject}/WebContent/WEB-INF/web.xml">   
           <lib dir="${distDir}"/>   
           <fileset dir="${codeProject}/WebContent"/>   
       </war> 
       <copy file="./run.bat" tofile="${deploy}/run.bat"/>
	</target>
	<target name="clean">
		<echo message="clean" />
		<delete dir="${buildDir}" />
		<delete dir="${distDir}" />
		<delete dir="${deploy}" />
	</target>
</project>

 

run tomcat bat

set DEPLOY_HOME=%~dp0%
set JAVA_HOME=%DEPLOY_HOME%\sun\jdk1.6.0_35

set PATH=%JAVA_HOME%\bin;%PATH%
set TOMCAT_HOME=%DEPLOY_HOME%\tomcat
set CATALINA_HOME=%TOMCAT_HOME%
call %TOMCAT_HOME%\bin\startup.bat
exit /b %ERRORLEVEL%

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值