eclipse中ant打war包


注:有可能打包失敗是eclipse沒有集成的原因,用我自己的eclipse可以,同事那就有可能打包失敗,一定要注意這點。

 

1、先在eclipse中集成或自己安裝解壓ant,總之有可用的ant就好。

2、照下圖新建extlib(比如servlet-api.jar是tomcat里的包,ant打包需要但是引用不到的包都放入extlib文件夾),war(打成功的war包的位置)文件夾。

3、編寫build.xml文件,內容如下:  

<?xml version="1.0" encoding="UTF-8"?>

<project name="GBEMSSystemMGR" default="deploy" basedir="."><!--GBEMSSystemMGR是項目名稱-->
    
    

    <!-- 判斷當前系統是windows還是linux  -->
    <condition property="isWindows">
        <os family="windows" />
    </condition>

    <condition property="isLinux">
        <os family="unix" />
    </condition>

    <!-- 定義了一些變量 -->    
    <property name="resource.dir" location="${basedir}/resources" />
    <property name="src.dir" location="${basedir}/src" />
    <property name="web.dir" location="${basedir}/WebRoot" />
    <property name="web.web-inf.dir" location="${web.dir}/WEB-INF" />
    <property name="lib.dir" location="${web.web-inf.dir}/lib" />
    <property name="classes.dir" location="${web.web-inf.dir}/classes" />
    <property name="ext.dir" location="${basedir}/extlib" />

    
    <!--定義一個時間戳-->
    <tstamp prefix="backup">
        <format property="time" pattern="yyyy-MM-dd.HH.mm.ss" />
    </tstamp>

    

    <!--path表示一個文件或路徑名列表-->
    <path id="classpath">
        <!--Fileset 數據類型定義了一組文件-->
        <fileset dir="${lib.dir}">
        <!--該文件夾下所有以.jar結尾的文件-->
            <include name="*.jar" />
        </fileset>
        <!--Fileset 數據類型定義了一組文件-->
        <fileset dir="${ext.dir}">
        <!--該文件夾下所有以.jar結尾的文件-->
            <include name="*.jar" />
        </fileset>
        
    </path>

    <property name="war.file.path" location="${basedir}/war" />
    <property name="war.file.name" value="GBEMSSystemMGR.war" />


    <!-- ================================= 
          target: deploy              
         ================================= -->
    <target name="deploy" depends="clean-classes-dir,copy-resource-to-classes,full-compile,war-app,deploy-under-windows">
        <echo>now you can start tomcat.</echo>
    </target>


    <target name="deploy-under-windows" if="isWindows">
        <!-- echo 往控制台輸出一段話 -->
        <echo>一般使用Eclipse集成的tomcat進行測試,省略</echo>

    </target>


    <!-- ================================= target: war-app ================================= --> <target name="war-app"> <echo>make War ..</echo> <mkdir dir="${war.file.path}" /> <!--將指定文件打成war包--> <war warfile="${war.file.path}/${war.file.name}" webxml="${web.web-inf.dir}/web.xml"> <lib dir="${lib.dir}" /> <classes dir="${classes.dir}" /> <fileset dir="${web.dir}"> </fileset> </war> <echo>War Success : ${war.file.path}/${war.file.name}</echo> </target> <!-- ================================= target: full-compile ================================= --> <target name="full-compile" description="description"> <echo>start compile.</echo> <!--編譯,其中refild標簽是引用之前定義的name為classpath的path文件或路徑--> <javac encoding="utf-8" srcdir="${src.dir}" destdir="${classes.dir}" includeAntRuntime="false" debug="true" > <classpath refid="classpath" /> </javac> <javac encoding="utf-8" srcdir="${resource.dir}" destdir="${classes.dir}" includeAntRuntime="false" debug="true"> <classpath refid="classpath" /> </javac> <echo>full-compile successfully.</echo> </target> <!-- copy src/**/*.(xml|properties ...) to classes dir --> <target name="copy-resource-to-classes"> <!--將指定文件拷貝到指定目錄--> <copy todir="${classes.dir}"> <fileset dir="${src.dir}"> <!-- 表示除了以.java結尾的文件都包含--> <exclude name="**/*.java" /> </fileset> </copy> <copy todir="${classes.dir}"> <fileset dir="${resource.dir}"> <!-- 表示除了以.java結尾的文件都包含--> <exclude name="**/*.java" /> </fileset> </copy> </target> <!-- 刪除整個classes目錄 --> <target name="clean-classes-dir"> <delete dir="${classes.dir}" /> <echo>${classes.dir} deleted.</echo> </target> </project>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM