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;
}