Ant 打包war 部署tomcat
<?xml version="1.0" encoding="UTF-8"?><!-- name:对应工程的名字;default:需要的缺省任务(运行"ant"不指明任务时执行的任务) -->
<project name="ssjdemo" basedir=".">
<!-- 建立目录结构
project
... src JAVA源码编辑目录
... WebRoot web文件存放地方
... WEB-INF
...lib jar包(类库)存放目录
... build 编译生成的class文件存放目录
... dist war和javadoc存放目录
... build.xml ant脚本
-->
<property name="src.dir" value="src"/>
<property name="lib.dir" value="WEB-INF/lib"/>
<property name="webRoot.dir" value="WebRoot"/>
<property name="web-inf.dir" value="${webRoot.dir}/WEB-INF"/>
<property name="build.dir" value="build"/>
<property name="dist.dir" value="dist" />
<!-- 指向环境变量的系统变量 -->
<property environment="env"/>
<!-- tomcat 路径 -->
<property name="tomcat.home" value="${env.CATALINA_HOME}" />
<!-- test -->
<target name="show_env">
<echo message="系统变量 = ${tomcat.home}" />
<echo message="${project.classpath}" />
</target>
<!-- 初始化 classpath -->
<path id="project.classpath">
<fileset dir="${webRoot.dir}/${lib.dir}">
<include name="**/*.jar"/>
</fileset>
<!--添加tomcat类路径-->
<fileset dir="${tomcat.home}/lib">
<include name="*.jar"/>
</fileset>
<pathelement location="${build.dir}/classes"/>
<pathelement path="${java.class.path}"/>
</path>
<!-- 删除之前的目录结构 -->
<target name="clear">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
<delete file="${tomcat.home}\webapps\${ant.project.name}.war"/>
<delete dir="${tomcat.home}\webapps\${ant.project.name}"/>
</target>
<!-- 创建目录结构 -->
<target name="init">
<mkdir dir="${build.dir}/classes"/>
<mkdir dir="${dist.dir}"/>
</target>
<!-- 编译Java代码 -->
<target name="compile" depends="init" description="编译java文件">
<javac srcdir="${src.dir}" destdir="${build.dir}/classes">
<compilerarg line="-encoding GBK"/>
<classpath refid="project.classpath"/>
</javac>
<copy todir="${build.dir}">
<fileset dir="${src.dir}">
<include name="**/*.xml"/>
<include name="**/*.jar"/>
</fileset>
</copy>
</target>
<!-- 将class文件打成 jar包 -->
<!--
<target name="pack" depends="compile">
<jar jarfile="${build.dir}/${ant.project.name}.jar">
<fileset dir="${build.dir}/classes">
<include name="**/*.class"/>
</fileset>
</jar>
</target>
-->
<!-- 打成war包, 名称默认为 项目名 -->
<target name="dist" depends="compile" description="将工程打成war包">
<war destfile="${dist.dir}/${ant.project.name}.war" basedir="${webRoot.dir}" webxml="${web-inf.dir}/web.xml"/>
</target>
<!-- copy war包 tomcat的deploy目录 -->
<target name="deploy" depends="dist" description="部署工程">
<copy file="${dist.dir}/${ant.project.name}.war"
todir="${tomcat.home}\webapps" />
</target>
</project>
页:
[1]