备份一下
第一份是真实使用的脚本,很工整:
<?xml version="1.0"?>
<project default="localDeployWar" basedir=".">
<echo message="pulling in property files" />
<property file="build.properties" />
<!-- 定义项目名称 -->
<property name="project.name" value="MyProject" />
<!-- 定义jar包名称 -->
<property name="jar.name" value="com.xx" />
<!-- 定义war名称 -->
<property name="war.name" value="${project.name}" />
<!-- 定义 web 容器的部署路径 -->
<property name="localWebServer.home" value="D:\a\apache-tomcat-6.0.18/" />
<!-- 定义 文件输出路径 -->
<property name="outFileDir" value="${localWebServer.home}/webapps/" />
<!-- 源代码路径 -->
<property name="src.dir" value="src" />
<!-- 编译的目标路径 -->
<property name="dist.dir" value="./dist" />
<property name="dist.classes.dir" value="${dist.dir}/classes" />
<property name="dist.jar.dir" value="${dist.dir}/jar" />
<property name="dist.war.dir" value="${dist.dir}/war" />
<!-- WebContent 结构下的几个路径 -->
<property name="webcontent.dir" value="./WebContent" />
<property name="webcontent.webinf.dir" value="${webcontent.dir}/WEB-INF" />
<!-- 定义 classpath 路径,抽象定义,可以多处引用 -->
<path id="classpath">
<!-- 项目的jar包-->
<fileset dir="${webcontent.webinf.dir}/lib">
<include name="**/*.jar" />
</fileset>
<!-- 项目的classes文件-->
<pathelement location="${webcontent.webinf.dir}/classes " />
<!-- web 服务器的jar包-->
<fileset dir="${localWebServer.home}/lib">
<include name="**/*.jar" />
</fileset>
</path>
<!-- 初始化删除、创建 classes 路径 -->
<target name="init">
<echo message=" ====== 初始化创建build.dir编译目录 ======" />
<delete dir="${dist.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${dist.jar.dir}" />
<mkdir dir="${dist.war.dir}" />
<echo message=" ====== 初始化WebContent.classes创建编译目录 ======" />
<!-- WebContent 下的 classes -->
<mkdir dir="${dist.classes.dir}" />
</target>
<!-- 编译 java 文件,编译到 dist.classes 下-->
<target name="compile" depends="init">
<echo message=" ====== 编译源代码 复制到 dist.classes 目录 ======" />
<javac encoding="utf-8" srcdir="${src.dir}" destdir="${dist.classes.dir}" debug="on" deprecation="false" optimize="true" failοnerrοr="true" source="1.6" target="1.6">
<classpath refid="classpath" />
</javac>
<!-- 格式 二 <javac srcdir="${src.dir} " destdir="${build.classes.dir}" classpathref="classpath" /> -->
<echo message=" ====== 将重新编译的classes文件复制到classes目录下 ======" />
<copy todir="${webcontent.webinf.dir}/classes">
<fileset dir="${dist.classes.dir}">
<exclude name="**/*Test*.*" />
</fileset>
</copy>
</target>
<target name="test" depends="compile" description="Junit 测试,暂无">
<echo message=" ====== Junit 测试,暂无 ======" />
<!--
<delete dir="${build.report.dir}" />
<mkdir dir="${build.report.dir}" />
<fail if="tests.failed" /> -->
</target>
<target name="jar" depends="test" description=" 将dist.classes打 jar包">
<echo message=" ====== 将src打 jar包 ======" />
<jar destfile="${dist.jar.dir}/${jar.name}.jar" basedir="${dist.classes.dir}">
<!--
<manifest>
<attribute name="Build-By" value="${user.name}"/>
<attribute name="Main-Class" value="com.anllin.ant.test.Window"/>
</manifest>
-->
</jar>
</target>
<target name="war" depends="test" description="将工程打成 war 包">
<echo message=" ====== 打 war 包 ======" />
<delete file="${dist.war.dir}/${war.name}.war" />
<war warfile="${dist.war.dir}/${war.name}.war" webxml="${webcontent.webinf.dir}/web.xml">
<fileset dir="${webcontent.dir}" >
</fileset>
</war>
</target>
<target name="localDeploy" depends="test" description="将工程直接部署到webapps">
<echo message="将工程不打包直接部署到localWebapps" />
<delete file="${outFileDir}/${war.name}.war" />
<delete dir="${outFileDir}/${project.name}" />
<mkdir dir="${outFileDir}/${project.name}" />
<copy todir="${outFileDir}/${project.name}" overwrite="true">
<fileset dir="${webcontent.dir}" />
</copy>
</target>
<target name="localDeployWar" depends="war" description="将war包部署server中">
<echo message="将war包部署local server中" />
<delete file="${outFileDir}/${war.name}.war" />
<delete dir="${outFileDir}/${project.name}" />
<copy todir="${outFileDir}" overwrite="true">
<fileset file="${dist.war.dir}/${war.name}.war" />
</copy>
</target>
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />
<target name="delete" description="删除local服务器上的 war ">
<delete dir="${dist.classes.dir}" />
<delete file="${dist.dir}/${war.name}.war" />
<delete dir="${localWebServer.home}/webapps/${project.name}" />
<delete file="${localWebServer.home}/webapps/${war.name}.war " />
</target>
</project>
这份是最全的:
<?xml version="1.0"?>
<project default="localDeployWar" basedir=".">
<echo message="pulling in property files" />
<property file="build.properties" />
<!-- 定义项目名称 -->
<property name="project.name" value="MyProject" />
<!-- 定义jar包名称 -->
<property name="jar.name" value="com.xx" />
<!-- 定义war名称 -->
<property name="war.name" value="${project.name}" />
<!-- 定义local web 容器的部署路径 -->
<property name="localWebServer.home" value="D:/common/apache-tomcat-6.0.39/" />
<!-- 定义remote web 容器 -->
<property name="url" value="http://192.168.0.56:7080/manager" />
<property name="username" value="coobase" />
<property name="password" value="fulong" />
<property name="remoteWebappPath" value="/EasyPiping" />
<!-- 源代码路径 -->
<property name="src.dir" value="src" />
<!-- 编译的目标路径 -->
<property name="dist.dir" value="./dist" />
<property name="dist.classes.dir" value="${dist.dir}/classes" />
<property name="dist.jar.dir" value="${dist.dir}/jar" />
<property name="dist.war.dir" value="${dist.dir}/war" />
<!-- WebContent 结构下的几个路径 -->
<property name="webcontent.dir" value="./WebContent" />
<property name="webcontent.webinf.dir" value="${webcontent.dir}/WEB-INF" />
<!-- 定义 classpath 路径,抽象定义,可以多处引用 -->
<path id="classpath">
<!-- 项目的jar包-->
<fileset dir="${webcontent.webinf.dir}/lib">
<include name="**/*.jar" />
</fileset>
<!-- 项目的classes文件-->
<pathelement location="${webcontent.webinf.dir}/classes " />
<!-- web 服务器的jar包-->
<fileset dir="${localWebServer.home}/lib">
<include name="**/*.jar" />
</fileset>
</path>
<!-- 初始化删除、创建 classes 路径 -->
<target name="init">
<echo message=" ====== 初始化创建build.dir编译目录 ======" />
<delete dir="${dist.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${dist.jar.dir}" />
<mkdir dir="${dist.war.dir}" />
<echo message=" ====== 初始化WebContent.classes创建编译目录 ======" />
<!-- WebContent 下的 classes -->
<mkdir dir="${dist.classes.dir}" />
</target>
<!-- 编译 java 文件,编译到 dist.classes 下-->
<target name="compile" depends="init">
<echo message=" ====== 编译源代码 复制到 dist.classes 目录 ======" />
<javac encoding="utf-8" srcdir="${src.dir}" destdir="${dist.classes.dir}" debug="on" deprecation="false" optimize="true" failοnerrοr="true" source="1.6" target="1.6">
<classpath refid="classpath" />
</javac>
<!-- 格式 二 <javac srcdir="${src.dir} " destdir="${build.classes.dir}" classpathref="classpath" /> -->
<echo message=" ====== 将重新编译的classes文件复制到classes目录下 ======" />
<copy todir="${webcontent.webinf.dir}/classes">
<fileset dir="${dist.classes.dir}">
<exclude name="**/*Test*.*" />
</fileset>
</copy>
</target>
<target name="test" depends="compile" description="Junit 测试,暂无">
<echo message=" ====== Junit 测试,暂无 ======" />
<!--
<delete dir="${build.report.dir}" />
<mkdir dir="${build.report.dir}" />
<fail if="tests.failed" /> -->
</target>
<target name="jar" depends="test" description=" 将dist.classes打 jar包">
<echo message=" ====== 将src打 jar包 ======" />
<jar destfile="${dist.jar.dir}/${jar.name}.jar" basedir="${dist.classes.dir}">
<!--
<manifest>
<attribute name="Build-By" value="${user.name}"/>
<attribute name="Main-Class" value="com.anllin.ant.test.Window"/>
</manifest>
-->
</jar>
</target>
<target name="war" depends="test" description="将工程打成 war 包">
<echo message=" ====== 打 war 包 ======" />
<delete file="${dist.war.dir}/${war.name}.war" />
<war warfile="${dist.war.dir}/${war.name}.war" webxml="${webcontent.webinf.dir}/web.xml">
<fileset dir="${webcontent.dir}" >
</fileset>
</war>
</target>
<target name="localDeploy" depends="test" description="将工程直接部署到webapps">
<echo message="将工程不打包直接部署到localWebapps" />
<delete file="${localWebServer.home}/webapps/${war.name}.war" />
<delete dir="${localWebServer.home}/webapps/${project.name}" />
<mkdir dir="${localWebServer.home}/webapps/${project.name}" />
<copy todir="${localWebServer.home}/webapps/${project.name}" overwrite="true">
<fileset dir="${webcontent.dir}" />
</copy>
</target>
<target name="localDeployWar" depends="war" description="将war包部署server中">
<echo message="将war包部署local server中" />
<delete file="${localWebServer.home}/webapps/${war.name}.war" />
<delete dir="${localWebServer.home}/webapps/${project.name}" />
<copy todir="${localWebServer.home}/webapps" overwrite="true">
<fileset file="${dist.war.dir}/${war.name}.war" />
</copy>
</target>
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />
<!-- 把war包部署到给定url下的由path所指定远程路径下。-->
<target name="remoteDeployWar_all" description="Install remote web application" depends="remoteUndeployWar,war">
<deploy url="${url}" username="${username}" password="${password}" path="${remoteWebappPath}" war="${dist.war.dir}/${war.name}.war" />
</target>
<target name="remoteDeployWar_only" description="Install remote web application" depends="war">
<deploy url="${url}" username="${username}" password="${password}" path="${remoteWebappPath}" war="${dist.war.dir}/${war.name}.war" />
</target>
<!-- 说明:depends="war,undeploy"说明将部署一个war包,如果存在同名的,那么先进行undeploy.-->
<target name="remoteUndeployWar" description="卸载部署工作">
<undeploy url="${url}" username="${username}" password="${password}" path="${remoteWebappPath}" />
</target>
<target name="delete" description="删除local服务器上的 war ">
<delete dir="${dist.classes.dir}" />
<delete file="${dist.dir}/${war.name}.war" />
<delete dir="${localWebServer.home}/webapps/${project.name}" />
<delete file="${localWebServer.home}/webapps/${war.name}.war " />
</target>
<!-- 直接使用 SQL 语句书此话数据库 -->
<target name="initDB1">
<sql driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/" userid="root" password="">
<classpath refid="classpath" />
DROP database IF exists login1;
CREATE database login1;
use login1;
create table user(
user_id int(11) auto_increment,
user_name varchar(50) not nul l ,
user_pass varchar(50) not nul l ,
email varchar(100),
primary key(user_id),
unique(user_name)
);
insert into user values(10000, 'pzp', '123' , 'pzpyp@163.com');
</sql>
</target>
<!-- 使用.sql 脚本文件 初始化数据库 -->
<target name="initDB2">
<sql driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/" userid="root" password="" src="./mysql .sq l ">
<classpath refid="classpath" />
</sql>
</target>
</project>
运行它你可以放在eclipse,直接使用(eclipse集成了ant插件)
也可以在命令行中使用,前提是你必须下载ant包:
ant -buildfile D:\workspace_eclipse_kepler\xx\ant.xml localDeployWar
其中 -buildfile 指定脚本名称,默认名称是build.xml localDepalyWar是具体的target,不指定target则默认使用脚本中的默认target