close

OS: Win XP SP3
Eclipse:Luna
Ant:1.9.4


Download Ant:http://ant.apache.org

1.設定環境變數

2.ant -version --確認目前版本

build.xml格式如下

3.ant or ant info

- Project元素

project 元素是 Ant 構件檔的根項目, Ant 構件檔至少應該包含一個 project 元素,否則會發生錯誤。在每個 project 元素下,可包含多個 target 元素,下表為project元素的屬性

  1. name:用於指定 project 元素的名稱。 (Optional)
  2. default:用於指定 project 默認執行時所執行的 target 的名稱。(Mandatory)
  3. basedir:指定 project 元素的root路徑 (Optional)

- Target 元素

target為Ant的基本執行單元,它可以包含一個或多個具體的任務。多個target 可以存在相互依賴關系。它有如下屬性:

  1. name:指定 target 元素的名稱,這個屬性在一個 project 元素中是唯一的。我們可以通過指定 target 元素的名稱來指定某個 target 。
  2. depends:用於描述 target 之間的依賴關系,若與多個 target 存在依賴關系時,需要以「,」間隔。 Ant 會依照 depends 屬性中 target 出現的順序依次執行每個 target 。被依賴的 target 會先執行。
  3. description:該屬性是關於 target 功能的簡短描述和說明。
  4. if:用於驗證指定的屬性是否存在,若不存在,所在 target 將不會被執行。
  5. unless:該屬性的功能與 if 屬性的功能正好相反,它也用於驗證指定的屬性是否存在,若不存在,所在 target 將會被執行。

echo 則相當於java System.out.println("hello world")將message列印出來。


可自行定義property或是ant定義的property包含:ant.file、ant.version、basedir、ant.java.version、ant.project.name、ant.project.default-target、ant.project.invoked-targets、ant.core.lib、ant.home、ant.library.dir等

當然也可如同java讀取外部properties的方式(可設定DEV、TEST、PROD環境)


DATA_TYPE

Ant提供了一些可事先定義的資料型態。

fileSet

src資料匣底下只包含.java不包含Stu*

    
   		
   		
	

若只針對某個資料匣底下的檔案可定義如下:

    
   		
	

filelist

不管檔案是否存在,filelist可以詳細的針對不同的檔案去定義

   
    
   		
   		
   		
   		
	

filterset

filterset通常會和copy和delete一起使用

 
    
   		
   		
      		
   		
	

path

path通常是用在類別路徑下

 
    
   		
   		
      		
   		
	

ANT Basic Build

檔案目錄結構

  Test
    ├─.settings 
    ├─build
    │   └─classes
    ├─src
    └─WebContent
        ├─META-INF
        └─WEB-INF
            └─lib

build.xml放置在專案(Test)目錄下,針對build.xml 右鍵 > Run As > Ant Build

 
    Buildfile: C:\Documents and Settings\paulstarchen\workspace\Test\build.xml
	clean:
    	[mkdir] Created dir: C:\Documents and Settings\paulstarchen\workspace\Test\WebContent\WEB-INF\classes
	build:
    	[javac] Compiling 1 source file to C:\Documents and Settings\paulstarchen\workspace\Test\WebContent\WEB-INF\classes
	BUILD SUCCESSFUL
	Total time: 4 seconds

build.xml

ANT在執行前會根據project default設定會去執行build target,而在執行前會根據該target depends設定clean而先去執行clean target,執行clean target會先建立目錄(不管是否有該目錄${build.dir}),建立完目錄後會刪除該目錄底下所有的java class檔,之後執行build target (jdk 1.8)後,會將java compiler後的java class檔放置在目錄(${build.dir})下

 
    
	
   	
   	
   	
   
   	
      
         
      
      
   	

   	
      
         
         
      
   	
 
   	
   	
      
         
            
         
      
   	
	

javac要加入includeantruntime="false" 否則會出現下列訊息,主要原因如下:

 
    [javac] build.xml:9: warning: 'includeantruntime' was not set, 
	defaulting to build.sysclasspath=last; set to false for repeatable builds
Whether to include the Ant run-time libraries in the classpath; defaults to yes, unless build.sysclasspath is set. It is usually best to set this to false so the script's behavior is not sensitive to the environment in which it is run.

Ant Build Jar

將該目錄${build.dir}底下的包含(includes)那些目錄下的資料或是檔案,以及排除(excludes)那些目錄下的資料或是檔案(Test.class)至目標目錄(destfile)底下

 
    
	
		
		
		
	   
		
		   	
		   	
		
	

    Buildfile: C:\Documents and Settings\paulstarchen\workspace\Test\build.xml
	build-jar:
      [jar] Building MANIFEST-only jar: C:\Documents and Settings\paulstarchen\workspace\Test\WebContent\lib\util.jar
	BUILD SUCCESSFUL
	Total time: 391 milliseconds

Ant Build War

 
    
	
	
	
	
		
		
		
		
	
		
		
		
			
		
	
		
		
			
				
				
				
				
				
				
			
		
	

    Buildfile: C:\Documents and Settings\paulstarchen\workspace\Test\build.xml
	clean:
	   [delete] Deleting directory C:\Documents and Settings\paulstarchen\workspace\Test\build
	compile:
	    [mkdir] Created dir: C:\Documents and Settings\paulstarchen\workspace\Test\build\classes
	    [javac] Compiling 1 source file to C:\Documents and Settings\paulstarchen\workspace\Test\build\classes
	war:
	      [war] Building war: C:\Documents and Settings\paulstarchen\workspace\Test\build\Test.war
	BUILD SUCCESSFUL
	Total time: 1 second

Ant Packing Application

build.properties

 
    #deploy place
	deploy.path = C:/XWS/Tomcat 5.5/webapps

build.xml

 
    
	
	
	
	
	

		
	
		
	

	
	
		
			
	

	
	
		
			
			
			
			
			
			
		
		
		
		
		
	

	Buildfile: C:\Documents and Settings\paulstarchen\workspace\Test\build.xml
	clean:
   		[delete] Deleting directory C:\Documents and Settings\paulstarchen\workspace\Test\build
	compile:
    	[mkdir] Created dir: C:\Documents and Settings\paulstarchen\workspace\Test\build\classes
    	[javac] Compiling 1 source file to C:\Documents and Settings\paulstarchen\workspace\Test\build\classes
	war:
      	[war] Building war: C:\Documents and Settings\paulstarchen\workspace\Test\build\Test.war
     	[echo] build war in C:\Documents and Settings\paulstarchen\workspace\Test/build
     	[copy] Copying 1 file to C:\XWS\Tomcat 5.5\webapps
	BUILD SUCCESSFUL
	Total time: 1 second

arrow
arrow
    全站熱搜

    Codeless 發表在 痞客邦 留言(0) 人氣()