Ant构建maven项目到tomcat
#Ant  #maven  #tomcat  #Java 


放弃tomcat插件的选择

之前项目通过tomcat插件各种编译不了,搭建出错,果断放弃了。还是选择自由的自己启动tomcat,通过ant把maven项目自动部署到tomcat,省去许多麻烦。

准备:ant-contrib-1.0b3.jar maven-ant-tasks-2.1.2.jar

还需要配置build.xml build.properties

附:build.xml

<project name="converse-frontend" basedir="." default="deploy" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
	<!-- 读取build.properties -->
	<property file="build.properties" />
	<!-- 引入需要的jar包,在项目中引入 -->
	<path id="maven-ant-tasks.classpath" path="${basedir}/lib/ant/maven-ant-tasks-2.1.2.jar" />
	<path id="ant-contrib.classpath" path="${basedir}/lib/ant/ant-contrib-1.0b3.jar" />

	<typedef resource="org/apache/maven/artifact/ant/antlib.xml" classpathref="maven-ant-tasks.classpath" uri="antlib:org.apache.maven.artifact.ant" />
	<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="ant-contrib.classpath" />

	<mkdir dir="${deploy.home}/WEB-INF/classes" />

	<!--定义Maven2变量 -->
	<artifact:pom id="feilongMaven" file="pom.xml" />
	<artifact:dependencies filesetId="feilong.maven.dependencies.fileset" pathid="feilong.maven.dependencies.runtime" useScope="runtime">
		<pom refid="feilongMaven" />
	</artifact:dependencies>

	<artifact:dependencies pathid="feilong.maven.dependencies.system" scopes="system">
		<pom refid="feilongMaven" />
	</artifact:dependencies>

	<artifact:dependencies pathid="feilong.maven.dependencies.all">
		<pom refid="feilongMaven" />
	</artifact:dependencies>


	<!-- 开发时常用, 和deploy-forHudson-nocompressorJSCSS 相比 ,不删除已经deploy的class 其实可以直接使用deploy-forHudson-nocompressorJSCSS 但是现有的hudson,很多已经有这些名称了,暂时不想更改 -->
	<target name="deploy" description="copyLib,maven-install,copyClass,copyJsp">
		<antcall target="copyLib" />
		<antcall target="maven-install" />
		<antcall target="copyClass" />
		<antcall target="copyJsp" />
	</target>


	<!-- install -->
	<target name="maven-install" description="install">
		<echo>[install]:ant use maven to install project</echo>
		<artifact:mvn pom="pom.xml">
			<arg value="clean" />
			<arg value="install" />
		</artifact:mvn>
	</target>

	<!-- copyClass -->
	<target name="copyClass">
		<delete dir="${deploy.home}/WEB-INF/classes" />
		<echo>[copyClass]:Compiled classes will be copied and no pre-compile works done.
			Please confirm the source codes are
			compiled successfully.</echo>
		<copy todir="${deploy.home}/WEB-INF/classes">
			<fileset dir="${feilongMaven.build.outputDirectory}" />
		</copy>
	</target>

	<!-- copyJsp -->
	<target name="copyJsp">
		<copy todir="${deploy.home}">
			<fileset dir="${basedir}/src/main/webapp" />
		</copy>
	</target>

	<target name="minjs">
		<delete dir="${basedir}/target/scripts">
			<include name="**/*.*" />
		</delete>
		<for param="file">
			<path>
				<fileset dir="${basedir}/src/main/webapp/scripts" includes="**/*.js" />
			</path>
			<sequential>
				<propertyregex override="true" property="filename" input="@{file}" regexp="(.*)[\\/](.*\.js)" replace="\2" />
				<propertyregex override="true" property="path" input="@{file}" regexp="(.*)webapp[\\/]scripts(.*)[\\/](.*\.js)" replace="\2" />
				<propertyregex override="true" property="min" input="@{file}" regexp="(.*)[\\/](.*)\.(.*)\.js" replace="\3" defaultvalue="--" />
				<if>
					<equals arg1="${min}" arg2="min" />
					<then>
						<echo level="warning">filename "${filename}" with ".min",will uncompressed,only copy</echo>
						<copy file="@{file}" todir="${basedir}/target/scripts" />
					</then>
					<else>
						<mkdir dir="${basedir}/target/scripts/${path}" />
						<java jar="${basedir}/lib/yuicompressor/yuicompressor-2.4.2.jar" dir="${basedir}/src/main/webapp/scripts/${path}" fork="true">
							<arg value="-v" />
							<arg value="${filename}" />
							<arg value="-o" />
							<arg value="${basedir}/target/scripts/${path}/${filename}" />
							<arg value="--charset" />
							<arg value="UTF-8" />
						</java>
					</else>
				</if>
			</sequential>
		</for>
	</target>

	<!-- copyLib -->
	<target name="copyLib" description="删除旧的jar,copy全部依赖的jar包">
		<echo>[copyLib]:will be copied maven dependencies jar(runtime and system scope)</echo>

		<echo>use:../${repo.name}/pom.xml</echo>

		<delete dir="${deploy.home}/WEB-INF/lib" />
		<mkdir dir="${deploy.home}/WEB-INF/lib" />

		<artifact:mvn pom="../${repo.name}/pom.xml">
			<arg value="clean" />
			<arg value="install" />
		</artifact:mvn>

		<copy todir="${deploy.home}/WEB-INF/lib">
			<fileset refid="feilong.maven.dependencies.fileset" />
			<mapper type="flatten" />
		</copy>

		<for param="mavenSystemJar">
			<path refid="feilong.maven.dependencies.system" />
			<sequential>
				<echo>mavenSystemJar:@{mavenSystemJar}</echo>
				<copy todir="${deploy.home}/WEB-INF/lib" file="@{mavenSystemJar}" />
			</sequential>
		</for>
	</target>

	<target name="copyJs" depends="minjs">
		<copy todir="${deploy.home}/scripts">
			<fileset dir="${basedir}/target/scripts" />
		</copy>
	</target>

	<!-- minCss -->
	<target name="minCss">
		<mkdir dir="${basedir}/target/css" />
		<delete dir="${basedir}/target/css">
			<include name="**/*.*" />
		</delete>
		<for param="file">
			<path>
				<fileset dir="${basedir}/src/main/webapp/css" includes="**/*.css" />
			</path>
			<sequential>
				<propertyregex override="true" property="filename" input="@{file}" regexp="(.*)[\\/](.*\.css)" replace="\2" />
				<propertyregex override="true" property="path" input="@{file}" regexp="(.*)webapp[\\/]css(.*)[\\/](.*\.css)" replace="\2" />
				<propertyregex override="true" property="min" input="@{file}" regexp="(.*)[\\/](.*)\.(.*)\.css" replace="\3" defaultvalue="--" />
				<if>
					<equals arg1="${min}" arg2="min" />
					<then>
						<echo level="warning">filename "${filename}" with ".min",will uncompressed,only copy</echo>
						<copy file="@{file}" todir="${basedir}/target/css" />
					</then>
					<else>
						<mkdir dir="${basedir}/target/css/${path}" />
						<java jar="${basedir}/lib/yuicompressor/yuicompressor-2.4.2.jar" dir="${basedir}/src/main/webapp/css/${path}" fork="true">

							<arg value="${filename}" />
							<arg value="-o" />
							<arg value="${basedir}/target/css/${path}/${filename}" />
							<arg value="--charset" />
							<arg value="UTF-8" />
						</java>
					</else>
				</if>
			</sequential>
		</for>
	</target>

	<!-- copyCss -->
	<target name="copyCss" depends="minCss">
		<copy todir="${deploy.home}/css">
			<fileset dir="${basedir}/target/css" />
		</copy>
	</target>


	<target name="about" description="about this build.xml">
		<echo>
	[${ant.project.name}] Ant build.xml:
	Brandstore Version:[${feilongMaven.version}]
	Your ant version:[${ant.version}]
			
	Author:[JinXin] (Learn from benjamin)
	Any questions you can contact him.
		</echo>
	</target>
</project>

附:build.properties

deploy.home=E:/apache-tomcat-6.0.37/webapps/mytest
repo.name=converse-repo

在运行ant的时候,遇到了下面的错误:

-source 1.3 中不支持注释 (请使用 -source 5 或更高版本以启用注释)

解决方案:主要是maven编译的环境存在问题(更新pom.xml)

pom.xml新增部分:

<build>
	<finalName>test2</finalName>
	<plugins> 
		  <plugin> 
			    <groupId>org.apache.maven.plugins</groupId> 
			    <artifactId>maven-compiler-plugin</artifactId> 
			    <configuration> 
				      <source>1.5</source> 
				      <target>1.5</target> 
			    </configuration> 
		  </plugin> 
	</plugins>
</build>

保存后即可解决。

Published under (CC) BY-NC-SA