Flashbuilder ant build

Source: Internet
Author: User
Tags echo message

Background: In the development of a flex project with N multi-module and N source codes and complex construction processes, manual construction of projects relying on Human Resources is unrealistic (mechanical repetitive and boring process, I believe that developers will not be afraid of anything, and it is a waste of manpower). By using ant, we can hand over the work to machines.

Solution: flashbuilder originally supports ant construction. We can create an ant builder. xml file and place it in the root directory of the Project. Right-click the file and choose run mode and debug mode to view ant construction.

Here only reprinted some of the basic knowledge of other blogs, need to learn in detail please go online again look for (http://wenku.baidu.com/view/a0e00315866fb84ae45c8d9d.html ).

I. antKey elements

Ant component files are written based on XML. The default name is build. xml. To better understand ant, write a simple ant program here to show ant functions and give readers a preliminary understanding of ant. First, create a build. xml file under the e drive. The content is as follows:

<? XML version = "1.0"?>
<Project name = "helloworld">
<Target name = "sayhelloworld">
<Echo message = "Hello, amigo"/>
</Target>
</Project>

You can enter the E drive and run ant sayhelloworld. The following running result is displayed:

Sayhelloworld indicates the name of the task to be executed. If the file name is not build. xml but hello. XML, the following error occurs in the Command window when the reader runs the same command:

Buildfile: Build. XML does not exist!

Build failed

The error message of the preceding command shows that the ant command searches for the build. xml file by default. If the file name is hello. in XML, the reader also needs to make a slight change to the command: ant-F hello. XML sayhelloworld, ant-buildfile hello. XML sayhelloworld or ant-file hello. XML sayhelloworld.

Next, I will explain to you the key elements of this section: Project, target, property, and task.

1. ProjectElement

The project element is the root element of the ant component file. The ant component file must contain at least one project element. Otherwise, an error occurs. Each project element can contain multiple target elements. Next, we will show you the attributes of the project element.

1)NameAttribute

Specifies the name of the project element.

2)DefaultAttribute

Specifies the name of the target to be executed when the project is executed by default.

3)BasedirAttribute

Specifies the location of the base path. When this attribute is not specified, the attachment directory of the ant component file is used as the reference directory.

The following is a simple example to show how to use each element of a project. Modify E: "build. xml file. The modified content is as follows:

<? XML version = "1.0"?>
<Project name = "projectstudy" default = "saybasedir" basedir = "E:" Apache-ant-1.7.0 ">
<Target name = "saybasedir">
<Echo message = "the base DIR is: $ {basedir}"/>
</Target>
</Project>

From the above content, we can see that the value of the default attribute is defined as saybasedir, that is, when the ant command is run, if the target is not specified, the saybasedir of the target is executed by default, at the same time, the basedir attribute value is defined as E: "Apache-ant-1.7.0. after entering the e-disk, run the ant command to view the running result, as shown in:

Because the basedir value is set, the value of the basedir attribute is changed to the value set by the reader. You can remove the basedir attribute of the project element and run ant to check the running result. The basedir value is changed to E: ", which is the parent directory of the ant component file.

Sometimes, you may want to get the names of all targets in a project. You can add-proecthelp to the ant command to achieve this purpose. For example, if ant-projecthelp is run for the above example, the output result is as follows:

Buildfile: Build. xml

Main targets:

Other targets:

Saybasedir

Default target: saybasedir

2. TargetElement

It is the basic execution unit of ant. It can contain one or more specific tasks. Multiple targets can be interdependent. It has the following attributes:

1)NameAttribute

Specifies the name of the target element. This attribute is unique in a project element. You can specify a target by specifying the target element name.

2)DependsAttribute

It is used to describe the dependency between targets. If there is a dependency between multiple targets, it must be separated by commas. Ant executes each target in sequence according to the order in which the target appears in the depends attribute. The target to be depended on will be executed first.

3)IfAttribute

Used to verify whether the specified property exists. If it does not exist, the target will not be executed.

4)UnlessAttribute

This property is opposite to the if property. It is also used to verify whether the specified property exists. If it does not exist, the target of the property will be executed.

5)DescriptionAttribute

This attribute is a brief description and description of the target function.

The following is an example of comprehensive use of each attribute. Modify E: "build. xml file. The modified content is as follows:

<? XML version = "1.0"?>
<Project name = "targetstudy">
<Target name = "targeta" If = "ant. java. Version">
<Echo message = "Java version: $ {ant. java. Version}"/>
</Target>
<Target name = "targetb" depends = "targeta" Unless = "amigo">
<Description>
A depend example!
</Description>
<Echo message = "the base DIR is: $ {basedir}"/>
</Target>
</Project>

Run ant targetb after entering the Elastic Block Storage, as shown in the following figure:

After analyzing the results, the reader can see that we are running a target named targetb. Because the target depends on targeta, targeta will be executed first, and because the system has installed the Java environment, so Ant. java. the version attribute exists and the target targeta is executed. The output information is [Echo] Java version: 1.5. After targeta is executed, the targetb is executed because amigo does not exist, the unless attribute is the target where the object is located when it does not exist. Therefore, it can be seen that targetb is executed and the output information is: The base DIR is: e :".

3. PropertyElement

This element can be seen as a parameter or Parameter definition. The property of a project can be set through the property element or outside ant. To introduce an external file, such as the build. properties file, you can introduce it using the following content: <property file = "build. properties"/>

The property element can be used as the attribute value of a task. In a task, attribute names are placed between "$ {" and "}" and the attribute values of the task are placed.

Ant provides some built-in attributes, including the list of system attributes that can be obtained and the system. the Properties Obtained by the getpropertis () method are the same. For more information about these system properties, see the sun website.

At the same time, ant also provides its own built-in attributes, as shown below:

Basedir: the absolute path of the project base directory. This attribute is not described in detail when explaining the project element;

Ant. File: the absolute path of buildfile. In the above examples, the ant. File value is E: "build. xml;

Ant. Version: ant version. In this article, the value is 1.7.0;

Ant. Project. Name: the name of the currently specified project, that is, the value of the name attribute of the project mentioned above;

Ant. java. Version: The JDK version detected by Ant. It can be seen as 1.5 in the running result of the previous example.

The following is a simple example of using a property element. Modify E: "build. xml file. The content is as follows:

<? XML version = "1.0"?>
<Project name = "propertystudy" default = "example">
<Property name = "name" value = "amigo"/>
<Property name = "Age" value = "25"/>
<Target name = "example">
<Echo message = "name :$ {name}, age :$ {age}"/>
</Target>
</Project>

The running result of this example is shown in:

The reader can see that the following two statements are used:

<Property name = "name" value = "amigo"/>

<Property name = "Age" value = "25"/>

We have set two attributes named name and age. After these two attributes are set, the reader can obtain the values of these two attributes through $ {name} and $ {age} respectively in the following article.

II. AntCommon Tasks

Each task in the ant tool encapsulates the specific functions to be executed, which is the basic execution unit of the ant tool. In this section, we will guide you through common ant tasks and examples.

1. CopyTask

This task is mainly used to copy files and directories. Example:

Eg1. copy a single file: <copy file = "file.txt" tofile = "copy.txt"/>

Eg2. copy the file directory:

<Copy todir = "../newdir/dest_dir">

<Fileset dir = "src_dir"/>

</Copy>

Eg3. copy the file to another directory:

<Copy file = "file.txt" todir = "../other/DIR"/>

2. DeleteTask

Delete a file or directory, for example:

Eg1. delete an object: <delete file = "Photo/amigo.jpg"/>

Eg2. delete a directory: <Delete dir = "photo"/>

Eg3. Delete All backup directories or empty directories:

<Delete multiple deemptydirs = "true">

<Fileset dir = "." includes = "**/*. Bak"/>

</Delete>

3. MkdirTask

Create a directory. Eg: <mkdir dir = "build"/>

4. MoveTask

An example is as follows:

Eg1. move a single file: <move file = "fromfile" tofile = "tofile"/>

Eg2. move a single file to another directory: <move file = "fromfile" todir = "movedir"/>

Eg3. move a directory to another directory:

<Move todir = "newdir">

<Fileset dir = "olddir"/>

</Move>

5. EchoTask

This task outputs Information Based on the log or monitor level. It includes four attributes: Message, file, append, and level, for example:

<Echo message = "Hello, amigo" file = "logs/system. log" APPEND = "true">

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.