Apache-ant_apache-ant

Source: Internet
Author: User

Below is a complete build.xml, and then we will explain each sentence in detail:

<?xml version= "1.0" encoding= "UTF-8"?> <project name= "HelloWorld" "Run" default= "." > <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 "D epends= "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> <targ ET name= "rerun" depends= "Clean,run" > <ant target= "clean"/> <ant target= "Run"/> </target> < /project> Explanation: <?xml version= "1.0" encoding= "UTF-8"?> buIld.xml the first sentence, there is no practical significance <project name= "HelloWorld" default= "Run" basedir= "." All of > </project> ant's contents must be included in this, name is what you give it, Basedir is the root of the work. Represents the current directory.
Default represents what you want to do. <property name= "src" value= "src"/> A variable in a similar program, why do you think about the role of variables <target name= "compile" depends= "init" > < Javac srcdir= "${src}" destdir= "${dest}"/> </target> write everything you want to be a target, it has a name, depends is the target it relies on,
Ant checks to see if Init has been executed before executing this target, such as the compile here, and if it executes directly compile, if not then executes the target for which it relies, such as init here, and then executes the target As our plan compiles: <target name= "Compile" depends= "init" > <javac srcdir= "${src}" destdir= "${dest}"/> > Make 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> in order not to copy, we can at the beginning of the definition of the target folder, so ant directly put the results in the target folder of the new folder: <target name=" Init "> <mkdir Dir= "${dest}"/> &LT;/target> for a more functional embodiment, added two target delete generated files <target name= "clean" > <delete dir= "${dest}"/> <delete File= "${hello_jar}"/> </target> runs again, showing how to invoke other target <target name= "rerun" clean in one target. Run "> <ant target=" clean "/> <ant target=" Run "/> </target> OK, the explanation is complete, let's check your ant. Create a new src folder, Then put the Helloworld.java in the package directory and do the Build.xml file. Type ant at the command line, and you'll find that each task is completed. You just need to type ant again every time you change the code. Sometimes we don't want to run a program, we just want to perform some of the one or two steps in these steps, for example, I just want to redeploy but not run, type ant build ant, each of these tasks can call Ant + target name. Such a simple ant task is complete.

One, when you use Ant
Maybe you heard someone talk about ant, and when you're ready to learn ant, when you've read the first example above, maybe you feel ant is really good, maybe you feel ant so much, come to these conclusions can not be wrong, although ant is very good,
But not in any case is the best choice, such as Windows there are more simple, easier to use tools, such as Eclipse+myeclipse ECLIPSE+WTP, and so on, whether it is compiled, deployed, run with more than Ant
Easy, convenient but in some cases it's a good place for ant to play:
1, when deployed on the server
When your program is developed and deployed on the server, it's not always possible to configure a eclipse+myeclipse because of the installation of a program, Ant is a good choice at this point because it's small and easy to match
Is it not a good thing that you take your build.xml to any server and simply make changes (some settings, such as a directory), and then one or two commands to complete?
2,linux, most of the time, program development is under Windows, but the program is running on Linux or UNIX, Linux or
Deploying on Unix (especially Unix) is a hassle, and the ant feature comes out again because Ant is Cross-platform, you can use it on most operating systems on Build.xml, and you don't need to change it.
3, when the server maintainers do not know how to program
Many people have had this experience, people who use your program, do not know how to write programs. You have to update the program because it needs to be redeployed again and again for bug fixes. This time you will find it difficult to teach a person. But
There is ant after you only need to tell him to enter ant xxx etc one or two commands, all OK.
These are some situations I have encountered.
After reading the above, think about whether you need to use ant, if you are continuing.

Learn a little more about a little more complex ant
In the actual work process may appear in the following situations, a project into a number of modules, each team or department responsible for a module, in order to test, they wrote a build.xml, and you are responsible for the combination of these modules to
Use together, write a build.xml
You have two choices at this time:
1, to write yourself a build.xml, it would be a troublesome thing to
2, try to take advantage of the build.xml they have already written, and reduce their work
As an example:
Let's say you have three teams down there, each with a section, each with a src and a written build.xml.
This time you get their src, what you need to do is to create three folders Src1, SRC2, src3 separate their src and build.xml, and then write a build.xml

<?xml version= "1.0" encoding= "UTF-8"?> <project "main" name= "Build" default= "." > <property name= "Bin" value= "${basedir}/bin"/> <property name= "Src1" value= "${basedir}/src1"/> &LT;PR Operty 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 D Ir= ' ${src3} ' target= ' clean '/> </target> <target name= ' build ' depends= ' Init,call ' > <copy todir= ' ${bi n} "> <fileset dir=" ${src1} "> <include name=" *.jar "/> </fileset> <fileset dir=" ${
    SRC2} "> <include name=" *.jar "/></fileset> <fileset dir= "${SRC3}" > <include name= "*.jar"/> </fileset> &LT;/COPY&G
T </target> <target name= "rebuild" depends= "Build,clean" > <ant target= "clean"/> <ant target= "Bui"
 LD "/> </target> </project>
OK, your mission is done.

OK, you finished the task, but do you have any feelings, in those build.xml, most of them are repetitive, and change a directory to change a lot of things. Can you make the job a little better, the answer is yes.
Introduce two things:
1,propery
2,xml include
These two things have a function, is to be able to build.xml in the <propery/> in the content of the separation, common use
In addition, they have their own characteristics:
Propery is characterized by simple maintenance and simple key-value pairs, because not everyone likes the XML format
XML include features that not only can extract attributes, but even target can.
Or a previous example:
For example, we want to put Src1 src2 SRC3 These three attributes from XML, you can create a new file called All.properties
Inside the content
Src1=d://study//ant//src1
Src2=d://study//ant//src2
Src3=d://study//ant//src3
Then your Build.xml file can be written like this, others only need to change the configuration file, and not to change your build.xml file

<?xml version= "1.0" encoding= "UTF-8"?> <project "main" name= "Build" default= "." > <property file= "all.properties"/> <property name= "bin" value= "${basedir}/bin"/> <target name= "ini T "> <mkdir dir=" ${bin} "/> </target> <target name=" Run "> <ant dir=" ${src1} "target=" Run "/&G
   T <ant dir= "${src2}" target= "Run"/> <ant dir= "${src3}" target= "Run"/> </target> <target name= "Clea n ' > <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= "Buil" D "/> </target> <target name=" Test "> <ant dir=" ${src1} "target=" Test "/> <ant" dir= " target= "Test"/> <ant dir= "${src3}" target= "Test"/> </target> </project> If you look at it yourself, you'll see a Target & Lt;target name= "Test" > <ant dir= "${src1}" target= "Test"/> "<ant dir=" ${src2} "target=" Test "/> <ant dir
= "${SRC3}" target= "test"/> </target> sometimes you want to add a few target to Build.xml for each group, one that is written in each and then called here but there is a better way. You can write a include.xml file that 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 three team's build.xml files, each with the following content <!--include a XML file, it can be common propery, can is also a target--> <!d Octype Project [<! ENTITY share-variable SYSTEM "File:..
/include.xml ">]> &share-variable;
It's going to look like this. This time, you just add propery in Include.xml, add target, three build.xml add these propery and target at the same time and don't make the build.xml of three groups more complex.   <?xml version= "1.0" encoding= "UTF-8"?> <!--include a XML file, it can be common propery, can is 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=" 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 ja Rfile= "${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= "${h" Ello_jar} "/> </target> <target name=" rerun "depends=" Clean,run "> <ant target=" clean "/> <an
 T target= "Run"/> </target> </project>
Once you've mastered the content, you'll know how to write a good ant, but you'll find that when you really want to do it, you can't immediately make good build.xml because you know too little ant's default command. This
Sometimes if you want to accomplish the task and improve yourself, there are many ways:
1, many open-source programs come with Build.xml to see how they are written.
2,ant's document, which lists the various default commands for Ant, and its rich
3,google, don't ever forget it.
OK, after that, as you write more ant builds, the more commands you know, the more powerful ant is in your hands.
This is a process of gradual accumulation.

Ant's example is very easy to find, all kinds of open source framework will carry a build.xml a careful look, there will be a great harvest
The other one is often used, but in the open source framework build.xml is not generally CVS
If you are using remote CVS, you can use the

<xml version= "1.0" encoding= "Utf-8"?> <project> <property name= "Cvsroot" value= "
      :p Server: 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>
Ant is inherently supported in eclipse, so you can write build.xml directly in eclipse

Because Eclipse offers hints, automatic replenishment, it can make you more effective.
Using the method, you only need to build a project and then create a file called Build.xml. And then you can write your ant build inside.
But always remember that http://www.apache.org/can always find what you need.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.