Ant Reference Guide (1)

  Ant Guide 
  1, what is the ant 
  Ant is building tools 
  2, What is Construction 
  Concept can be found everywhere, the image, you must code from a local used, the compiler and then copied to a place to operate, and so on, of course, not only with this, but mainly to stem this 
  3, the benefits of ant 
  Cross-platform - because ant using java to achieve, and so it cross-platform 
  Use simple - and ant brothers than make up 
  Grammar clear - and make the same comparison 
  Powerful - ant can do a lot of things, you may use for a long time, you still do not know how much it can function.    When you ant to develop their own plug-in, you will find it more functional. 
  4, the brothers make ant 
  Ant to do a lot of things, most had made a call to make, but different groups, make more for c / c + +, Java is more for ant.    Of course, this is not certain, but most people's. 
  First, constructing ant environment to be used to build a first ant ant environment, a very simple steps: 
  1), the installation jdk, set JAVA_HOME, PATH, CLASS_PATH (these should be read the article should know) 
  2), download ant address http://www.apache.org/ find a version you like, or simply the latest version 
  3), you have to extract the ant is a compressed, decompressed it, and put it in a directory as simple as possible, for example, D: \ ant-1.6 although you do not necessarily have to do so, but doing so is good. 
  4), set up ANT_HOME PATH add ANT_HOME bin directory under the directory 
  5), to test your settings, start -> run -> cmd enter the command line -> Enter type ant, to see if 
  Buildfile: build.xml does not exist! 
  Build failed 
  Congratulations to you, you then have completed the establishment of ant, ant experience 
  Just as each language has HelloWorld, one of the most simple applications can people feel Ant 
  1, first of all, you should know that you have to do, I want to do now is: 
  Compiled some of the procedures to prepare them to pack it into their jar on the package should be placed where they are running here for the sake of simplicity only write a program that HelloWorld.java code as follows: 
  Package test.ant; 
  Public class HelloWorld ( 
  Public static void main (String [] args) ( 
  System.out.println ( "Hello world1"); 
  ) 
  ); 
  2, in order to achieve the purpose of the top, you can use javac manually, copy, jar, java to complete, but consider if you have hundreds of categories, in many debugging, deployment, the first time javac , copy, jar, 
  Java it will be a hard work.    Now look at the ant how to complete their elegant. 
  To run ant build.xml Although there is a need not necessarily be called this name, but I suggest you do so below is a complete build.xml, and then we also explained in detail to every one 
  <? Xml version = "1.0" encoding = "UTF-8"> 
  <project Name="HelloWorld" default="run" basedir="."> 
  <property Name="src" value="src"/> 
  <property Name="dest" value="classes"/> 
  <property Name="hello_jar" value="hello1.jar"/> 
  <target Name="init"> 
  <mkdir Dir="${dest}"/> 
  </ Target> 
  <target Name="compile" depends="init"> 
  <javac Srcdir="${src}" destdir="${dest}"/> 
  </ Target> 
  <target Name="build" depends="compile"> 
  <jar Jarfile="${hello_jar}" basedir="${dest}"/> 
  </ Target> 
  <target Name="run" depends="build"> 
  <java Classname="test.ant.HelloWorld" classpath="${hello_jar}"/> 
  </ Target> 
  <target Name="clean"> 
  <delete Dir="${dest}" /> 
  <delete File="${hello_jar}" /> 
  </ Target> 
  <target Name="rerun" depends="clean,run"> 
  <ant Target="clean" /> 
  <ant Target="run" /> 
  </ Target> 
  </ Project> 
  Explained: 
  <? Xml version = "1.0" encoding = "UTF-8"> 
  Build.xml in the first sentence that has no real significance 
  <project Name="HelloWorld" default="run" basedir="."> 
  </ Project> 
  Ant must contain all the elements in the inside, you name it take to the name of basedir Guming think Italy is the work of the root directory. Representative of the current directory.    Default on behalf of the default needs to be done. 
  <property Name="src" value="src"/> 
  Variables in a similar procedure, why do think about the role of variables 
  <target Name="compile" depends="init"> 
  <javac Srcdir="${src}" destdir="${dest}"/> 
  </ Target> 
  You want to do every thing into a target, it has a name, is it depends rely on the target, such as in the implementation of this target here before compile ant will check whether the init have been carried out, if carried out direct direct execution compile, if there is no will to implement its dependence on the target such as init here, and then in the implementation of this target 
  If we plan compiled: 
  <target Name="compile" depends="init"> 
  <javac Srcdir="${src}" destdir="${dest}"/> 
  </ Target> 
  Do jar package: 
  <target Name="build" depends="compile"> 
  <jar Jarfile="${hello_jar}" basedir="${dest}"/> 
  </ Target> 
  Run: 
  <target Name="run" depends="build"> 
  <java Classname="test.ant.HelloWorld" classpath="${hello_jar}"/> 
  </ Target> 
  To do not have to copy, we can start in the definition of good target folder, this ant directly on the results on the target folder in the new folder: 
  <target Name="init"> 
  <mkdir Dir="${dest}"/> 
  </ Target> 
  In order to reflect a little more functional, and joined the two target 
  Delete the document generation 
  <target Name="clean"> 
  <delete Dir="${dest}" /> 
  <delete File="${hello_jar}" /> 
  </ Target> 
  Once again running the show here how, in a call the other target inside the target 
  <target Name="rerun" depends="clean,run"> 
  <ant Target="clean" /> 
  <ant Target="run" /> 
  </ Target> 
  Well, a complete explanation, below test your new ant it a src folder, and then into the HelloWorld.java according to the package directory to build.xml file at the command line, type ant, you will find a task are completed.    End code changes every time need only type ant again 
  Sometimes we may not want to run a program, would like to implement these steps in one or two steps, for example, I just want to redeployment and not running, type 
  Ant build 
  Each ant in a task can be so called ant + target name 
  Well, such a simple ant task completed. 


  1, when the use of ant 
  Perhaps you have heard others talked about ant, impulsive for learning about ant, the upside when you read the first example, you may feel ant's nice, you may feel ant Buguoruci, reached these conclusions are not wrong, although ant good use 
  But not, under any circumstances, the best choice, for example, there are more windows simpler, more easy-to-use tools, such as the eclipse + + wtp myeclipse eclipse, and so on, whether it is compiled, deployment, operation than to use ant more easily, but in some cases facilitate the ant is a good place to play: 
  1, when deployed on the server when you completed the development of the procedure, the deployment of personnel to deploy on the server, not because of the installation of a procedure because a configuration eclipse + myeclipse bar, ant at this time is a good choice because it compact, easy-to-configure, you write the build.xml with you to any one server, only to do simple changes (some settings, such as directory), and then ordered to complete 12 Is it not a beautiful things?. 
  2, linux, many times this is the programmers in the windows, but in the process to run on linux or unix, in linux or unix (especially on unix) deployment is a troublesome matter, the characteristics of this time ant Also out because ant is cross-platform, you can build.xml use in most operating systems, needed to amend. 
  3, defenders do not know when the server programming time many people have experienced this before, the procedure you use, do not know how to write procedures.    You updated version of the procedure because, as amended bug once again the need for redeployment.    This time you will find that the case of a person in difficulties.    However, there ant, you only need to tell him, such as the importation of ant xxx 12 orders, all ok. 
  These are in some cases I encountered. 
  After reading the above, think carefully, you need to use ant, if it is to continue. 

  A slightly complicated further study a little ant 
  In the actual course of their work may be a number of circumstances, a project divided into a number of modules, each team or department responsible for a module, in order to test, they write a build.xml, and you responsible for the combination of these modules together use, write a build.xml 
  This time you have two choices: 
  1, his re-write a build.xml, this will be a troublesome thing 
  2, to make full use of them has been written build.xml to reduce their work is an example: 
  Suppose you below three teams, each responsible for one part of the Panel, they were a src and a written build.xml 
  This time you have obtained their src, and you need to do is to establish three folders src1, src2, respectively src3 their src and build.xml put to, and then write a build.xml 
  <? Xml version = "1.0" encoding = "UTF-8"> 
  <project Name="main" default="build" basedir="."> 
  <property Name="bin" value="${basedir}\bin" /> 
  <property Name="src1" value="${basedir}\src1" /> 
  <property Name="src2" value="${basedir}\src2" /> 
  <property Name="src3" value="${basedir}\src3" /> 
  <target Name="init"> 
  <mkdir Dir="${bin}" /> 
  </ Target> 
  <target Name="run"> 
  <ant Dir="${src1}" target="run" /> 
  <ant Dir="${src2}" target="run" /> 
  <ant Dir="${src3}" target="run" /> 
  </ Target> 
  <target Name="clean"> 
  <ant Dir="${src1}" target="clean" /> 
  <ant Dir="${src2}" target="clean" /> 
  <ant Dir="${src3}" target="clean" /> 
  </ Target> 
  <target Name="build" depends="init,call"> 
  <copy Todir="${bin}"> 
  <fileset Dir="${src1}"> 
  <include Name="*.jar" /> 
  </ Fileset> 
  <fileset Dir="${src2}"> 
  <include Name="*.jar" /> 
  </ Fileset> 
  <fileset Dir="${src3}"> 
  <include Name="*.jar" /> 
  </ Fileset> 
  </ Copy> 
  </ Target> 
  <target Name="rebuild" depends="build,clean"> 
  <ant Target="clean" /> 
  <ant Target="build" /> 
  </ Target> 
  </ Project> 
  Ok your mission accomplished. 

  Ok, the top you have completed the task, but do you have some deep feelings about it, in those build.xml, mostly repeat, and changes a lot of things need to change the directory.    Whether the work can then do a better job of that, the answer is yes. 
  The introduction of two things: 
  1, propery 
  2, xml include 
  These two things have a function that can build.xml in <propery /> separated from the content, the addition they have to use common characteristics: 
  Propery is to maintain the characteristics of simple, need only simple keys right, because not everyone likes the format of xml 
  Xml include features that not only can be extracted attributes, and can even target. 
  Or previous examples: 
  For example, we want to src1 src2 src3 from these three attributes in xml, can be a new document called all.properties 
  The contents inside 
  Src1 = D: \ \ study \ \ ant \ \ src1 
  Src2 = D: \ \ study \ \ ant \ \ src2 
  Src3 = D: \ \ study \ \ ant \ \ src3 
  Then you the build.xml file can be written, people only need to change the configuration file, but not to change your build.xml file 
  <? Xml version = "1.0" encoding = "UTF-8"> 
  <project Name="main" default="build" basedir="."> 
  <property File="all.properties" /> 
  <property Name="bin" value="${basedir}\bin" /> 
  <target Name="init"> 
  <mkdir Dir="${bin}" /> 
  </ Target> 
  <target Name="run"> 
  <ant Dir="${src1}" target="run" /> 
  <ant Dir="${src2}" target="run" /> 
  <ant Dir="${src3}" target="run" /> 
  </ Target> 
  <target Name="clean"> 
  <ant Dir="${src1}" target="clean" /> 
  <ant Dir="${src2}" target="clean" /> 
  <ant Dir="${src3}" target="clean" /> 
  </ Target> 
  <target Name="build" depends="init,call"> 
  <copy Todir="${bin}"> 
  <fileset Dir="${src1}"> 
  <include Name="*.jar" /> 
  </ Fileset> 
  <fileset Dir="${src2}"> 
  <include Name="*.jar" /> 
  </ Fileset> 
  <fileset Dir="${src3}"> 
  <include Name="*.jar" /> 
  </ Fileset> 
  </ Copy> 
  </ Target> 
  <target Name="rebuild" depends="build,clean"> 
  <ant Target="clean" /> 
  <ant Target="build" /> 
  </ Target> 
  <target Name="test"> 
  <ant Dir="${src1}" target="test" /> 
  <ant Dir="${src2}" target="test" /> 
  <ant Dir="${src3}" target="test" /> 
  </ Target> 
  </ Project> 
  If you look at it you will see a target 
  <target Name="test"> 
  <ant Dir="${src1}" target="test" /> 
  <ant Dir="${src2}" target="test" /> 
  <ant Dir="${src3}" target="test" /> 
  </ Target> 
  Sometimes you want to join each group build.xml several target, the approach is to write each inside, and then call in here but there is a better way. 
  You can write a include.xml document, which reads as follows 
  <? Xml version = "1.0" encoding = "UTF-8"> 
  <property Name="src" value="src"/> 
  <property Name="dest" value="classes"/> 
  <target Name="test"> 
  <ant Target="run" /> 
  </ Target> 
  Then change your team build.xml three documents, each of the following elements inside accession 
  <! - Include a xml file, it can be common propery, can be also a target -> 
  <! DOCTYPE project [ 
  <! ENTITY share-variable SYSTEM "file: .. / include.xml"> 
]>
  & Share-variable; 
  Following a time like this, if you add in include.xml propery, add target, three build.xml will also add the target and propery 
  And will not allow the three groups build.xml become even more complicated. 
  <? Xml version = "1.0" encoding = "UTF-8"> 
  <! - Include a xml file, it can be common propery, can be also a target -> 
  <! DOCTYPE project [ 
  <! ENTITY share-variable SYSTEM "file: .. / include.xml"> 
]>
  <project Name="HelloWorld" default="run" basedir="."> 
  <! - Use the include -> 
  & Share-variable; 
  <! - Defined the property -> 
  <! - Via include 
  <property Name="src" value="src"/> 
  <property Name="dest" value="classes"/> 
–>
  <property Name="hello_jar" value="hello1.jar"/> 
  <! - Define the op -> 
  <target Name="init"> 
  <mkdir Dir="${dest}"/> 
  </ Target> 
  <target Name="compile" depends="init"> 
  <javac Srcdir="${src}" destdir="${dest}"/> 
  </ Target> 
  <target Name="build" depends="compile"> 
  <jar Jarfile="${hello_jar}" basedir="${dest}"/> 
  </ Target> 
  <target Name="run" depends="build"> 
  <java Classname="test.ant.HelloWorld" classpath="${hello_jar}"/> 
  </ Target> 
  <target Name="clean"> 
  <delete Dir="${dest}" /> 
  <delete File="${hello_jar}" /> 
  </ Target> 
  <target Name="rerun" depends="clean,run"> 
  <ant Target="clean" /> 
  <ant Target="run" /> 
  </ Target> 
  </ Project> 

  Mastered the upside of those elements, you knew how to write a good ant, but you will find that when you really want to do, you can not immediately make good build.xml because, as you know too little ant The default provision of the order. this time if you want to complete the tasks, and improve themselves, there are many ways: 
  1, many open-source programs with build.xml to see how they wrote 
  2, the document ant inside the ant write detailed in the various default order, and the rich 
  3, google, it must never forget 
  Ok, but after you write with the ant build more and more, you know the order the more you ant in the hands of the more powerful it. 
  This is a slowly accumulating process. 

  Ant to find a good example, with various open-source framework will be a closer look at the build.xml, there will be another great harvest will be used frequently, but in the framework of the open-source is not generally build.xml cvs 
  If you are using the remote cvs, can use 
  <xml Version="1.0" encoding="utf-8"?> 
<project>
  <property Name="cvsroot" value=":pserver:wang:@192.168.1.2:/cvsroot"/> 
  <property Name="basedir" value="/tmp/testant/"/> 
  <property Name="cvs.password" value="wang"/> 
  <property Name="cvs.passfile" value="${basedir}/ant.cvspass"/> 
  <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" dest = "$ () basedir" 
  Passfile = "$ () cvs.passfile" /> 
  </ Target> 
  </ Project> 

  In the eclipse inside congenital support ant, so you can write directly in the eclipse inside build.xml 
  Because the eclipse provides alerts, automatic replenishment functions, it will allow you success. 
  Use only the need for a project, then the establishment of a call build.xml file.    Then you can write inside the ant build a moment to remember http://www.apache.org/ but never able to find the things you need 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply