Apache ANT チュートリアル

⚡ スマートサマリー

Apache Ant はオープンソースの Java単一の build.xml ファイルを通じてコン​​パイル、テスト、パッケージング、デプロイを自動化するベースのビルドツール。 Selenium (NAIST) と TestNGこれにより、テスト実行の再現性、組織化、および場所を選ばない容易な実行が可能になります。

  • 🐜 中核的な役割: Antは、XML設定ファイルからビルド手順を保存し、順番に実行します。
  • ⚙️ ビルドライフサイクル: 1つのファイルで、クリーンアップ、コンパイル、クラスパスの設定、実行、レポート作成を処理します。
  • 📦 build.xmlの構造: プロジェクト、ターゲット、タスク、およびプロパティのタグは、すべてのビルドステップを定義します。
  • 🔧 インストール: ANT_HOME変数を設定し、%ANT_HOME%\binをシステムパスに追加します。
  • 🧪 Selenium (NAIST) と TestNG: Antはtestng.xmlを呼び出すため、ビルド中にテストスイートが実行されます。
  • 🚀 依存関係の制御: サードパーティ製JARファイルのパスが一元管理されているため、信頼性と導入性が向上します。

Apache Ant と共に Selenium

アパッチアントとは?

完全なソフトウェア製品を作成する際には、さまざまなサードパーティ API、それらのクラスパス、以前の実行可能バイナリ ファイルのクリーニング、ソース コードのコンパイル、ソース コードの実行、レポートの作成、コード ベースの配置などを処理する必要があります。これらのタスクがXNUMX つずつ手動で行うと、膨大な時間がかかり、プロセスでエラーが発生しやすくなります。

Ant のようなビルド ツールの重要性はここにあります。 Ant の構成ファイル (通常は build.xml) に記載されている順序で、すべてのプロセスを保存、実行、および自動化します。

アパッチアント

Ant ビルドの利点

  1. Ant は、クリーン、コンパイル、依存関係の設定、実行、レポートなど、アプリケーションのライフサイクルを作成します。
  2. サード パーティ API の依存関係は Ant によって設定できます。つまり、他の Jar ファイルのクラス パスは Ant ビルド ファイルによって設定されます。
  3. エンド ツー エンドの配信と展開のための完全なアプリケーションが作成されます。
  4. これは、XML ファイルを使用してすべての構成を行うことができ、コマンド ラインから実行できるシンプルなビルド ツールです。
  5. 構成が実際のアプリケーション ロジックから分離されているため、コードがクリーンになります。

Ant のインストール方法

Ant をインストールする手順 Windows 以下のとおりであります

ステップ1) に行く https://ant.apache.org/bindownload.cgi または から .zip ファイルをダウンロードします。 apache-ant-1.9.4-bin.zip

Antをインストールする

ステップ2) フォルダーを解凍し、解凍したフォルダーのルートに移動してパスをコピーします

Antをインストールする

ステップ3) [スタート] -> [コンピューター] -> ここを右クリックして [プロパティ] を選択し、[システムの詳細設定] をクリックします。

Antをインストールする

ステップ4) 新しいウィンドウが開きます。 「環境変数…」ボタンをクリックします。

Antをインストールする

ステップ5) 「新規…」ボタンをクリックし、変数名を「ANT_HOME」に、変数値を解凍したフォルダーへのルートパスとして設定し、「OK」をクリックします。

Antをインストールする

ステップ6) リストから「パス」変数を​​選択し、「編集」をクリックして追加します。 %ANT_HOME%\bin.

Antをインストールする

システムを一度再起動すると、Ant ビルド ツールを使用する準備が整います。

ステップ7) コマンドラインを使用して Ant のバージョンを確認するには:

Ant – バージョン

Antをインストールする

Build.xml について

Build.xml は、Ant ビルド ツールの最も重要なコンポーネントです。 のために Java プロジェクト、すべてのクリーニング、セットアップ、コンパイル、および展開に関連するタスクは、このファイルに XML 形式で記載されています。 コマンド ラインまたは任意の IDE プラグインを使用してこの XML ファイルを実行すると、このファイルに書き込まれたすべての命令が順次実行されます。

サンプル build.XML 内のコードを理解しましょう。

  • project タグは、プロジェクト名と basedir 属性を示すために使用されます。 basedir はアプリケーションのルート ディレクトリです。
    <project name="YTMonetize" basedir=".">
  • プロパティ タグは、build.XML ファイルで変数として使用され、以降の手順で使用されます。
<property name="build.dir" value="${basedir}/build"/>
		<property name="external.jars" value=".\resources"/>
	<property name="ytoperation.dir" value="${external.jars}/YTOperation"/>
<property name="src.dir"value="${basedir}/src"/>
  • Target 順番に実行されるステップとして使用されるタグ。Name 属性はターゲットの名前です。1 つの build.xml に複数のターゲットを含めることができます。
    <target name="setClassPath">
  • パスタグは、共通の場所にあるすべてのファイルを論理的にバンドルするために使用されます
    <path id="classpath_jars">
  • pathelement タグは、すべてのファイルが保存されている共通の場所のルートへのパスを設定します
    <pathelement path="${basedir}/"/>
  • path タグ内のすべての共通ファイルのパスをシステムのクラスパス形式に変換するために使用される pathconvert タグ
    <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
  • プロジェクト内のさまざまなサードパーティ jar のクラスパスを設定するために使用される fileset タグ
    <fileset dir="${ytoperation.dir}" includes="*.jar"/>
  • エコータグは、コンソールにテキストを出力するために使用されます
<echo message="deleting existing build directory"/>
  • タグを削除すると、指定されたフォルダーからデータが削除されます
<delete dir="${build.dir}"/>
  • mkdir タグは新しいディレクトリを作成します
	<mkdir dir="${build.dir}"/>
  • Java ソース コードをコンパイルし、.class ファイルを新しいフォルダに移動するために使用される javac タグ
        <javac destdir="${build.dir}" srcdir="${src.dir}">
	<classpath refid="classpath_jars"/>
</javac>
  • jar タグは .class ファイルから jar ファイルを作成します
	<jar destfile="${ytoperation.dir}/YTOperation.jar" basedir="${build.dir}">
  • マニフェストタグは、実行用のメインクラスを設定します
<manifest>
		<attribute name="Main-Class" value="test.Main"/>
</manifest>
  • あるターゲットを別のターゲットに依存させるために使用される「depends」属性
<target name="run" depends="compile">
  • java タグは、コンパイル ターゲット セクションで作成された jar から main 関数を実行します。
<java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/>

次を使用して Ant を実行します Eclipse プラグイン

EclipseからAntを実行するには、build.xmlファイルに移動し、ファイルを右クリック→実行…→ファイルのビルドをクリックします。

次を使用して Ant を実行します Eclipse プラグイン

例:

Ant の機能を非常に明確に説明する小さなサンプル プログラムを取り上げます。 私たちのプロジェクト構造は次のようになります -

次を使用して Ant を実行します Eclipse プラグイン

この例では、4 つのターゲットがあります。

  1. 外部jarのクラスパスを設定し、
  2. 以前にコンパイルされたコードをきれいにする
  3. 既存の Java コードをコンパイルする
  4. コードを実行する

Guru99AntClass.class

package testAnt;		
import java.util.Date;		

public class Guru99AntClass {				
   public static void main(String...s){									       
		System.out.println("HELLO GURU99 ANT PROGRAM");					        
		System.out.println("TODAY's DATE IS->"+ currentDate() );					  
}		    		   
public static String currentDate(){					        
	return new Date().toString();					  
	}		
}

Build.xml

 
<?xml version="1.0" encoding="UTF-8"	standalone="no"?>									
<!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->	

<project name="YTMonetize" basedir=".">								
     <!--Property tags will be used as variables in build.xml file to use in further steps-->		

	<property name="build.dir" value="${basedir}/build"/>								
    <property name="external.jars" value=".\resources"/>								
		<property name="ytoperation.dir" value="${external.jars}/YTOperation"/>
<property name="src.dir"value="${basedir}/src"/>
<!--Target tags used as steps that will execute in sequential order. name attribute will be the name  of the target and < a name=OLE_LINK1 >'depends' attribute used to make one target to depend on another target -->		
	       <target name="setClassPath">					
			<path id="classpath_jars">						
				<pathelement	path="${basedir}/"/>					
			</path>				         
<pathconvert	pathsep=";"property="test.classpath" refid="classpath_jars"/>	
</target>				
	<target name="clean">						
		<!--echo tag will use to print text on console-->		
		<echo message="deleting existing build directory"/>						
		<!--delete tag will clean data from given folder-->		
		<delete dir="${build.dir}"/>						
	</target>				
<target name="compile" depends="clean,setClassPath">								
	<echo message="classpath:${test.classpath}"/>					
			<echo message="compiling.........."/>						
	<!--mkdir tag will create new director-->							
	<mkdir dir="${build.dir}"/>						
		<echo message="classpath:${test.classpath}"/>						
		<echo message="compiling.........."/>						
	<!--javac tag used to compile java source code and move .class files to a new folder-->		
	<javac destdir="${build.dir}" srcdir="${src.dir}">								
			<classpath refid="classpath_jars"/>						
	</javac>				
	<!--jar tag will create jar file from .class files-->		
	<jar	destfile="${ytoperation.dir}/YTOperation.jar"basedir="${build.dir}">								
	            <!--manifest tag will set your main class for execution-->		
						<manifest>				
							<attribute name="Main-Class" value="testAnt.Guru99AntClass"/>  
</manifest>		
</jar>				
    </target>				
	<target name="run" depends="compile">								
		<!--java tag will execute main function from the jar created in compile target section-->	
<java jar="${ytoperation.dir}/YTOperation.jar"fork="true"/>			
</target>				
	</project>				

次を使用して Ant を実行します Eclipse プラグイン

実行方法 TestNG Antを使用したコード

実行する TestNG Antを使用したコード

ここでは、クラスを作成します テスト メソッドとクラスパスの設定 テスト build.xml で。

ここで、testng メソッドを実行するために、別の testng.xml ファイルを作成し、build.xml ファイルからこのファイルを呼び出します。

ステップ1) 作成します Guru99AntClass.class” パッケージで テストアント

Guru99AntClass.class

package testAnt;
import java.util.Date;
import org.testng.annotations.Test;		
public class Guru99AntClass {				
    @Test		  
	public void Guru99AntTestNGMethod(){					     
		System.out.println("HELLO GURU99 ANT PROGRAM");					
		System.out.println("TODAY's DATE IS->"+ currentDate() );					
	}		
	public static String currentDate(){					
		return new Date().toString();					
	}		
}		

ステップ 2) Build.xml でこのクラスをロードするターゲットを作成する

<!-- Load testNG and add to the class path of application -->
	<target name="loadTestNG" depends="setClassPath">
<!—using taskdef  tag we can add a task to run on the current project. In below line, we are adding testing task in this project. Using testing task here now we can run testing code using the ant script -->
		<taskdef resource="testngtasks" classpath="${test.classpath}"/>
</target>

ステップ3) testng.xml を作成する

テスト

<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="YT"thread-count="1">
			<test name="GURU99TestNGAnt">
			<classes>
			   <class name="testAnt.Guru99AntClass">
	</class>
</classes>
</test>
</suite>

ステップ4) 創造する Target これを実行するにはBuild.xmlで TestNG コード

<target name="runGuru99TestNGAnt" depends="compile">
<!-- testng tag will be used to execute testng code using corresponding testng.xml file. Here classpath attribute is setting classpath for testng's jar to the project-->
	<testng classpath="${test.classpath};${build.dir}">
<!—xmlfileset tag is used here to run testng's code using testing.xml file. Using includes tag we are mentioning path to testing.xml file-->
	 <xmlfileset dir="${basedir}" includes="testng.xml"/>
</testng>				

ステップ5) 完全な Build.xml

<?xml version="1.0"encoding="UTF-8"standalone="no"?>
<!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->
			<project name="YTMonetize" basedir=".">
		       <!--Property tags will be used as variables in build.xml file to use in further steps-->
			<property name="build.dir"value="${basedir}/build"/>
<!-- put  testng related jar in the resource  folder -->
	      <property name="external.jars" value=".\resource"/>
				<property name="src.dir" value="${basedir}/src"/>
<!--Target tags used as steps that will execute in  sequential order. name attribute will be the name
    of the target and 'depends' attribute used to make one target to depend on another target-->
<!-- Load testNG and add to the class path of application -->
         <target name="loadTestNG"depends="setClassPath">
				<taskdef resource="testngtasks"classpath="${test.classpath}"/>
		</target>
		<target name="setClassPath">
		       <path id="classpath_jars">
					<pathelement path="${basedir}/"/>
					<fileset dir="${external.jars}" includes="*.jar"/>
         </path>
        <pathconvert pathsep=";"property="test.classpath"refid="classpath_jars"/>
	</target>
	<target name="clean">
              <!--echo tag will use to print text on console-->
	               <echo message="deleting existing build directory"/>
               <!--delete tag will clean data from given folder-->
	               <delete				dir="${build.dir}"/>
			</target>
<target name="compile"depends="clean,setClassPath,loadTestNG">
	         <echo message="classpath:${test.classpath}"/>
	               <echo	message="compiling.........."/>
		       <!--mkdir tag will create new director-->
		        <mkdir dir="${build.dir}"/>
					<echo message="classpath:${test.classpath}"/>
			<echo message="compiling.........."/>
	<!--javac tag used to compile java source code and move .class files to a new folder-->
	        <javac destdir="${build.dir}"srcdir="${src.dir}">
	             <classpath refid="classpath_jars"/>
		</javac>
  </target>
<target name="runGuru99TestNGAnt"depends="compile">
		<!-- testng tag will be used to execute testng code using corresponding testng.xml file -->
			<testng classpath="${test.classpath};${build.dir}">
               <xmlfileset dir="${basedir}"includes="testng.xml"/>
	</testng>
</target>
</project>

ステップ6) 出力

実行する TestNG Antを使用したコード

上記のファイルをダウンロード

Antが構築を管理し、 TestNG 配線が完了したら、次のステップで Ant を直接接続します。 Selenium ウェブドライバー。

アリと Selenium ウェブドライバー

これまで見てきたように、Antを使えば、サードパーティ製のjarファイルをすべてシステム内の特定の場所に配置し、プロジェクトのパスを設定できます。この方法を用いることで、プロジェクトのすべての依存関係を1か所に集約し、コンパイル、実行、デプロイの信頼性を高めることができます。

同様に、Selenium を使用したテスト プロジェクトでは、build.xml で Selenium の依存関係を簡単に記述でき、アプリケーションに手動でクラス パスを追加する必要はありません。

したがって、プロジェクトのクラスパスを設定する以下の従来の方法を無視できます。

アリと Selenium ウェブドライバー

例:

前の例を変更します

ステップ1) リソースフォルダ内のSelenium関連のjarにプロパティselenium.jarsを設定します。

		<property name="selenium.jars" value=".\selenium"/>

ステップ2) ターゲットsetClassPathにSeleniumファイルを追加します

<target name="setClassPath">
	        <path id="classpath_jars">
				<pathelement path="${basedir}/"/>	
				<fileset dir="${external.jars}" includes="*.jar"/>
	            <!-- selenium jar added here -->
  		            <fileset dir="${selenium.jars}" includes="*.jar"/>
         </path>		

ステップ3) Build.xml を完成させます。

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

<!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->
			<project name="YTMonetize" basedir=".">
                  <!--Property tags will be used as variables in build.xml file to use in further steps-->
				<property name="build.dir" value="${basedir}/build"/>
      <!-- put  testng related jar in the resource  folder -->
	       <property name="external.jars" value=".\resource"/>
<!-- put  selenium related jar in resource  folder -->
     <property name="selenium.jars" value=".\selenium"/>
			<property name="src.dir" value="${basedir}/src"/>
				<!--Target tags used as steps that will execute in  sequential order. name attribute will be the name 
of the target and 'depends' attribute used to make one target to depend on another target-->
      <!-- Load testNG and add to the class path of application -->
       <target name="loadTestNG" depends="setClassPath">
				<taskdef resource="testngtasks" classpath="${test.classpath}"/>
		</target>
<target name="setClassPath">
	        <path id="classpath_jars">
				<pathelement path="${basedir}/"/>
					<fileset dir="${external.jars}" includes="*.jar"/>
			<!-- selenium jar added here -->
	            <fileset dir="${selenium.jars}"includes="*.jar"/>
        </path>
   <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
</target>
<target name="clean">
<!--echo tag will use to print text on console-->
               <echo message="deleting existing build directory"/>
	                <!--delete tag will clean data from given folder-->
		               <delete dir="${build.dir}"/>
				</target>
<target name="compile" depends="clean,setClassPath,loadTestNG">
         <echo message="classpath:${test.classpath}"/>
                <echo message="compiling.........."/>
        <!--mkdir tag will create new director-->
	        <mkdir dir="${build.dir}"/>
          			<echo message="classpath:${test.classpath}"/>
			<echo message="compiling.........."/>
	<!--javac tag used to compile java source code and move .class files to new folder-->
     <javac destdir="${build.dir}"srcdir="${src.dir}">
             <classpath refid="classpath_jars"/>
	</javac>
</target>
<target name="runGuru99TestNGAnt" depends="compile">
		<!-- testng tag will be used to execute testng code using corresponding testng.xml file -->
			<testng classpath="${test.classpath};${build.dir}">
               <xmlfileset dir="${basedir}" includes="testng.xml"/>
		</testng>
	</target>
</project>

ステップ4) 以前に作成したクラスを変更します Guru新しいコードを含む99AntClass.java。

この例では、次の手順を使用します。 Selenium には次の値があります:

  1. に行く https://demo.guru99.com/test/guru99home/
  2. すべてのコースのリンクを XNUMX つずつ読む
  3. コンソールにすべてのコースのハイパーリンクを印刷します。

Guru99AntClass.java:

package testAnt;		
import java.util.List;		
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;	
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Guru99AntClass {

	@Test		
		public void Guru99AntTestNGMethod(){
	      WebDriver driver = new FirefoxDriver();	
		  driver.get("https://demo.guru99.com/test/guru99home/");
		  List<WebElement> listAllCourseLinks = driver.findElements(By.xpath("//div[@class='canvas-middle']//a"));							        
          for(WebElement webElement : listAllCourseLinks) {
			System.out.println(webElement.getAttribute("href"));
      	  }
		}
}		

ステップ5) 実行が成功すると、出力は次のようになります。

アリと Selenium ウェブドライバー

上記のサンプル ファイルをダウンロードします。

よくあるご質問

Antは手続き型のビルドツールで、各ステップを手動でスクリプト化します。一方、Mavenは宣言型で、中央リポジトリを通じて依存関係を自動的に管理します。Antはより詳細な制御を可能にし、Mavenは規約を重視します。

はい。 Java (NAIST) と Selenium プロジェクトは依然としてビルドと継続的インテグレーションに Ant に依存しています。新しいプロジェクトでは、Maven や Gradleしかし、Antはきめ細かなビルド制御には依然として有用である。

Apache Ant 1.10.x は、現在も活発にメンテナンスされているシリーズであり、 Java 8以降。古い1.9.x系はレガシーをサポートしています。 Javaバイナリファイルは必ず公式のant.apache.orgサイトからダウンロードしてください。

はい。ChatGPTやCopilotなどのAIアシスタントは、build.xmlのターゲットを生成し、タスク定義を提案し、エラーを説明します。 Rev生成されたXMLを注意深く確認してください。パスと依存関係のバージョンはまだ検証が必要です。

AI搭載ツールが生成する TestNG テストスイートを最適化し、テストの順序を最適化し、Antターゲットを介してトリガーされる不安定なテストを特定します。ロケーターの修正を提案し、ビルドレポートを自動的に分析することで、メンテナンスの手間を軽減します。

いいえ。Antはコンパイルしてそのまま実行できます。 Java or JUnit テストも。 TestNG これは、ビルド中に Ant が testng タスクを使用して呼び出す testng.xml を通じて、柔軟なスイート構成を追加するだけです。

ANT_HOMEは、Antがどこにインストールされているかをオペレーティングシステムに伝え、どのディレクトリからでもantコマンドが実行できるようにします。インストール時にシステムパスに%ANT_HOME%\binを追加します。 Windows.

Gradle Antは、Antの柔軟性とMavenの依存関係管理、そして高速なインクリメンタルビルドを兼ね備えているため、新規プロジェクトで一般的に好まれます。既存のビルドスクリプトを保守する場合は、主にAntを選択してください。