Ant task notes for programmers

Source: Internet
Author: User

Author: Jiangnan Baiyi

This document is intended for programming enthusiasts who need to pay attention to important tasks during ant programming.

I don't know why, all the scripts of foreigners are exceptionally beautiful. I learned a lot from appfuse, and I was forced to learn a lot by myself when compiling springside2.0 component installation script. Here I will summarize it.

If you only talk about the most important thing, ant-contrib's <if> and <for> nodes give ant full programming capabilities.

1. Variables

Variables in ant have a strange feature-once assigned, they will not change. This feature is sometimes very helpful and sometimes annoying. Be sure to pay attention to it. Note that the variables in ant are the same as those in other languages and have a valid range.

1. value assigned by the command line

Ant build. XML-dtomcat. Home = foo

2. Interactive input with users -- input task

<Input message = "select a target"
Validargs = "compile, jar, test"
Addproperty = "My. Input"/>

3. Read from the propertis file and save the disk --Propertyfile task

<Propertyfile file = "My. properties">
<Entry key = "springside. Home" default = "."/>
</Propertyfile>

If my. properties does not exist, generate the my. properties file, springside. Home = .. There is a particularly useful place: the properties of some properties files are different for each developer, and you do not want to put them in SVN, but want to initialize the value, you can use this command.

<Propertyfile file = "My. properties">
<Entry key = "springside. Home" value = "..."/>
</Propertyfile>

Write the configuration file again.

2. Process Control

Without the <if> and <for> nodes contributed by ant-contrib, ant's programmability is extremely low.

2.1 If task

Ant can be used to determine the IF (unless, if attribute) at the target level, but it is too inconvenient.

2.2 Conditions

However, it is very convenient for ant to encapsulate a bunch of conditions in advance. These conditions fully start from the actual situation, including whether the file exists and whether http: // localhost: 8080 is connected. For details, refer to the ant reference manual.

2.3 For task

Supports the loop of "a, B, c, d" String Array, file directory, and fileset.

2.4 parallel task

Parallel is very useful. For example, if you want to enable Tomcat while doing something else, you need to use it. Otherwise, you only need to use the spawn = true attribute to run tomcat in the background. Spawn has many disadvantages, such as the inability to view information on the console in real time, and the inability to disable Tomcat when stopped ant running.

Parallel is equivalent to a container where every task is executed in parallel. If you want to perform several tasks sequentially, use the sequential task equivalent to () to package.

2.5 waitfor task

Pause ant execution until the conditions are met. For example, <waitfor>

3. Code Encapsulation

The most basic encapsulation of ant code is

1. Ant task: the task that calls other scripts. You can set Dir and whether to inherit the variables of this script.

2. antcall task: Call other tasks in this script and set parameters.

3. Import Task: import other script content to this script just like include in other languages.

1. antfetch, antcallback task

Ant-contrib contribution, corresponding to ant and antcall. The original version can only pass variables to the called function, but the return value cannot be obtained after the function is executed. The antcallback syntax is as follows:
<Antcallback target = "mytarget" return = "myresult1, myresult2"/>

2. macrodef task

As the smallest encapsulation unit, it is not much different from <target> encapsulation and then <antcall target = "XXX"> calling, and you can experience the nuances on your own. Macrodef is preferred.

3. Java tasks and exec tasks directly execute Java classes or programs

Note the definition of the execution directory. In Windows, If You Want To directly run commands in the DOS window, run the following command to enable the default browser to access localhost:

<Exec executable = "cmd.exe">
<Arg line = "/C start http: // localhost: 8080>
</Exec> "/

4. Extended ant tasks

The extension of ant task is very simple. Execute () is implemented to execute the task, and the setter interface is implemented to allow ant framework to execute attribute injection. Inherit tasks to obtain ant capabilities, such as querying the value of a variable.

A little troublesome is the injection of multi-layer nested attributes. See http://ant.apache.org/manual/developlist.html for details

Springside 2.0 implements an XML file merge task. For details, see xmlmergetask. java.

One lesson is that if the variable is a file or path, you need to declare the variable as Java. io. file or Org. apache. tools. ant. types. path, ant will add the project to the relative path during the injection. basedir.

The easiest way to expand ant is to nest scripts in ant. After importing the BSF library, you can use the script language supported by BSF (see script task ). Next, I will try it.

 5. File Operations

In springside, there are a lot of file operations. Just remember.

When replace and copy are added to filter, strings can be replaced.

Concat adds content of other files at the end of the file.

It's so sleepy. I haven't written much details about many things. I will try again tomorrow.

Aside from the question, ant fully demonstrated how to write XML-style code. Although it is very troublesome for developers to code XML, it is far from Ruby's rake to build a clear system with Ruby Code itself, however, for IDE, especially the IDE that wants graphical programming, XML is much easier to render than common code, so the Graphic programming of Puyuan EOS is also serialized into XML code. In addition, ant's task and general element are similar.

 

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.