日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

      首頁編程開發(fā)java → Java工程自動化構(gòu)建、部署工具Ant使用教程

      Java工程自動化構(gòu)建、部署工具Ant使用教程

      前往專題相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2012/11/30 10:10:43字體大。A-A+

      作者:西西點擊:0次評論:0次標(biāo)簽: 部署

      • 類型:編程輔助大小:134KB語言:中文 評分:3.3
      • 標(biāo)簽:
      立即下載

           Ant是一個用于簡單或復(fù)雜Java工程的自動化構(gòu)建、部署工具,它對于那些具有分布式開發(fā)團隊或者相信通過頻繁的構(gòu)建來進行不間斷集成的公司尤其有用。對于那些建立傳統(tǒng)全Java應(yīng)用程序以及那些使用HTML、JSP和Java servlets創(chuàng)建Web應(yīng)用程序的公司來說,Ant極具價值。無論你的Java開發(fā)者使用什么操作系統(tǒng)、集成開發(fā)環(huán)境或者構(gòu)建環(huán)境,Ant都可以將你的工程集合在一起,用于那些重要的構(gòu)建。Ant也能夠自動化并且同步文檔部署,這通常發(fā)生在軟件開發(fā)過程中的沒有正式文檔和文檔比較混亂的部分。

           在構(gòu)建和部署Java應(yīng)用程序的時候,Ant處理著大量有用的任務(wù)。最基本的任務(wù)包括添加和移除目錄、使用FTP拷貝和下載文件、創(chuàng)建JAR和ZIP文件以及創(chuàng)建文檔。更高級的特性包括用源代碼控制系統(tǒng)諸如CVS或者SourceSafe來檢查源代碼、執(zhí)行SQL查詢或腳本、將XML文件轉(zhuǎn)換為人能識別的HTML,以及為遠(yuǎn)程方法調(diào)用生成stub(存根)文件。

           Ant和Make(非常著名的構(gòu)建工具,很多c語言開發(fā)人員都使用它)之間有什么不同?Ant是為Java而創(chuàng)建,帶有屬于其自身的、獨特的范例,具有可移植性。而Make依賴于固定的操作系統(tǒng)命令(因此一個運行在微軟Windows下的Make文件對于使用UNIX的開發(fā)者來說毫無用處),利用Ant構(gòu)建的純Java工程是可移植的,因為Ant本身就是用Java編寫的,并且Ant bulidfiles使用XML語法。

      1.下載及安裝

      Apache Ant是一個基于Java的構(gòu)建工具,滿足跨平臺使用。

      下載及安裝:http://ant.apache.org/

      解壓下載的Ant拷貝到任意目錄以D:\Program Files\apache-ant-1.8.4為例

      將D:\Program Files\apache-ant-1.8.4配置為系統(tǒng)變量ANT_HOME

      在PATH中加入%ANT_HOME%\bin

      命令提示符中輸入ant相關(guān)命令可以查看是否配置正確。

      2.簡單實例

      實例:編譯運行一個簡單的類。

      編寫一個簡單的HelloWorld

      public class HelloWorld{
      public static void main(String[] args){
      System.out.println("Hello, ANT!!!");
      }
      }

      建立build.xml(名稱可改)

      HelloWorld.java和build.xml所在目錄示意:

      ├src

      │ └HelloWorld.java

      └build.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <project name="antStudyProj" default="run" basedir=".">
      <property name="classdir" value="build/classes"/>
      <echo message="Learn Ant!!!"/>
      <target name="clean">
      <delete dir="${classdir}"/>
      </target>

      <target name="compile" depends="clean">
      <mkdir dir="${classdir}" />
      <javac srcdir="src" destdir="${classdir}" includeantruntime="false"/>
      </target>
      <target name="run" depends="compile">
      <java classname="HelloWorld">
      <classpath path="${classdir}"/>
      </java>
      </target>
      </project>

      運行Ant,查看結(jié)果(可以通過ant -help查看ant命令的使用)

          這里使用ant -buildfile build.xml文件的絕對路徑來運行,結(jié)果如下:

          注意:可以通過其他方式也可運行ant,ant的入口為org.apache.tools.ant.launch.Launcher,里面包含mian方法,可指定執(zhí)行該類并傳入?yún)?shù)。

      3.buildfile介紹

      這是官網(wǎng)的build.xml示例

      <project name="MyProject" default="dist" basedir=".">
      <description>
      simple example build file
      </description>
      <!-- set global properties for this build -->
      <property name="src" location="src"/>
      <property name="build" location="build"/>
      <property name="dist" location="dist"/>

      <target name="init">
      <!-- Create the time stamp -->
      <tstamp/>
      <!-- Create the build directory structure used by compile -->
      <mkdir dir="${build}"/>
      </target>

      <target name="compile" depends="init"
      description="compile the source " >
      <!-- Compile the java code from ${src} into ${build} -->
      <javac srcdir="${src}" destdir="${build}"/>
      </target>

      <target name="dist" depends="compile"
      description="generate the distribution" >
      <!-- Create the distribution directory -->
      <mkdir dir="${dist}/lib"/>

      <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
      <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
      </target>

      <target name="clean"
      description="clean up" >
      <!-- Delete the ${build} and ${dist} directory trees -->
      <delete dir="${build}"/>
      <delete dir="${dist}"/>
      </target>
      </project>

      buildfile使用XML編寫。每個buildfile包含一個project元素,而project元素至少有一個target。

      project有三個屬性name、default、basedir,都不是必須的。

      name:項目名稱

      default:指定默認(rèn)執(zhí)行的target

      basedir:故名思意就是工作的根目錄 .代表當(dāng)前目錄

      project中可以定義多個target。Target是要執(zhí)行的tasks的集合。

      target有以下屬性:name,depends,if,unless,description,extensionOf,onMissingExtensionPoint。

      name:target的名字,必須

      depends:此target依賴的targets,用逗號隔開,非必須

      if:某個屬性存在或表達式的計算結(jié)果為true,非必須

      unless:和if對應(yīng),非必須

      description:對target的描述,非必須

      task能被執(zhí)行的一段代碼,結(jié)構(gòu)如下

      <name attribute1="value1" attribute2="value2" ... />

      Property屬性可以通過name及value定義屬性,或通過file指定載入的屬性文件等。

      當(dāng)定義了一個name為example的屬性后,可通過${example}進行引用。

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

      將屬性foo.dist的值設(shè)置為dist

      <property file="foo.properties"/>

      從foo.properties文件中讀取屬性

      <property url="http://www.mysite.com/bla/proPS/foo.properties"/>

      從url指定的properties中讀取所有屬性

      Classpath有兩個屬性path和location,path指定jar包,location指定包含jar包的路徑。可以通過fileset和dirset簡化配置。

      <classpath>
      <pathelement path="${classpath}"/>
      <fileset dir="lib">
      <include name="**/*.jar"/>
      </fileset>
      <pathelement location="classes"/>
      <dirset dir="${build.dir}">
      <include name="apps/**/classes"/>
      <exclude name="apps/**/*test*"/>
      </dirset>
      <filelist refid="third-party_jars"/>
      </classpath>

      4.Ant流程控制 if elseif else

          使用循環(huán),判斷等控制時需添加ant-contrib包。

          taskdef定義第三方的任務(wù),主要包含ant的處理流程邏輯

          if elseif else格式如下

      <property name="hello" value="false"/>
      <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="D:\Program Files\apache-ant-1.8.4\lib\ant-contrib-1.0b3.jar" />
      <target name="run" depends="compile">
      <java classname="HelloWorld">
      <classpath path="${classdir}"/>
      </java>
      <if>
      <equals arg1="${hello}" arg2="true"/>
      <then>
      <echo>${hello} is true</echo>
      </then>
      <elseif>
      <equals arg1="${hello}" arg2="false"/>
      <then>
      <echo>${hello} is false</echo>
      </then>
      </elseif>
      <else>
      <echo>${hello}</echo>
      </else>
      </if>
      </target>

      5.常用task

      使用classpath

      <target>
      <javac>
      <classpath refid="project.class.path"/>
      </javac>
      </target>

      設(shè)置classpath

      <classpath id="project.class.path">
      <pathelement path="${classpath}"/>
      <fileset dir="lib">
      <include name="**/*.jar"/>
      </fileset>
      <pathelement location="classes"/>
      <dirset dir="build">
      <include name="apps/**/classes"/>
      <exclude name="apps/**/*Test*"/>
      </dirset>
      <filelist refid="third-party_jars"/>
      </classpath>

      輸出信息

      <echo message="XXX"/><echo>XXX</echo>

      引入一個XML文件

      <import file="../common-targets.xml"/>

      拷貝文件

      <copy file="myfile.txt" tofile="mycopy.txt"/>
      <copy file="myfile.txt" todir="../otherdir"/>
      <copy todir="..//newdir">
      <fileset dir="src_dir"/>
      </copy>
      <copy todir="../destdir">
      <fileset dir="src_dir">
      <exclude name="**/*.java"/>
      </fileset>
      </copy>
      <copy todir="../destdir">
      <fileset dir="src_dir" excludes="**/*.java"/>
      </copy>
      <!--拷貝一個文件集合到一個目錄,同時建立備份文件-->
      <copy todir="../backup/dir">
      <fileset dir="src_dir"/>
      <globmapper from="*" to="*bak"/>
      </copy>
      <!--拷貝一個集合的文件到指定目錄,并替換掉TITLE-->
      <copy todir="../backup/dir">
      <fileset dir="src_dir"/>
      <filterset>
      <filter token="TITLE" value="Foo Bar"/>
      </filterset>
      </copy>
      <copydir src="${src}/resources" dest="${dest}" includes="**/*.java" excludes="**/Test.java"/>
      <copyfile src="test.java" dest="subdir/test.java"/>

      刪除文件、目錄

      <delete file="/lib/ant.jar"/>
      <delete dir="/lib"/>
      <delete>
      <fileset dir="." includes="**/*.bak"/>
      </delete>
      <!--刪除當(dāng)前目錄下所有的文件和目錄,包括當(dāng)前目錄-->
      <delete includeEmptyDirs="true">
      <fileset dir="build"/>
      </delete>
      <!--刪除當(dāng)前目錄下所有的文件和目錄,不包括當(dāng)前目錄-->
      <delete includeEmptyDirs="true">
      <fileset dir="build" includes="**/*"/>
      </delete>
      <!--刪除當(dāng)前目錄下所有的svn相關(guān)文件(因為SVN文件默認(rèn)是excludes的,所以這里說明一下)-->
      <delete defaultExcludes="false">
      <fileset dir="${src}" includes="**/*.svn"/>
      </delete>
      <!--刪除文件目錄樹-->
      <deltree dir="dist"/>

      剪切文件

      <move todir="some/new/dir"> <fileset dir="my/src/dir"> <include name="**/*.jar"/> <exclude name="**/ant.jar"/> </fileset></move>

      重命名

      <rename src="foo.jar" dest="ant-${version}.jar"/>

      建立臨時文件

      <!--在目錄build下,建立文件名為temp.file,后綴名為xml的文件--><tempfile property="temp.file" destDir="build" suffix=".xml"/>

      Touch使用

      <!--如果文件不存在,創(chuàng)建文件,如果存在,更改最后訪問時間為當(dāng)前系統(tǒng)時間--><touch file="myfile"/><!--如果文件不存在,創(chuàng)建文件,更改最后訪問時間--><touch file="myfile" datetime="06/28/2000 2:02 pm"/><!--更改目錄下所有文件的最后訪問時間--><touch datetime="09/10/1974 4:30 pm"> <fileset dir="src_dir"/></touch>

      Condition的使用 <and> <or> <not>等tag

      peoperty: The name of the property to set

      value: The value to set the property to.

      else: The value to set the property to if the codition evaluates to false.

      <condition property="isMacOsButNotMacOsX"> <and> <os family="mac"> <not> <os family="unix"> </not> </and></condition>

      替換

      <replace
      file="configure.sh"
      value="defaultvalue"
      propertyFile="src/name.properties">
      <replacefilter
      token="@token1@"/>
      <replacefilter
      token="@token2@"
      value="value2"/>
      <replacefilter
      token="@token3@"
      property="property.key"/>
      <replacefilter>
      <replacetoken>@token4@</replacetoken>
      <replacevalue>value4</replacevalue>
      </replacefilter>
      </replace>

       調(diào)用chmod

      <chmod perm="go-rwx" type="file">
      <fileset dir="/web">
      <include name="**/*.cgi"/>
      <include name="**/*.old"/>
      </fileset>
      <dirset dir="/web">
      <include name="**/private_*"/>
      </dirset>
      </chmod>

      設(shè)置property

      <!--設(shè)置屬性name-value--><property name="foo.dist" value="dist"/><!--讀取屬性文件中的屬性配置--><property file="foo.properties"/><!--讀取網(wǎng)絡(luò)中的property-set--><property url="http://www.mysite/props/foo.properties"/><!--讀取文件中的屬性配置--><property resource="foo.properties"/><!--讀取環(huán)境變量--><property environment="env"/>

       顯示錯誤信息

      Fail task 退出當(dāng)前構(gòu)建,拋出BuildException,打印錯誤信息。

      message:A message giving further information on why the build exited

      if:Only fail if a property of the given name exists in the current project

      unless:Only fail if a property of the given name doesn't exist in the current project

      status:Exit using the specified status code; assuming the generated Exception is not caught, the JVM will exit with this status.Since Apache Ant 1.6.2

      <fail>Something wrong here.</fail><fail message="${屬性}"/><!--如果這個屬性不存在,顯示錯誤--><fail unless="failCondition" message="unless Condition"/><!--如果這個屬性存在,顯示錯誤--><fail if="failCondition" message="if Condition"/><!--如果符合條件,顯示錯誤--><fial message="tag condition"> <condition> <not> <isset property="failCondition"/> </not> </condition></fail>

      創(chuàng)建目錄

      <mkdir dir="${dist}/lib"/>

      打jar包

      <jar destfile="${dist}/lib/app.jar" basedir="${build}/classes"/><jar destfile="${build}/lib/app.jar" basedir="${build}/classes" includes="mypackage/test/**" excludes="**/Test.class"/>

      打ear包

      <ear destfile="build/myapp.ear" appxml="src/metadata/application.xml"> <fileset dir="build" includes="*.jar,*war"/></ear>

      執(zhí)行程序

      <target name="help"> <exec executable="cmd"> <arg value="/c"> <arg value="ant.bat"/> <arg value="-p"/> </exec></target>

      運行jar包

      <java classname="test.Main" dir="${exec.dir}" jar="${exec.dir}/dist/test.jar" fork="true" failonerror="true" maxmemory="128m"> <arg value="-h"/> <classpath> <pathelement location="dist/test.jar"/> <pathelement path="/Users/antoine/dev/asf/ant-core/bootstrap/lib/ant-launcher.jar"/> </classpath></java>

        編譯程序

      <javac srcdir="${src}" destdit="${build}" classpath="xyz.jar" debug="on" source="1.4"/>

      Length使用

      <!--把字符串foo的長度保存到屬性length.foo中--><length string="foo" property="length.foo"/><!--把文件bar的長度保存到屬性length.bar--><length file="bar" property="length.bar"/>

      輸入input

      輸入值保存在db.user中

      <input message="Please enter db-username:" addproperty="db.user" defaultvalue="Scott-Tiger"/>

      壓縮、解壓縮

      <!--解壓縮zip文件--><unzip src="apache-ant-bin.zip" dest="${tools.home}"> <patternset> <include name="apache-ant/lib/ant.jar"/> </patternset> <mapper type="flatten"/></unzip><!--壓縮zip文件--><zip destfile="${dist}/manual.zip" basedir="htdoc/manual" includes="api/**/*.html" excludes="**/todo.html"/><!--打tar包--><tar destfile="/Users/antoine/dev/asf/ant-core/docs.tar"> <tarfileset dir="${dir.src}/doc" fullpath="/usr/doc/ant/README" preserveLeadingSlashes="true"> <include name="readme.txt"/> <tarfileset> <tarfileset dir="${dir.src}/docs" prefix="/usr/doc/ant" preserveLeadingSlashes="true"> <include name="*.html"/> </tarfileset></tar><!--解壓tar包--><untar src="tools.tar" dest="${tools.home}"/>

      打war包

      <war destfile="myapp.war" webxml="src/metadata/myapp.xml"> <fileset dir="src/html/myapp"/> <fileset dir="src/jsp/myapp"/> <lib dir="thirdparty/libs"> <exclude name="jdbc1.jar"> </lib> <classes dir="build/main"/> <zipfileset dir="src/graphics/images/gifs" prefix="images"/></war>

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)
        推薦文章

        沒有數(shù)據(jù)