`
txin0814
  • 浏览: 218494 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

ant 将WEB工程打成WAR包并进行远程部署

    博客分类:
  • ant
阅读更多
公司最近要求我将本地项目编译,并发布到服务器上,在网上看了很久,只找到了有关ANT相关操作的,现将代码贴出希望到时候有人需要.
我还有一个问题,希望名位大虾帮我解决,如何利用ANT从TFS上自动获取项目源码?
<?xml version="1.0" encoding="utf-8"?>

<project name="xbcw" default="deploy">
	<!-- 
	<property name="dist" value="E:/txin0814/workspace1/myceaas/dist" />
	-->
	<property name="context-path" value="xbcw" />
	<!-- 必须保证TOMCAT在运行状态  服务器IP 端口 -->
	<property name="tomcat.manager.url" value="http://192.168.1.68:9090/manager/html" />
	<!-- TOMCAT用户名 -->
	<property name="tomcat.manager.username" value="admin" />
	<!-- TOMCAT密码 -->
	<property name="tomcat.manager.password" value="" />

	<!--//////////////////////////////////////////////////////// -->
	<!-- 建立目录结构  
	        project  
	            ... src                 JAVA源码编辑目录  
	            ... WebRoot             web文件存放地方  
	                ... WEB-INF   
	                    ...lib          jar包(类库)存放目录  
	            ... build               编译生成的class文件存放目录  
	            ... dist                war和javadoc存放目录  
	            ... build.xml           ant脚本  
	    -->

	<property name="src.dir" value="src" />
	<property name="lib.dir" value="WEB-INF/lib" />

	<property name="webRoot.dir" value="WebRoot" />
	<property name="web-inf.dir" value="${webRoot.dir}/WEB-INF" />

	<property name="build.dir" value="build" />

	<property name="dist.dir" value="dist" />


	<!-- 初始化 classpath -->
	<path id="project.classpath">
		<fileset dir="${webRoot.dir}/${lib.dir}">
			<include name="**/*.jar" />
		</fileset>

		<pathelement location="${build.dir}/classes" />
		<pathelement path="${java.class.path}" />
	</path>

	<!-- 删除之前的目录结构 -->
	<target name="clear">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
	</target>

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

	<!-- 编译Java代码 -->
	<target name="compile" depends="init" description="编译java文件">
		<javac srcdir="${src.dir}" destdir="${build.dir}/classes">
			<compilerarg line="-encoding GBK" />

		</javac>
		<copy todir="${build.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.xml" />
				<include name="**/*.jar" />
			</fileset>
		</copy>
		<copy todir="${web-inf.dir}/classes">
			<fileset dir="src">
				<include name="**/*.properties" />
				<include name="**/*.xml" />
			</fileset>
		</copy>
		<copy todir="${web-inf.dir}/classes">
			<fileset dir="${build.dir}/classes">
				<include name="**/*.*" />
			</fileset>
		</copy>
		
	</target>

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

	<!-- 打成war包, 名称默认为 项目名 -->
	<target name="dist" depends="compile" description="将工程打成war包">
		<war destfile="${dist.dir}/${ant.project.name}.war" basedir="${webRoot.dir}" webxml="${web-inf.dir}/web.xml" />
	</target>


	
	<!--//////////////////////////////////////////////////////// -->

	<target name="undeploy" description="Remove application in Tomcat">
		<echo message="undeploying to web ..." />
		<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />
		<undeploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}"
			path="/${context-path}" />
	</target>

	<target name="deploy" description="Install application in Tomcat" depends="dist">
		<echo message="deploying to web ..." />
		<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
		<deploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" 
			path="/${context-path}" war="${dist.dir}/xbcw.war" update="true" />
		<!-- file:${dist}/deploy.war -->
	</target>

	<target name="list">
		<echo message="list web tomcat ..." />
		<taskdef name="list" classname="org.apache.catalina.ant.ListTask" />
		<list url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" />
	</target>

</project>

如果JAVA文件引用了第三方JAR包,还要把所有的JAR全部引用进来
<path id="project.classpath">
	
	        <pathelement location="WebRoot/WEB-INF/classes"/>
	        <pathelement location="WebRoot/WEB-INF/lib/activation.jar"/>
	        <pathelement location="WebRoot/WEB-INF/lib/commons-codec-1.4.jar"/>
	        <pathelement location="WebRoot/WEB-INF/lib/commons-httpclient-3.1.jar"/>
	        <pathelement location="WebRoot/WEB-INF/lib/commons-logging-1.1.1.jar"/>
	        <pathelement location="WebRoot/WEB-INF/lib/jsoup-1.5.2.jar"/>
	        <pathelement location="WebRoot/WEB-INF/lib/mailapi.jar"/>
			<pathelement location="WebRoot/WEB-INF/lib/smtp.jar"/>
	    </path>


并在JAVAC中加对其进行CLASSPATH的引用
<javac srcdir="${src.dir}" destdir="${build.dir}/classes">
			<compilerarg line="-encoding GBK" />
			<classpath refid="project.classpath"/>
		</javac>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics