Simple use of Ant

xiaoxiao2021-03-06  39

ANT uses a summary 1. Introduction to Ant

Ant is an open source tool developed by Apache, he can automate a lot of things. For example, he can perform Java from

Compile, package, execute. You can also call the executable of the operating system. So, from the simple, the role of Ant can be similar to a batch file, that is, you can define a series of tasks, then knock a command, which is all executed by the system. Of course, Ant is not so simple, because the design goals of Ant are not seeing to replace batch files.

So, if you want to know the value of Ant, I want to know what is the limit programming (XP). Extreme programming is a software party law, he advocates "simple, communication, encouragement, feedback". From a simple perspective, what we do should be as simple as possible, do not add too many additional useless features, because the number of function is more than the code length, and the complexity of the software. Therefore, simple design means simple and easy-to-understand code, and better software maintenance. As for feedback, there is a wide meaning here, mainly to understand the individual's understanding, mainly including, user feedback, code feedback, etc. Through simple design, we can generate the prototype of the software in a shorter time, so that the user's feedback is obtained earlier. And the feedback of the code is in my opinion, more refers to the result of the unit test. When the test passes, the code can be executed, so that we can make further reconstruction, integration, etc. More activities. As for communication and encouragement, more is to make an idea from team management. This is no longer detailed here.

Back to Ant's use value, I think the role of Ant is that he can provide a very simple way to continuous integration and debugging of software, faster to get feedback from code level.

2. Ant use

a) Core: Ant's Using the core is a build file in an XML format, and the user can define the ANT to perform the corresponding operations according to their own requirements.

b) Structure: Ant's build file, you need to define a Project tag first, there is a plurality of different targets in the same project, and there are multiple different TASKs inside any Target.

c) example:

...

...

...

...

Note: There is no connection between Target, of course, can also cause certain dependencies between Targets through the Depends property.

3. ANT use details

a) Project definition:

I. Syntax:

b) Target definition:

I. Syntax:

1. Name: Target name.

Ii. Example:

III. Extension:

Dependence:

Note: PREPARE To execute, you need to execute init.

2. Conditions:

Note: INIT is to be executed, and only the PROP property is only defined.

Note: INIT is to be executed, and when and only the PROP property is not defined.

c) Attribute definition:

I. Syntax:

1. Name: Attribute name.

2. Value: Attribute value.

Ii. Example:

d) Path definition:

I. Syntax:

The value of the PATH is divided by ":" or ";".

Ii. Example:

Represents two files of A. Jar and B. Jar.

Iii. Syntax:

The value of Location is a separate file or directory.

IV. Example:

Represents a file with a.jar.

grammar:

Indicates all files under the folder specified by DIR.

Vi. Example:

Indicates that all file names containing TEST in the C: / Directory. In addition to including Include, we can use other Exclude, Date, Depth, Size. In addition to FileSet, there are other similar icons et al.

e) Task definition:

The task is the core of Ant because the ANT operation defined by the user is implemented on the task.

i. Compile:

Syntax:

Ii. Pressible JAR Package:

Syntax:

a) Basedir: Source Directory.

b) destfile: Target file.

c) The location of the Manifest: Manifest.mf file.

2. Example:

Iii. Press WAR Package:

Syntax:

a) WARFILE: Target file.

b) WEBXML: Web.xml file location.

2. Example:

Iv. Copy:

Syntax:

a) File: Source file.

b) TODIR: Target folder.

2. Example:

4. Integration of Ant and Junit

a) Configuration: You need to put junit.jar in the LIB directory of Ant.

b) call JUnit:

I. Syntax:

"

>

1. Printsummary property: Whether print results are printed.

2. Formatter tag: Used to output a detailed message. Type's type can be Plain and XML, etc. UseFile refers to whether the output result is output to a file, and the default is TRUE.

3. Test tag: Testcase used to specify the TestCase to be called.

4. ClassPath tag: The location of the TestCase specified in the TEST tag.

c) Generate a report: When the file generated by the JUnit tag is an XML format, the XML's result can be converted to an HTML file through the XSLT engine with the XSLT engine.

I. Syntax:

">

1. Todir property: Indicates the storage location of the file.

2. FileSet Tags: Specify the file location to be converted.

d) Complete example:

5. ANT extension

We can perform TASK customization by extending an interface provided by Ant.

a) Simple task (does not contain attributes and child elements)

Code:

Public class hellotask extends task {

Public void execute () {

System.out.println ("Hello World");

}

}

Build.xml file:

ClassName = "nick.task.hellotask"

Classpath = "./ mytask.jar">

Note: The TaskDep tag is used to associate our classes we implemented, with task name mytask.

b) Simple tasks (only contain attributes)

Code:

Public class hellotaskwithattr extends task {

Private string name = NULL;

Public void execute () {

System.out.println ("Hello" Name);

}

Public void setname (String name) {

THIS.NAME = Name;

}

}

Note: In order to add attributes for the task, you need to provide the corresponding setter method in the implementation class.

Build.xml file:

ClassName = "nick.task.hellotaskwithattr"

Classpath = "./ mytask.jar">

c) Simple task (including sub-elements)

Code:

Public class hellotaskwithele extends task {

PRIVATE PERSON P = NULL;

Public void execute () {

System.out.println ("Hello" P.GetName () ". You are" p.getage ()

"OLD.");

}

Public person creagePerson () {

Return new persons ();

}

Public void addconfiguredPerson (Person P) {

THIS.P = P;

}

}

Public class person {

Private string name = NULL;

PRIVATE INT AGE = 0; public person () {

}

Public int getage () {

Return Age;

}

Public void setage (int Age) {

THIS.AGE = AGE;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

}

Note: In order to include a sub-tag, first provide a sub-tag implementation class. Then provide two methods in the implementation class, one is

Createxxx method (CREATEPERSON () method in the above example). The other is AddConfigureDxxx () (the AddConfigureDperson (Person P) method in the previous example). If you need to add a plurality of the same sub-tags, all of these submarkers are saved in the AddConfigureDxxxx () method.

Build.xml file:

ClassName = "nick.task.hellotaskwithele"

Classpath = "$ {dist.dir} /mytask.jar">

d) Composite task (including self-mission)

Code:

Public class hellotaskwithele extends Task imports taskcontainer {

PRIVATE PERSON P = NULL;

Private Task T = NULL;

Public void execute () {

IF (t! = null)

T.Perform ();

System.out.println ("Hello" P.GetName () ". You are" p.getage ()

"OLD.");

}

Public person creagePerson () {

Return new persons ();

}

Public void addconfiguredPerson (Person P) {

THIS.P = P;

}

Public void addtask (task t) {

THIS.T = T;

}

}

Note: In order to include sub-tasks, implementation classes need to implement the TaskContainer interface, and provide the AddTask (Task T) method.

If you want to limit the task that tasks can include, you can filter in the AddTask method. The perform () method of the task is required when you want to initiate the sub task.

Build.xml file:

ClassName = "nick.task.hellotaskwithele"

Classpath = "$ {dist.dir} /mytask.jar">

6. to sum up:

Here is just a brief introduction and summary of the functionality and characteristics of Ant. In terms of function, Ant can make more, broader applications in addition to compilation and assembly of code. In terms of tool integration, Ant can integrate with other tools such as XDoclets, except for JUnit, to play a greater role.

转载请注明原文地址:https://www.9cbs.com/read-77862.html

New Post(0)