The blog "ant-using ant to build a simple Java project (i)" demonstrates building a simple Java project using ant Tools, followed by this example to further learn Ant:
The above example requires running multiple ant commands to run the main function in the test class, can you simplify the need to run the command? The answer is yes, you can change the script in the Build.xml file to the following script:
<?XML version= "1.0" encoding= "UTF-8"? ><project name= "Test" default= "Run-test" basedir= "." ><property name= "src" value= "src"/><property name= "Lib" value= "Build/lib"/><property name= "Dest" Value= "build/classes"/><property name= "Np_jar" value= "Build/lib/test.jar"/><target name= "Create-path" ><mkdir dir= "${dest}"/><mkdir dir= "${lib}"/></target><target name= "Create-class" depends= " Create-path "><javac srcdir=" ${src} "destdir=" ${dest} "/></target><target name=" Create-jar " depends= "Create-class" ><jar destfile= "${np_jar}" basedir= "${dest}" > <manifest> <attribute name= " Main-class "value=" Test "/> </manifest></jar></target><target name=" run-Test "depends=" Create-jar "><java classname=" Test "classpath=" ${np_jar} "/></target><target name=" clean ">< Delete file= "${np_jar}"/><delete dir= "${dest}"/><delete dir= "${lib}"/></target></project& Gt;
How does it do this by simply running the ant command and then running the "Ant Create-path", "Ant Create-class", "Ant Create-jar", and "Ant run-test" commands in order to complete the operation?
A, because the project label default property defines a value of run-test, when you run the ant command directly, Ant will first run the target named Run-test;
b, because the depends attribute in the target named Run-test points to the target named Create-jar, the target named Create-jar is run first;
C, because the depends property in the target named Create-jar, points to the target named Create-class. So the target of name Create-class will be run first;
D, because the depends attribute in target named Create-class points to target named Create-path, the target named Create-path is run first.
E, because the name is Create-path, the target does not have a depends attribute. So the ant command finally runs the target first.
Thus. Running the ant command alone now actually runs the "Ant Create-path", "Ant Create-class", "Ant Create-jar" and "Ant run-test" commands in turn.
The DOS form runs independently of the results of the ant command, for example:
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
ant-using ant to build a simple Java project (two)