一、前言
通过apache ant对于项目的编译、配置、打包部署发布的批处理命令集相关构建文件样例模板。
二、批处理命令
1. 编译 - ant_compile.bat&build.xml
@echo on@b@title ant compile@b@set ANT_HOME=D:\study\java\ant\ant_1.8.4@b@set path=.;%ANT_HOME%\bin;%path%@b@ant -buildfile build.xml@b@@pause
<?xml version="1.0" encoding="UTF-8"?>@b@<project name="zhsq" basedir="D:\project" default="com">@b@ <description>@b@ 一段描述信息,没有实际作用。@b@ </description>@b@ <property name="project_home" location="D:\project\zhsq"/>@b@ <property name="srcDir" location="${project_home}\src"/>@b@ <property name="buildDir" location="${project_home}\WebRoot\WEB-INF\classes"/>@b@ <property name="distDir" location="${project_home}\WebRoot\WEB-INF\lib"/>@b@ <property name="jre_home" location="C:\Program Files\Java\jre1.6.0_02"/> @b@ <property name="tomcat_lib" location="C:\Tomcat6.0\lib"/>@b@ @b@ <path id="project.classpath">@b@ <fileset dir="${distDir}">@b@ <include name="**/*.jar" />@b@ </fileset> @b@ <fileset dir="${tomcat_lib}">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${jre_home}\lib">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${jre_home}\lib\ext">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <pathelement location="${buildDir}" /> @b@ </path>@b@ @b@ <target name="init" description="在元素中指定描述信息!">@b@ <echo message="系统开始初始化..."/> @b@ <delete dir="${build.dir}" />@b@ <mkdir dir="${build.dir}"/>@b@ <echo message="配置文件正在复制中..."/>@b@ <copy todir="${buildDir}" overwrite="true" includeemptydirs="false">@b@ <fileset dir="${srcDir}\config">@b@ <include name="**/*"/>@b@ <exclude name="**/.svn/**"/>@b@ <exclude name="**/*.java"/>@b@ </fileset>@b@ </copy>@b@ </target>@b@@b@@b@ <target name="test" depends="init"> @b@ <echo message="开始编译src/test..."/> @b@ <javac srcdir="${srcDir}\test" destdir="${buildDir}" source="1.6" target="1.6" encoding="utf-8" debug="no" deprecation="false" optimize="false" failonerror="true"> @b@ <classpath refid="project.classpath"/>@b@ </javac>@b@ </target>@b@ @b@ <target name="com" depends="test"> @b@ <echo message="开始编译src/java..."/> @b@ <javac srcdir="${srcDir}\java" destdir="${buildDir}" source="1.6" target="1.6" encoding="utf-8" debug="no" deprecation="false" optimize="false" failonerror="true"> @b@ <classpath refid="project.classpath"/>@b@ </javac>@b@ </target>@b@@b@ @b@ @b@</project>
2. 配置 - compile-config.bat&config.xml
@echo on@b@title ant compile@b@set ANT_HOME=D:\study\java\ant\ant_1.8.4@b@set path=.;%ANT_HOME%\bin;%path%@b@ant -buildfile config.xml@b@@pause
<?xml version="1.0" encoding="UTF-8"?>@b@<project name="zhsq" basedir="D:\project" default="init">@b@ <description>@b@ 一段描述信息,没有实际作用。@b@ </description>@b@ <property name="project_home" location="D:\project\zhsq"/>@b@ <property name="srcDir" location="${project_home}\src"/>@b@ <property name="buildDir" location="${project_home}\WebRoot\WEB-INF\classes"/>@b@ <property name="distDir" location="${project_home}\WebRoot\WEB-INF\lib"/>@b@ <property name="jre_home" location="C:\Program Files\Java\jre1.6.0_02"/>@b@ <property name="tomcat_lib" location="C:\Tomcat6.0\lib"/>@b@ @b@@b@ @b@ <path id="project.classpath">@b@ <fileset dir="${distDir}">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${tomcat_lib}">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${jre_home}\lib">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${jre_home}\lib\ext">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <pathelement location="${buildDir}" /> @b@ </path>@b@ @b@ <target name="init" description="在元素中指定描述信息!">@b@ <echo message="系统开始初始化..."/> @b@ <echo message="配置文件正在复制中..."/>@b@ <copy todir="${buildDir}" overwrite="true" includeemptydirs="false">@b@ <fileset dir="${srcDir}\config">@b@ <include name="**/*"/>@b@ <exclude name="**/.svn/**"/>@b@ <exclude name="**/*.java"/>@b@ </fileset>@b@ </copy>@b@ </target> @b@</project>
3. 执行环境 - compile-java.bat&java.xml
@echo on@b@title ant compile@b@set ANT_HOME=D:\study\java\ant\ant_1.8.4@b@set path=.;%ANT_HOME%\bin;%path%@b@ant -buildfile java.xml@b@@pause
<?xml version="1.0" encoding="UTF-8"?>@b@<project name="zhsq" basedir="D:\project" default="test">@b@ <description>@b@ 一段描述信息,没有实际作用。@b@ </description>@b@ <property name="project_home" location="D:\project\zhsq"/>@b@ <property name="srcDir" location="${project_home}\src"/>@b@ <property name="buildDir" location="${project_home}\WebRoot\WEB-INF\classes"/>@b@ <property name="distDir" location="${project_home}\WebRoot\WEB-INF\lib"/>@b@ <property name="jre_home" location="C:\Program Files\Java\jre1.6.0_02"/>@b@ <property name="tomcat_lib" location="C:\Tomcat6.0\lib"/>@b@ @b@@b@ @b@ <path id="project.classpath">@b@ <fileset dir="${distDir}">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${tomcat_lib}">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${jre_home}\lib">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${jre_home}\lib\ext">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <pathelement location="${buildDir}" /> @b@ </path>@b@ @b@ <target name="init" description="在元素中指定描述信息!"> @b@ </target>@b@@b@@b@ <target name="test" depends="init"> @b@ <echo message="开始编译src/java..."/> @b@ <javac srcdir="${srcDir}\java" destdir="${buildDir}" source="1.6" target="1.6" encoding="utf-8" debug="no" deprecation="false" optimize="false" failonerror="true"> @b@ <classpath refid="project.classpath"/>@b@ </javac>@b@ </target>@b@ @b@</project>
4. 打包部署发布 - delploy.bat&deploy.xml
@echo on@b@title ant compile@b@set ANT_HOME=D:\study\java\ant\ant_1.8.4@b@set path=.;%ANT_HOME%\bin;%path%@b@ant -buildfile deploy.xml@b@@pause
<?xml version="1.0" encoding="UTF-8"?>@b@<project name="zhsq" basedir="D:\project" default="init">@b@ <description>@b@ 一段描述信息,没有实际作用。@b@ </description>@b@ <property name="project_home" location="D:\project\zhsq"/>@b@ <property name="srcDir" location="${project_home}\src"/>@b@ <property name="buildDir" location="${project_home}\WebRoot\WEB-INF\classes"/>@b@ <property name="distDir" location="${project_home}\WebRoot\WEB-INF\lib"/>@b@ <property name="jre_home" location="C:\Program Files\Java\jre1.6.0_02"/>@b@ <property name="tomcat_lib" location="C:\Tomcat6.0\lib"/>@b@ <property name="jar_name" value="commons-po"/>@b@ @b@@b@ @b@ <path id="project.classpath">@b@ <fileset dir="${distDir}">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${tomcat_lib}">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${jre_home}\lib">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <fileset dir="${jre_home}\lib\ext">@b@ <include name="**/*.jar" />@b@ </fileset>@b@ <pathelement location="${buildDir}" /> @b@ </path>@b@ @b@ <target name="init" description="在元素中指定描述信息!">@b@ <echo message="删除原有${commons-po}jar包..."/>@b@ <delete file="${distDir}\${jar_name}.jar" />@b@@b@ <mkdir dir="${distDir}\java\com"/>@b@ <echo message="复制class文件到lib下..."/>@b@ <copy todir="${distDir}\java\com" overwrite="true" includeemptydirs="false">@b@ <fileset dir="${buildDir}\com">@b@ <include name="**\*"/> @b@ </fileset>@b@ </copy>@b@@b@ <echo message="将class打成jar包"/>@b@ <jar jarfile="${distDir}\${jar_name}.jar" compress="true">@b@ <fileset dir="${distDir}\java">@b@ <include name="**/*"/> @b@ </fileset>@b@ </jar>@b@@b@ <echo message="删除临时目标文件"/>@b@ @b@ <delete dir="${distDir}\java" />@b@@b@ <delete dir="${buildDir}\com" />@b@@b@ @b@ </target> @b@ @b@</project>