1. 程式人生 > >Ant學習例子2(用ant釋出web專案,及連線資料庫)

Ant學習例子2(用ant釋出web專案,及連線資料庫)


<project>
    
    <property name="build.dir" location="build"></property>
    <property name="src.dir" location="src"></property>
    <property name="build.classes" location="${build.dir}/classes"></property>
    <property name="build.war" location="${build.dir}/war"></property>

    <property name="web.name" value="hello"></property>
    <property name="web.root" value="WebRoot"></property>
    <property name="web.WEB-INF" location="${web.root}/WEB-INF"></property>
    <property name="web.lib" location="${web.WEB-INF}/lib"></property>
    <!--environment="env" 用${env.xxx}可以引用相應的系統變數或者環境變數等等。如${env.TOMCAT_HOME}就引用TOMCAT_HOME-->
    <property environment="env"></property>
    
    <!--lib所在的路徑,及tomcat的lib所在的路徑-->
    <path id="complie">
        <fileset dir="${web.lib}" includes="*.jar"></fileset>
        <fileset dir="${tomcat_home}/lib" includes="*.jar"></fileset>
    </path>
    
    <target name="init">
        <delete dir="${build.dir}"></delete>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.classes}"/>
        <mkdir dir="${build.war}"/>
    </target>
    
    <!--編譯-->
    <target name="complie" depends="init" >
        <javac destdir="${build.classes}" srcdir="${src.dir}" classpathref="complie"></javac>
    </target>
    
    <!--把編譯好的檔案打成war包-->
    <target name="war" depends="complie">
        <war destfile="${build.war}/${web.name}.war" basedir="${build.classes}">
            <fileset dir="${web.root}" includes="**/*.*"></fileset>
            <lib dir="${web.lib}"></lib>
            <webinf dir="${web.WEB-INF}"></webinf>
            <classes dir="${build.classes}"></classes>
        
        </war>
    </target>
    <!--把war包拷貝到webapps目錄下,啟動tomcat就可以訪問專案了-->
    <target name="deploy" depends="war">
            <copy todir="${env.TOMCAT_HOME}/webapps" >
                <fileset dir="${build.war}" includes="${web.name}.war"></fileset>
            </copy>
    </target>

    <!--MySql 連線-->
    <presetdef name="sql-admin">
        <sql driver="com.mysql.jdbc.Driver" password="123" url="jdbc:mysql://localhost:3300/test" userid="123">
            <classpath refid="complie"></classpath>
        </sql>
    </presetdef>
    <!--SQLServer 連線-->
    <!--
    <presetdef name="sql-admin">
            <sql driver="com.microsoft.jdbc.sqlserver.SQLServerDriver" password="123" url="jdbc:sqlserver://localhost:1433;SelectMethod=Cursor;DatabaseName=test" userid="123">
                <classpath refid="complie"></classpath>
            </sql>
            
    </presetdef>
    -->
    
    <target name="init-mysql">
        <sql-admin>
            <transaction src="${build.dir}/mysql/mysql.sql"><!--可以用src引用外部的sql檔案,也可以把語句直接寫在下面-->
                 create table ant2(id int);
            </transaction>
        </sql-admin>
    </target>
    <!--echo測試-->
    <target name="echo_test">
    
    <property environment="env"></property>
    <echo>${env.TOMCAT_HOME}</echo>
    <echo>${env.OS}</echo>
    <echo>${env.JAVA_HOME}</echo>
    
    </target>
    
</project>