使用Maven管理依赖JAR文件,自定义项目布局,利用ANT生成不同的发布包

<?xml version="1.0" encoding="GB2312"?>
<project name="CardInfo-WebApp" default="build_all" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">
	
	<property file="build.properties"/>
	
	<target name="build_all" depends="zip, war, war_patch, zip_library" description="build project and create zip file of publish.">
		<delete includeemptydirs="true" dir="${target.basedir}" includes="**/*" excludes="/*.zip, /*.war" />
	</target>
	
	<target name="build_app" depends="zip" description="build project and create zip file of publish.">
		<delete includeemptydirs="true" dir="${target.basedir}" includes="**/*" excludes="/*.zip" />
	</target>
	
	<target name="build_war" depends="war" description="build project and create war file of publish.">
		<delete includeemptydirs="true" dir="${target.basedir}" includes="**/*" excludes="/*.war" />
	</target>
	
	<target name="build_war_patch" depends="war_patch" description="build project and create war file of publish.">
		<delete includeemptydirs="true" dir="${target.basedir}" includes="**/*" excludes="/*.war" />
	</target>
	
	<target name="build_library" depends="zip_library" description="copy jar resource and create zip file.">
		<delete includeemptydirs="true" dir="${target.basedir}" includes="**/*" excludes="/*.zip" />
	</target>
	
	<tstamp prefix="build.">
		<format property="time" pattern="yyyy-MM-dd HH:mm" />
		<format property="timestamp" pattern="yyyyMMddHHmm"/>
	</tstamp>
	
	<target name="init_maven">
		<echo message="init maven config. local repository is: ${M2_REPO}"/>
		<path id="maven-ant-tasks.classpath" path="${M2_REPO}/org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.jar" />
		<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" />
		
		<artifact:localRepository id="local.repository" path="${M2_REPO}"/>
		
		<artifact:pom id="maven.project" file="pom.xml" />
		
		<artifact:dependencies pathId="classpath.build" filesetid="maven.fileset.build">
			<localRepository refid="local.repository"/>
			<pom refid="maven.project" />
		</artifact:dependencies>

		<artifact:dependencies pathId="classpath.runtime" filesetid="maven.fileset.runtime" usescope="runtime">
			<localRepository refid="local.repository"/>
			<pom refid="maven.project"/>
		</artifact:dependencies>
		
		<artifact:dependencies pathId="classpath.provided" filesetid="maven.fileset.provided" scopes="provided">
			<localRepository refid="local.repository"/>
			<pom refid="maven.project" />
		</artifact:dependencies>
	</target>

	<target	name="init_path" depends="init_maven">
		<echo>*********************************</echo>
		<echo>Maven build project base directory is ${maven.project.basedir}</echo>
		<echo>Maven build project source directory is ${maven.project.build.sourceDirectory}</echo>
			
		<echo>*********************************</echo>
		<property name="project.application.basedir" value="${maven.project.basedir}/Application" />
		<echo>Maven build project application directory is ${project.application.basedir}</echo>
		
		<property name="project.path.tools" value="${project.application.basedir}}/tools"/>
		<echo>Maven build project application tools directory is ${project.path.tools}</echo>
		
		<property name="project.path.webapps" value="${project.application.basedir}/webapps"/>
		<echo>Maven build project application webapps directory is ${project.path.webapps}</echo>
		<property name="project.path.webapps.webinf" value="${project.path.webapps}/WEB-INF"/>
		
		<property name="project.path.conf" value="${project.path.webapps.webinf}/conf"/>
		<echo>Maven build project application conf directory is ${project.path.conf}</echo>
		
		<property name="project.path.lib" value="${project.path.webapps.webinf}/lib"/>
		<echo>Maven build project application jar lib directory is ${project.path.lib}</echo>
		
		<property name="project.path.log" value="${project.path.webapps.webinf}/log"/>
		<echo>Maven build project application log directory is ${project.path.log}</echo>
		
		<property name="project.path.workspace" value="${project.path.webapps.webinf}/workspace"/>
		<echo>Maven build project application workspace directory is ${project.path.workspace}</echo>

		<echo>*********************************</echo>
		<property name="target.basedir" value="${maven.project.basedir}/target"/>
		<mkdir dir="${target.basedir}"/>
		<echo>Maven build target base directory is ${target.basedir}</echo>
		
		<property name="target.path.build" value="${target.basedir}/build"/>
		<mkdir dir="${target.path.build}"/>
		<echo>Maven build target build directory is ${target.path.build}</echo>
		
		<property name="target.path.classes" value="${target.path.build}/classes"/>
			<mkdir dir="${target.path.classes}"/>
		<echo>Maven build target classes directory is ${target.path.classes}</echo>
		
		<property name="target.path.tools" value="${target.path.build}/tools"/>
		<mkdir dir="${target.path.tools}"/>
		<echo>Maven build target tools directory is ${target.path.tools}</echo>
		
		<property name="target.path.webapps" value="${target.path.build}/webapps"/>
		<mkdir dir="${target.path.webapps}"/>
		<echo>Maven build target webapps directory is ${target.path.webapps}</echo>
		
		<property name="target.path.webapps.webinf" value="${target.path.webapps}/WEB-INF"/>
		<mkdir dir="${target.path.webapps.webinf}"/>
		
		<property name="target.path.conf" value="${target.path.webapps.webinf}/conf"/>
		<mkdir dir="${target.path.conf}"/>
		<echo>Maven build target conf directory is ${target.path.conf}</echo>
		
		<property name="target.path.lib" value="${target.path.webapps.webinf}/lib"/>
		<mkdir dir="${target.path.lib}"/>
		<echo>Maven build target lib directory is ${target.path.lib}</echo>
		
		<property name="target.path.log" value="${target.path.webapps.webinf}/log"/>
		<mkdir dir="${target.path.log}"/>
		<echo>Maven build target log directory is ${target.path.log}</echo>
		
		<property name="target.path.webapps.classes" value="${target.path.webapps.webinf}/classes"/>
		<mkdir dir="${target.path.webapps.classes}"/>
		
		<property name="target.path.webapps.template" value="${target.path.webapps.webinf}/template"/>
		<mkdir dir="${target.path.webapps.template}"/>
		<echo>Maven build target template directory is ${target.path.webapps.template}</echo>
		
		<property name="target.path.workspace" value="${target.path.webapps.webinf}/workspace"/>
		<mkdir dir="${target.path.workspace}"/>
		<echo>Maven build target workspace directory is ${target.path.workspace}</echo>
	</target>
	
	<target name="init_build_var" depends="init_maven">
		<property name="compile.debug" value="false" />
		<property name="compile.deprecation" value="false" />
		<property name="compile.optimize" value="true" />
		<property name="compile.encoding" value="UTF-8" />
		<property name="compile.sourceversion" value="1.6" />
		<property name="compile.targetversion" value="1.6" />
		
		<property name="build.version" value="${maven.project.version}-Build${build.timestamp}"/>
		<echo>Maven build version is ${build.version}</echo>
		
		<property name="patch.version" value="${maven.project.version}-Patch${build.timestamp}"/>
		<echo>Maven build version is ${build.version}</echo>
		
		<property name="build.jar.name" value="${maven.project.artifactId}.jar" />
		<echo>Maven build target zip file name is ${build.zip.name}</echo>

		<property name="build.zip.name" value="${maven.project.artifactId}-${build.version}.zip" />
		<echo>Maven build target zip file name is ${build.zip.name}</echo>
		
		<property name="build.war.name" value="${maven.project.artifactId}-${build.version}.war" />
		<echo>Maven build target war file name is ${build.war.name}</echo>
		
		<property name="patch.war.name" value="${maven.project.artifactId}-${patch.version}.war" />
		<echo>Maven build target patch-war file name is ${patch.war.name}</echo>
	</target>
	
	<target name="compile" description="compile java source to target classes path." depends="init_path, init_build_var">
		<echo>compile java source.</echo>
		<echo>source version is ${compile.sourceversion}</echo>
		<echo>target version is ${compile.targetversion}</echo>
		<javac srcdir="${maven.project.build.sourceDirectory}" destdir="${target.path.webapps.classes}"
			source="${compile.sourceversion}" target="${compile.targetversion}"
			description="${compile.deprecation}" optimize="${compile.optimize}"
			debug="${compile.debug}" encoding="${compile.encoding}"
		>
			<classpath refid="classpath.build"/>
			<classpath refid="classpath.provided"/>
			<classpath refid="classpath.runtime"/>
		</javac>
		<!-- jar destfile="${target.path.lib}/${build.jar.name}" basedir="${target.path.classes}">
			<manifest>
				<attribute name="Version" value="${maven.project.version}" />
				<attribute name="Build-by" value="${build.timestamp}" />
			</manifest>
		</jar -->
	</target>
	
	<target name="copy_library" description="copy jar library to target lib path" depends="init_path">
		<copy todir="${target.path.lib}">
			<fileset refid="maven.fileset.runtime"/>
			<fileset dir="${project.path.lib}" />
			<mapper type="flatten" />
		</copy>
	</target>
	
	<target name="copy_resource" description="copy resource file to target classes path" depends="init_path">
		<copy todir="${target.path.webapps.classes}" includeemptydirs="false">
			<fileset dir="${maven.project.build.sourceDirectory}">
				<exclude name="**/*.java"/>
			</fileset>
		</copy>
		<copydir dest="${target.path.webapps}" src="${project.path.webapps}" excludes="/WEB-INF/**/*"/>
		<copydir dest="${target.path.webapps.webinf}" src="${project.path.webapps.webinf}">
			<exclude name="/classes/**/*"/>
			<exclude name="/work/**/*"/>
			<exclude name="/tmp/**/*"/>
			<exclude name="/temp/**/*"/>
		</copydir>
		<copydir dest="${target.path.build}" src="${project.application.basedir}">
			<exclude name="/webapps/**/*"/>
			<exclude name="/${target.path.classes}"/>
		</copydir>
	</target>
	<target name="copy_resource_noconfig" description="copy resource file to target classes path" depends="init_path">
		<copy todir="${target.path.webapps.classes}">
			<fileset dir="${maven.project.build.sourceDirectory}">
				<exclude name="**/*.java"/>
			</fileset>
		</copy>
		<copydir dest="${target.path.webapps}" src="${project.path.webapps}" excludes="/WEB-INF/**/*"/>
		<copydir dest="${target.path.webapps.webinf}" src="${project.path.webapps.webinf}">
			<exclude name="/classes/**/*"/>
			<exclude name="/work/**/*"/>
			<exclude name="/tmp/**/*"/>
			<exclude name="/temp/**/*"/>
			<exclude name="/conf/**/*"/>
			<exclude name="/log/**/*"/>
			<exclude name="/workspace/**/*"/>
			<exclude name="/lib/**/*"/>
		</copydir>
		<copydir dest="${target.path.build}" src="${project.application.basedir}">
			<exclude name="/webapps/**/*"/>
			<exclude name="/${target.path.classes}"/>
		</copydir>
	</target>
		
	<target	name="clean" description="clean build directory." depends="init_path">
		<echo>clean	build directory	: ${target.basedir}</echo>
		<delete includeemptydirs="true" dir="${target.basedir}" includes="**/*" />
	</target>
	
	<target name="zip" description="create published zip file" depends="init_path, init_build_var, compile, copy_library, copy_resource">
        <zip destfile="${target.basedir}/${build.zip.name}">
            <fileset dir="${target.path.build}"></fileset>
        </zip>
    </target>
	<target name="zip_library" description="create published zip file" depends="init_path, init_build_var, copy_library">
		<zip destfile="${target.basedir}/library.zip">
            <fileset dir="${target.path.lib}"></fileset>
        </zip>
    </target>
	
	<target name="war" description="create published war file" depends="init_path, init_build_var, compile, copy_library, copy_resource">
		<war destfile="${target.basedir}/${build.war.name}">
			<fileset dir="${target.path.webapps}"></fileset>
			<manifest>
				<attribute name="Version" value="${maven.project.version}" />
				<attribute name="Build-by" value="${build.timestamp}" />
			</manifest>
		</war>
	</target>
	
	<target name="war_patch" description="create published war file for update" depends="init_path, init_build_var, compile, copy_resource_noconfig">
		<war destfile="${target.basedir}/${patch.war.name}">
			<fileset dir="${target.path.webapps}"></fileset>
			<manifest>
				<attribute name="Version" value="${maven.project.version}" />
				<attribute name="Build-by" value="${build.timestamp}" />
			</manifest>
		</war>
	</target>
</project>


posted @ 2013-06-28 18:55  坚固66  阅读(487)  评论(0编辑  收藏  举报