使用ant 打包war檔案

來源:互聯網
上載者:User

  遇到最無語的問題,java下的編譯辦法多樣,有來自ide的也有專門的編譯工具,什麼maven啊,ant啊。Ant是一個挺有名的,這次就遇到了這樣的項目需要部署,但是內建的build檔案居然不能用,先考慮了Myeclipse下如何使用ant,只要在project裡面設定一下就行了,記住去掉java builder選項。

     雖然正確設定了,並進行了build,但是沒有正常的編譯,而看看build.xml,雖然英文都懂,內容也熟悉,但是就是不清楚其原理,網上直接找一些war打包的build也不能直接用。看來沒辦法,只好認真的學一下ant指令碼了。

     其實ant就是一個make工具,而build.xml則是makefile。其中使用了xml的方式,比c下面的makefile寫起來更簡便,而且功能更加強大。

    

Code
<?xml version="1.0" encoding="UTF-8"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. -->
<project basedir="." default="build" name="項目名稱">
    <property environment="env"/>
    <property name="ECLIPSE_HOME" value="D:/Program Files/MyEclipse 6.5/eclipse"/>
    <property name="debuglevel" value="source,lines,vars"/>
    
    <property name="target" value="1.5"/>
    <property name="source" value="1.5"/>
    
    <target name="init">
    <property name="build" value="build"></property>
    <property name="src" value="src"></property>
        <mkdir dir="${build}"/>
        <mkdir dir="${build}\WEB-INF"/>
        <mkdir dir="${build}\WEB-INF\classes"/>
        <copy todir="${build}">
            <fileset dir="${basedir}\WebContent">
                
                <include name="WEB-INF/**" />
                <include name="*.jsp" />
                <include name="conf/**" />
                <include name="ext/**" />
                <include name="fusioncharts/**" />
                <include name="images/**" />
                <include name="jsp/**" />
                <include name="META-INF/**" />
                <include name="style/**" />
                <include name="temp/**" />
            </fileset>
        </copy>
    </target>
    <target name="complie" depends="init">
        <javac srcdir="${src}" destdir="${build}/WEB-INF/classes">
        </javac>
    
    </target>
    <target name="makewar" depends="complie">
        <war warfile="${build}/test.war" webxml="${build}/WEB-INF/web.xml">
        <lib dir="${build}/WEB-INF/lib"/>
        <classes dir="${build}/WEB-INF/classes"/>
        <fileset dir="${build}"/>
        </war>
    
    </target>
     
</project>

簡單寫出了如上的build.xml,其實3個target一環套一環的進行了依賴,但是要執行,還要加上參數 ,參數則是target中的name了,在project中設定一下即可,這樣就能正常打包war檔案。其中關鍵的還是要對實際項目熟悉,知道要打包的內容。

 總結:通過簡單使用ant,雖然沒有寫出多麼高明的build指令碼,但是可以發現,很多東西去找現成的是不可靠的,尤其是還沒搞明白原理的情況下,只會浪費更多的時間。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.