Ant與批處理(win環境)學習筆記(2)

來源:互聯網
上載者:User
在《Ant與批處理(win環境)學習筆記》中學習了Ant的一些基礎知識,這期繼續深入學習
——————————Ant常用task————————————————————————
1、使用classPath
  

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

2、設定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="xxx"/>   </classpath>

3、 輸出資訊
輸出文本資訊使用echo;  輸出xml使用echoxml; 引入檔案使用import

4、 拷貝檔案 
<copy file="myfile.txt" tofile="yourfile.txt"/>
copy的屬性有:
file、 dir、fileset等;
copydir:拷貝目錄

5、 刪除檔案
<delete file="xxx"/>
delete屬性有file、dir
deletetree:刪除檔案分類樹

6、 剪下檔案
<move todir="xxx">
  //被剪下的東西
</move>

7、 重新命名
<rename src="xxx" dest="yyyy"/>

8、建立臨時檔案
<tempfile property="xxx" destDir="yyy" suff=".xml"/>

9、 touch的使用
<touch file="myfile" datetime="xxx"/>

10、 Condition的使用[color=blue][/color]
有<and>、<or>、<not>等tag,樣本如下:

 <condition property="isLinuxButNotRedHat">      <and>         <os family="Linux"/>         <not>          <os family="RedHat"/>         </not>      </and>   </condition>

11、 替換replace
replace、replacefilter

12、 調用chmod
樣本:

   <chmod perm="go-rwx" type="file">      <fileset dir="/web">        <include name="xxx"/>        <exclude name="yyy"/>      </fileset>   </chmod>

13、 設定Property
有如下一些情況:
設定屬性name-value;讀取屬性檔案中的配置:<property file="xxx.properties"/>
讀取網路中的property-set:
<property url="xxx..."/>;讀取環境變數:<property environmen="xxx"/>

13、 建立目錄
<mkdir dir="xxx"/>

14、 打jar包
<jar destfile="${dist}" basedir="xxx"/>

15、 打ear包
<ear destfile="build/myapp.ear" appxml="src/metadata/application.xm">
   <fileset> dir="build" includes="*.jar, *.war"/>
</ear>

16、 執行程式
<target name="help">
   <exec exectuable="cmd">
      <arg value="/c"/>
      <arg value="ant.bat"/>
      <arg value="-p"/>
   </exec>
</target>

17、 運行jar包

   <java classname="test.Main">      <arg value="-h"/>      <classpath>         <pathelement location="dist/test.jar"/>         <pathelement path="/xxxx/yyy.jar"/>      </classpath>   </java>

疑問:
location與path的區別?
path可以用於指向存在多個檔案的位置,而location只能指向單個的檔案或目錄。另外path可以被設定id,供其它的path或classpath引用。如:<path id="main-classpath">,而location則沒有。

18、 製作Javadoc
... ...

19、 定義一個新的task類庫
   <taskdef name="myjavadoc" classname="xxxxx"/>

20、 運行sql
   

   <sql>     driver="xxxx"     url="yyy"     userid="sa"     password="123456">     insert into table test_table values(1,2,3);     truncate table some_other_table;   </sql>

21、 Filter的使用
<filter token="xxx" value="yyy"/>

22、還有一些常用的task如發送郵件、解壓縮
  例子略去... ...

23、 antcall
調用target,DoSomethingelse:

 <target name=default">     <antcall target="doSomethingelse">       <param name="param1" value="value"/>     </antcall>     <target name="doSomethingelse">       <echo message="Hello World"/>     </target>   </target>

在target中還可以使用if...else類的控制流程程

————————Ant中使用CVS的樣本————————————

<?xml version="1.0" encoding="UTF-8"?>   <project>      <project name="cvsroot" value=":pserver:yaoxxxx"/>      <project name="basedir" value="xxx"/>      <project name="cvs.password" value=":yyyy"/>      <project name="cvs.passfile" value="zzzz"/>      <target name="initpass">         <cvspass cvsroot="${cvsroot}" password="${cvs.password}" passfile="${cvs.passfile}"/>      </target>      <target name="checkout" depends="initpass">         <cvs cvsroot="${cvsroot}" command="checkout" cvsrsh="ssh" package="myproject"         dese="${distdir}" passfile="${cvs.passfile}"/>      </target>   </project></xml>

Ant的學習就此告一段落,平時多看看開源項目的build.xml,翻翻ant docs。這是一個積累的過程... ... 我積累呀我積累呀!!!

——————————家庭作業
1、 使用Eclipse整合Ant
2、 使用Ant構建tomcat的源碼
3、 使用Ant結合Junit進行自動化測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.