首页 > linux, 工具脚本 > 自用ant+svn+tomcat自动部署脚本

自用ant+svn+tomcat自动部署脚本

分享一个自用的ant+svn+tomcat自动部署的脚本,自动从svn中下载代码、编译、打包、部署、运行;可以用它,配合Linux的crontab来实现daily build的效果。

PS:需要用到svnjavahl.jarsvnant.jarsvnClientAdapter.jarsvnkit.jar这几个svn的库,把它们放到$ANT_HOME/lib下即可。

 

build.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." name="Server" default="auto">
	<!--  all properties are in build.properties -->
	<property file="build.properties" />

	<!-- svn runtime libs -->
	<path id="svnant.lib">
		<pathelement location="${svnjavahl.jar}" />
		<pathelement location="${svnant.jar}" />
		<pathelement location="${svnClientAdapter.jar}" />
	</path>

	<!--java EE 1.4 -->
	<path id="javaEE1.4">
		<fileset dir="${javaEE1.4.lib}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<!-- load the libs in classpath -->
	<path id="project.classpath">
		<pathelement location="${build.dir}" />
		<fileset dir="${lib.dir}" />
	</path>

	<!-- clean up -->
	<target name="clear">
		<delete dir="${work.space}" />
		<delete dir="${tomcat.home}/work/Catalina/localhost/${ant.project.name}" />
		<delete dir="${tomcat.deploy}/${ant.project.name}" />
        <delete dir="${tomcat.deploy}/${ant.project.name}.war" />
	</target>

    <!-- load the svn task -->
    <taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask" classpathref="svnant.lib" />
	<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpath="svnant.jar" />
    <svnSetting id="svn.settings" javahl="false" svnkit="true" username="${svn.user}" password="${svn.passwd}" />
    <!--svn checkout-->
    <target name="svn" depends="clear">
        <mkdir dir="${work.space}"/>
        <svn refid="svn.settings">
            <checkout url="${urlRepos}" destPath="${work.space}" />
        </svn>
    </target>

    <!--compile the project-->
    <target name="compile" depends="svn" description="======compile project======">
        <echo message="compile==========>${ant.project.name}: ${ant.file}" />
        <mkdir dir="${build.dir}" />

        <copy includeemptydirs="false" todir="${build.dir}">
            <fileset dir="${java.source}" excludes="**/*.launch, **/*.java, config/*.*"/>
        </copy>
		<!-- 
        <copy includeemptydirs="false" todir="${build.dir}">
            <fileset dir="${java.config}" excludes="**/*.launch, **/*.java"/>
        </copy>
        -->
        <javac includejavaruntime="true" debug="true" debuglevel="${debuglevel}" destdir="${build.dir}" source="${source}" target="${target}" encoding="gbk">
            <src path="${java.source}" />
            <exclude name="config/"/>
            <classpath>
                <path refid="project.classpath">
                </path>
                <path refid="javaEE1.4">
                </path>
            </classpath>
        </javac>

		<!--
        <javac debug="true" debuglevel="${debuglevel}" destdir="${build.dir}" source="${source}" target="${target}" encoding="gbk">
            <src path="${java.config}" />
        </javac>    
		-->
    </target>

    <!--compress and pack -->
    <target name="war" depends="compile" description="======compress j2ee war file======">
        <mkdir dir="${dist.dir}" />
        <!--compress j2ee war file-->
        <war destfile="${war.file}" webxml="${web.dir}/WEB-INF/web.xml">
            <fileset dir="${web.dir}" />
            <classes dir="${build.dir}" />
            <lib dir="${lib.dir}" />
			<metainf dir="${web.dir}/META-INF">  
                <include name="*.xml"/>  
            </metainf>  
        </war>
    </target>

    <!--shutdowntomcat-->
    <target name="shutdowntomcat" description="========shutdowntomcat===========">
        <exec executable="${tomcat.home}/bin/shutdown.sh" failonerror="false"></exec>
        <sleep seconds="10"/>
    </target>

    <!--startuptomcat-->
    <target name="startuptomcat" description="========startuptomcat===========">
        <sleep seconds="5"/>
        <exec executable="${tomcat.home}/bin/startup.sh" failonerror="false"></exec>
    </target>

    <!--deploy to the tomcat project dir-->
    <target name="deploy" depends="war">
        <copy file="${war.file}" todir="${tomcat.deploy}" />
    </target>

    <!--Done,restart tomcat-->
    <target name="auto" depends="shutdowntomcat,deploy,startuptomcat">
        <echo message="All DONE!!!!" />
    </target>
</project>

 

build.properties文件

build.version=1.0.0

#you need these jars
svnant.jar=/usr/local/ant/lib/svnant.jar
svnClientAdapter.jar=/usr/local/ant/lib/svnClientAdapter.jar
svnjavahl.jar=/usr/local/ant/lib/svnjavahl.jar
javaEE1.4.lib=/usr/local/tomcat7/lib

#tomcat home
tomcat.home=/usr/local/tomcat7
#tomcat project deploy dir				
tomcat.deploy=${tomcat.home}/webapps

debuglevel=source,lines
target=1.6
source=1.6

work.space=/tmp/project
dist.dir=${work.space}

build.dir=${work.space}/WebContent/WEB-INF/classes
lib.dir=${work.space}/WebContent/WEB-INF/lib

java.source=${work.space}/src
java.config=${work.space}/src/config

web.dir=${work.space}/WebContent
resource.dir=${work.space}/resources

#tomcat information
war.file=${dist.dir}/${ant.project.name}.war

#project information in SVN 
urlRepos=svn://xxx.xxx.xxx/projectname
svn.user=user
svn.passwd=password