西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

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

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

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

作者:西西點(diǎn)擊:0次評(píng)論:0次標(biāo)簽: 部署

  • 類型:編程輔助大。134KB語(yǔ)言:中文 評(píng)分:3.3
  • 標(biāo)簽:
立即下載
2 頁(yè) 常用task

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>

引入一個(gè)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>
<!--拷貝一個(gè)文件集合到一個(gè)目錄,同時(shí)建立備份文件-->
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<globmapper from="*" to="*bak"/>
</copy>
<!--拷貝一個(gè)集合的文件到指定目錄,并替換掉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)文件(因?yàn)镾VN文件默認(rèn)是excludes的,所以這里說(shuō)明一下)-->
<delete defaultExcludes="false">
<fileset dir="${src}" includes="**/*.svn"/>
</delete>
<!--刪除文件目錄樹(shù)-->
<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"/>

建立臨時(shí)文件

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

Touch使用

<!--如果文件不存在,創(chuàng)建文件,如果存在,更改最后訪問(wèn)時(shí)間為當(dāng)前系統(tǒng)時(shí)間--><touch file="myfile"/><!--如果文件不存在,創(chuàng)建文件,更改最后訪問(wèn)時(shí)間--><touch file="myfile" datetime="06/28/2000 2:02 pm"/><!--更改目錄下所有文件的最后訪問(wèn)時(shí)間--><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"/>

 顯示錯(cuò)誤信息

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

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="${屬性}"/><!--如果這個(gè)屬性不存在,顯示錯(cuò)誤--><fail unless="failCondition" message="unless Condition"/><!--如果這個(gè)屬性存在,顯示錯(cuò)誤--><fail if="failCondition" message="if Condition"/><!--如果符合條件,顯示錯(cuò)誤--><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>

運(yùn)行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的長(zhǎng)度保存到屬性length.foo中--><length string="foo" property="length.foo"/><!--把文件bar的長(zhǎng)度保存到屬性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>

本文導(dǎo)航

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

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

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

    沒(méi)有數(shù)據(jù)