使用ant脚本,对java web项目,编译、打包、发布部署到tomcat

javac默认使用jdk进行编译。当项目中有

<T> T <X> X
这种类型时,ant编译不能通过。必须使用eclipse jdt进行编译。

指定使用jdt进行编译,两种方法都可以。

一是定义build.compiler属性:

<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />

二是在javac中指定参数compiler="org.eclipse.jdt.core.JDTCompilerAdapter"


如果项目中没有那样的泛型代码,不需要指定编译器。


<?xml version="1.0" encoding="UTF-8"?>
<project name="lms" default="deploy" basedir="D:/workspace/lms">
	<property environment="env" />
	<property name="webapp.name" value="lms" />
	<property name="catalina.home" value="D:/developer/apache-tomcat-6.0.32" />
	<property name="dist.dir" value="${basedir}/dist" />
	<property name="ant.dir" value="D:/developer/apache-ant-1.8.3" />
	<property name="webRoot.dir" value="${basedir}/webapp" />
	<property name="src.dir" value="${basedir}/src" />
	<property name="config.dir" value="${basedir}/resources" />
	<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
	<property name="build.dir" value="${basedir}/build" />
	<!-- 使用eclipse jdt进行编译,而不使用JDK编译  -->
	<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />

	<!-- 初始化classpath -->
	<path id="project.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<!-- 添加tomcat类路径 -->
		<fileset dir="${catalina.home}/lib">
			<include name="*.jar" />
		</fileset>
		<!-- ant lib包  -->
		<fileset dir="${ant.dir}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<!-- get the source compile classpath in a printable form -->
	<pathconvert pathsep="${line.separator}|   |-- "
             property="echo.path.compile"
             refid="project.classpath">
	</pathconvert>
	
	<!-- show classpath jars -->
	<target name="print_classpath">
		<echo message="|-- compile classpath"/>
		<echo message="|   |"/>
		<echo message="|   |-- ${echo.path.compile}"/>
	</target>
	
	
	<!-- 删除之前的目录结构 -->
	<target name="clear" description="清理旧文件">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
		<delete file="${catalina.home}/webapps/${webapp.name}.war" />
		<delete dir="${catalina.home}/webapps/${webapp.name}" />
	</target>

	<!-- 创建目录结构 -->
	<target name="init" depends="clear" description="创建初始化目录结构">
		<mkdir dir="${build.dir}/classes" />
		<mkdir dir="${dist.dir}" />
	</target>

	<!-- 编译java -->
	<target name="compile" depends="init" description="编译java文件">
		<echo message="begin compile..." />
		<javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
			includeantruntime="false" nowarn="on" 
			source="1.6" target="1.6" deprecation="true" debug="true" 
			encoding="UTF-8" classpathref="project.classpath" 
			>
			<compilerarg line="-Xlint:unchecked" />
			<!-- <classpath refid="project.classpath" /> -->
		</javac>
		<copy todir="${build.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			</fileset>
			<fileset dir="${config.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			</fileset>
		</copy>
		<echo message="end compile..." />
	</target>

	<!-- 将class文件打成 jar包 -->
	<!--  
	    <target name="pack" depends="compile"> 
	        <jar jarfile="${build.dir}/${webapp.name}.jar"> 
	            <fileset dir="${build.dir}/classes"> 
	                <include name="**/*.class"/> 
	            </fileset> 
	        </jar> 
	    </target> 
	-->

	<!-- 打成war包, 名称默认为 项目名 -->
	<target name="war" depends="compile" description="将工程打成war包">
		<echo message="begin war..." />
		<war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}" 
			webxml="${webRoot.dir}/WEB-INF/web.xml">
			<lib dir="${lib.dir}" />
			<classes dir="${build.dir}/classes" />
			<fileset dir="${webRoot.dir}">
				<include name="***.*" />
			</fileset>
		</war>
		<echo message="end war..." />
	</target>

	<!-- copy war包 tomcat的deploy目录 -->
	<target name="deploy" depends="war" description="部署项目">
		<echo message="begin deploy..." />
		<copy file="${dist.dir}/${webapp.name}.war" todir="${catalina.home}/webapps" />
		<echo message="end deploy..." />
	</target>

</project>

之后,在ant eclipse 插件里面,运行ant脚本,还需要如下设置:

在build.xml上点右键 ---  Run as --->

External Tools Configurations .然后点JRE.选中第一项,然后点击Run。

或者在eclipse ant插件界面,点lmx即项目名称,run as,也是一样。

之后再运行ant,就无需再这样设置。直接运行就可。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值