Interactive ant call and self-written ant task

Source: Internet
Author: User

Ant is required to call the framework generated by the Code. There are three things worth remembering after writing:

1. ant -- the definitive guide 2nd is a good cookbook.

2. Interactive input with users instead of forcing users to change build. xml

When the following code is run, ant will ask "what is the name of your pojo? ", Ask the user to enter the value of the attribute pojoname.

<input message="What is the name of your POJO (i.e. Person)?" addproperty="PojoName"/>

3. Write ant task

Writing ant task is actually very simple. Just look at the turtorial below.

Http://ant.apache.org/manual/tutorial-writing-tasks.html

The above can be condensed into three sentences:
1. Write a Java class that inherits from org. Apache. Tools. Ant. task.
2. Implement the execute () method and ant will call it.
3. When parameters are passed in from build. XML, the task class only needs to have variables and setter functions with the same name, and ant will inject them to you.

1. Ant task standard implementation

import org.apache.tools.ant.Task;
public class HelloWorld extends Task
{
String msg;
public void execute()
{
  System.out.println(msg);
}

public void setMsg(String msg)

  this.msg = msg;
}
}

Compile and package the above files into helloworld. Jar

The build. xml call is as follows:

<target description="Use the Task">
  <taskdef name="helloworld" classname="HelloWorld" classpath="helloworld.jar"/>
  </target>

In addition:
1. Obtain the public variables and target names in build. xml.

String myProperty = getProject().getProperty("myProperty ");
String targetName = getOwningTarget().getName();                                

2.Get compound attributesA little more complicated. This mode can be extended to a much more complex situation than the following.


<target>
       <message msg="Hello "/>
     <message msg="World"/>
  </target>

1. First define the internal class called message to represent the message node. This class has a MSG attribute and also has a setter function, so that it can be assigned a value in build. xml.

public class Message extends Object
{
  public Message()
  {  }
  String msg;
  public void setMsg(String msg)  {    this.msg = msg;  }
  public String getMsg() {    return msg;  }
}

2. Define the createxxx function for injecting message to helloworld and the messages object holding the message list according to the ant framework. Ant will automatically inject the message for you.

Vector msgs = new Vector(); 
public Message createMessage()
{
  Message msg = new Message();
  messages.add(msg);
  return msg;
}

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.