使用ant编译发布web项目

本文就不对ant进行详细介绍,直接对一个实际项目的自动构建和部署等进行说明。

build.xml 内容主要分为2部分:项目编译库的配置 和 项目自动构建部署的编写

1、配置项目编译库classpath(文章底部贴出来的例子 build.xml 文件中,对编译库的配置写的不好,大家使用中参考下面这段代码这样写会比较清晰。)

	<!-- ClassPath configuration setting -->
	<!-- J2EE库 -->
	<path id="J2EE.libraryclasspath">
		<pathelement location="${library}/common/javaee/javaee.jar" />
	</path>
	<!-- 使用ant编译,在使用到com.sun包下类时,需要指定rt.jar文件的位置 -->
	<path id="JAVA.rt">
		<pathelement location="${JAVA_HOME}/jre/lib/rt.jar" />
	</path>
	<!-- Weblogic、Tomcat等容器lib -->
	<path id="Server.libraryclasspath">
		<fileset dir="${server.lib.dir}" includes="*.jar" />
	</path>
	<!-- 项目中引入其他工程的lib库, 或一些独立在某个地方的公共lib库 -->
	<path id="Common.libraryclasspath">
		<fileset dir="${commonProject.lib.dir}" includes="*.jar" />
	</path>
	<!-- 项目中引入其他工程源代码, 这里指定其他工程源码编译后的classes位置 -->
	<path id="Common.classesclasspath">
		<pathelement location="${commonProject.classes.dir}" />
	</path>
	<!-- 项目项目自身lib库 -->
	<path id="Project.libraryclasspath">
		<fileset dir="${basedir}/WebContent/WEB-INF/lib" includes="*.jar" />
	</path>
	<!-- 工程项目的classpath, 引入上面的一些公共库 -->
	<path id="Project.classpath">
		<path refid="JAVA.rt" />
		<path refid="J2EE.libraryclasspath" />
		<path refid="Server.libraryclasspath" />
		<path refid="Common.libraryclasspath" />
		<path refid="Project.libraryclasspath" />
		<path refid="Common.classesclasspath" />
	</path>

2、编译项目过程说明(层级关系就是 depends 关系)
   > deploy ⑻  使用ant执行deploy之前,需要先对Common项目进行ant编译,因为当前项目需要引用Common项目的classes
       > release-code ⑹  depends="dist, copy-sql, copy-config"
           > dist ⑶  复制项目WebContent目录下所有的 (jsp文件\WEB-INF下除classes目录以外的所有文件\CommonLibrary文件) 到发布目录中的WebContent中、复制“ant编译的classes目录”到发布目录下的WebContent\WEB-INF目录中、复制commonProject.lib下的所有jar到发布目录下的WebContent\WEB-INF\lib中
               > build-project ⑵  指定src和Project.classpath, 使用javac进行编译, 指定编译后的classes存放目录
                   > init ⑴  复制当前项目src目录下的 (除.java\.launch\.svn文件以外的所有文件) 到“编译后classes文件的存放目录”
           > copy-sql ⑷  复制当前项目及引用的项目src目录下所有mybatis的sql文件到发布目录中的WEB-INF/classes中
           > copy-config ⑸  复制当前项目src目录下的config目录到发布目录中中的WEB-INF/classes中(项目中的所有配置文件都放在src下的config目录中)
       > release-resource ⑺  复制当前项目WebContent下的所有静态资源文件到发布目录中(<fileset dir="${basedir}/WebContent" excludes="**/META-INF/**,**/WEB-INF/**,**/*.jsp" />) 或发布到apache中(静态资源交由apache处理)


大家对照下面的某项目的实际 build.xml 文件内容便于理解(其中有一些多余的部分,没有进行删除,不影响理解)。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="release-code" name="Backoffice">
	<!-- ######################################################### -->
	<!-- properties configuration for build                        -->
	<!-- ######################################################### -->
	<property environment="env" />
	<property name="systemname" value="MobileWAP" />
	<property name="libname" value="Common" />
	<property name="projectname" value="${systemname}-Backoffice" />
	<property name="was.home" value="/app/bea/wlserver" />
	<property name="frameone.home" value="/svcroot" />
	<property name="frameone.runtime" value="${frameone.home}/runtime" />
	<property name="frameone.workspace" value="${frameone.home}/workspace" />
	<property name="runtime.webapps" value="${frameone.runtime}/webinterface" />
	<property name="dist.project.common" value="${frameone.runtime}/common/${libname}-Library/common" />
	<property name="dist.frameone.common" value="${frameone.runtime}/common/${libname}-Library/frameone" />
	<property name="frameone.lib.dir" value="${dist.frameone.common}/lib" />
	<property name="project.lib.dir" value="${dist.project.common}/lib" />
	<property name="backoffice.dir" value="${runtime.webapps}/${projectname}" />
	<property name="backoffice.config.dir" value="${backoffice.dir}/config" />
	<property name="backoffice.sql.dir" value="${backoffice.dir}/sql" />
	<property name="dist.sqlx.dir" value="${frameone.workspace}/dist/${libname}-Library/sqlx" />
	<property name="dist.config.dir" value="${frameone.workspace}/dist/${libname}-Library/config" />
	<property name="dist.classes.dir" value="${frameone.workspace}/dist/${libname}-Library/build/classes" />
	<property name="dist.classes.dir.wap" value="${dist.classes.dir}/wap" />
	<property name="debuglevel" value="source,lines,vars" />
	<property name="target" value="1.7" />
	<property name="source" value="1.7" />
	<property name="src.dir" value="${basedir}/src" />
	<property name="dist.dir" value="${frameone.workspace}/dist" />
	<property name="deploy.dir" value="${backoffice.dir}/WebContent" />
	<property name="web.inf.dir" value="${basedir}/WebContent/WEB-INF" />
	<property name="web.inf.lib.dir" value="${web.inf.dir}/lib" />
	<property name="build.dir" value="${basedir}/build" />
	<property name="build.classes.dir" value="${basedir}/build/classes" />
	<property name="war.name" value="${projectname}.war" />
	<property name="backup.dir" value="${frameone.runtime}/backup/${projectname}" />
	<property name="backup.name" value="${projectname}" />
	<property name="web.archive.name" value="${projectname}-Web" />
	<property name="web.archive.ext" value="zip" />
	<property name="web.archive.ftp.remotedir" value="${frameone.runtime}/webstatic" />
	<property name="web.dir" value="${basedir}/WebContent" />
	<!--<property name="runtime.contextname" value="wap" />-->
	<property name="runtime.contextname" value="" />
	<property name="runtime.webstatic" value="${frameone.runtime}/webstatic" />
	<tstamp>
		<format property="now" pattern="yyyyMMddhhmmss" />
	</tstamp>
	<!-- ClassPath configuration setting -->
	<path id="J2EE.libraryclasspath">
		<pathelement location="${frameone.runtime}/common/javaee/javaee.jar" />
	</path>
	<path id="WAS.libraryclasspath">
		<fileset dir="${was.home}/server/lib" includes="*.jar">
		</fileset>
	</path>
	<path id="Project.classpath">
		<pathelement location="${dist.classes.dir}" />
		<path refid="J2EE.libraryclasspath" />
		<fileset dir="${frameone.lib.dir}" includes="common*.jar" />
		<fileset dir="${frameone.lib.dir}" includes="*.jar" />
		<fileset dir="${project.lib.dir}" includes="*.jar" />
		<path refid="WAS.libraryclasspath" />
	</path>

	<!-- ######################################################### -->
	<!-- endpoint targets                                          -->
	<!-- ######################################################### -->
	<target name="cleanall" depends="clean" />
	<target name="deploy" depends="release-code, release-resource" />
	<target name="release-code" depends="dist, copy-sql, copy-config" />
	<target name="clean">
		<delete includeemptydirs="true" quiet="true">
			<fileset dir="${dist.classes.dir}" includes="**/*" excludes="**/upload,**/img/">
			</fileset>
		</delete>
	</target>
	<target name="init">
		<mkdir dir="${dist.classes.dir}" />
		<copy includeemptydirs="false" todir="${dist.classes.dir}">
			<fileset dir="${src.dir}" excludes="**/*.launch, **/*.java" />
		</copy>
		<copy includeemptydirs="false" todir="${dist.classes.dir.wap}">
			<fileset dir="${src.dir}" excludes="**/*.launch, **/*.java" />
		</copy>
	</target>
	<target name="build-project" depends="init">
		<echo message="${ant.project.name}: ${ant.file}" />
		<javac includeantruntime="false" debug="true" debuglevel="${debuglevel}" destdir="${dist.classes.dir.wap}" source="${source}" target="${target}" encoding="UTF-8">
			<src path="${src.dir}" />
			<classpath refid="Project.classpath" />
		</javac>
	</target>

	<target name="backup">
		<mkdir dir="${backup.dir}" />
		<mkdir dir="${deploy.dir}" />
		<zip destfile="${backup.dir}/${backup.name}_${now}.zip" basedir="${deploy.dir}" />
	</target>

	<target name="dist" depends="build-project">
		<delete includeemptydirs="true" quiet="true">
			<fileset dir="${deploy.dir}" includes="**/*" excludes="**/upload,**/img/">
			</fileset>
		</delete>
		<mkdir dir="${deploy.dir}" />
		<mkdir dir="${web.dir}" />
		<copy todir="${deploy.dir}" verbose="true" overwrite="true">
			<fileset dir="${web.dir}">
				<include name="install/**" />
				<include name="MiPlatform/*.jsp" />
				<include name="common/**/*.jsp" />
				<include name="WEB-INF/**/*" />
				<include name="RDServer/**" />
			</fileset>
		</copy>
		<copy todir="${deploy.dir}/WEB-INF/classes" verbose="true" overwrite="true">
			<fileset dir="${dist.classes.dir.wap}">
			</fileset>
		</copy>
		<copy todir="${deploy.dir}/WEB-INF/lib" verbose="true" overwrite="true">
			<fileset dir="${frameone.lib.dir}">
			</fileset>
			<fileset dir="${project.lib.dir}">
			</fileset>
		</copy>
		<copy tofile="${deploy.dir}/WEB-INF/web.xml" file="${web.inf.dir}/web-dev.xml" verbose="true" overwrite="true">
		</copy>
		<!-- 가우스 설정파일 -->
		<!-- <copy tofile="${deploy.dir}/WEB-INF/gauce.properties" file="${web.inf.dir}/gauce50-dev.properties" verbose="true" overwrite="true">
		</copy> -->
		<!-- ant condition 을 이용해서 최초 한번만 weblogic.xml 파일을 복사한다 -->
		<condition property="check-file">
			<not>
				<available file="${deploy.dir}/WEB-INF/weblogic.xml" />
			</not>
		</condition>
		<copy tofile="${deploy.dir}/WEB-INF/weblogic.xml" file="${web.inf.dir}/weblogic.xml" verbose="true" overwrite="true">
		</copy>
	</target>
	<!-- sql xml filse copy -->

	<target name="copy-sql">
		<delete includeemptydirs="true" quiet="true">
			<fileset dir="${backoffice.sql.dir}" includes="**/*">
			</fileset>
		</delete>
		<copy todir="${backoffice.sql.dir}" overwrite="true">
			<fileset dir="${dist.sqlx.dir}">
				<include name="**/sql/**/**.sqlx" />
			</fileset>
		</copy>
		<copy todir="${backoffice.sql.dir}" overwrite="true">
			<fileset dir="${basedir}/src">
				<include name="**/**.sqlx" />
			</fileset>
		</copy>
	</target>

	<!-- config xml filse copy -->
	<target name="copy-config">
		<mkdir dir="${backoffice.config.dir}" />
		<copy todir="${backoffice.config.dir}" verbose="true">
			<fileset dir="${basedir}/src/config" excludes="**/.svn">
			</fileset>
		</copy>
		<copy file="${dist.config.dir}/spring/bean/bean-common-dev.xml" tofile="${backoffice.config.dir}/spring/bean/bean-common.xml" overwrite="true" />
		<copy file="${backoffice.config.dir}/system/applicationProperties-dev.xml" tofile="${backoffice.config.dir}/system/applicationProperties.xml" overwrite="true" />
		<copy file="${backoffice.config.dir}/spring/bean/bean-application-dev.xml" tofile="${backoffice.config.dir}/spring/bean/bean-application.xml" overwrite="true" />
		<copy file="${backoffice.config.dir}/log4j/log4j-dev.xml" tofile="${backoffice.config.dir}/log4j/log4j.xml" overwrite="true" />
	</target>

	<!-- archive web resources  -->
	<target name="release-resource">
		<!--<zip destfile="${build.dir}/${web.archive.name}.${web.archive.ext}" basedir="${web.dir}" excludes="**/META-INF/**,**/WEB-INF/**,**/*.jsp">
		</zip> -->
		<ftp server="52.208.118.81" port="21" userid="apache" password="qwer!@#$" action="put" binary="true" remotedir="${web.archive.ftp.remotedir}/${projectname}/${runtime.contextname}" verbose="true">
			<fileset dir="${web.dir}" excludes="**/META-INF/**,**/WEB-INF/**,**/*.jsp">
			</fileset>
		</ftp>
	</target>

	<!-- explode web resources  -->
	<target name="explode-web">
		<mkdir dir="${web.archive.ftp.remotedir}/${projectname}/${runtime.contextname}" />
		<unzip src="${runtime.webstatic}/${web.archive.name}.${web.archive.ext}" dest="${web.archive.ftp.remotedir}/${projectname}/${runtime.contextname}">
		</unzip>
	</target>

	<!-- explode web resources - both hudson and webserver in single machine -->
	<target name="dist-web">
		<mkdir dir="${web.archive.ftp.remotedir}/${projectname}/${runtime.contextname}" />
		<copy todir="${web.archive.ftp.remotedir}/${projectname}/${runtime.contextname}">
			<fileset dir="${web.dir}" excludes="**/META-INF/**,**/WEB-INF/**,**/*.jsp">
			</fileset>
		</copy>
	</target>
</project>

这个build.xml 的部署场景是:从svn上下载源代码,将编译后的classes文件存储在某个位置,编译完成后,将动态代码(classes\jsp等) 部署到web容器中,将WebContent下面的静态资源发布到内网另外一个apache服务器上。(web容器为weblogic、静态资源服务器为apache)


--------------------------

(完)



上一篇:超燃!Apache Flink 全球*盛会强势来袭


下一篇:Java web 开发填坑记 2 -如何正确的创建一个Java Web 项目