How to define a task with a hyphen?

build.xml

<taskdef onerror ="ignore" name ="monitor-client" classpath="${jar-client}" classname="hpms.app.mon.client.AntTask" /> <target name="run-client" depends="compile-sample" description="Launch monitor"> <monitor-client layout ="Layout.xml" gui ="true" autostart ="true"> <log-server port ="3000" capacity="2048" /> ... 

AntTask.java

 public final class AntTask extends Task { private ... public void setLayout( String layout ) { } public void setGui( boolean gui ) { } public void setAutostart( boolean autostart ) { } public void addConfiguredLogServer( LogServer logServer ) { } @Override public void execute() { ... } } 

Execution

 Buildfile: ...\build.xml compile-sample: run-client: BUILD FAILED ...\build.xml:116: monitor-client doesn't support the nested "log-server" element. 

Question

I am looking for suitable naming rules for elements and attributes and Java matching rules.

+5
source share
2 answers

The answer is based on matt comment , many thanks to him!

Apache ANT uses two methods for identifying an element and attributes:

  • Reflection based on Java names displayed in xml, ignoring the case, as an explanation in manouti .
  • Interfaces org.apache.tools.ant.DynamicElement and org.apache.tools.ant.AttributeElement

Interfaces should be used to map XML identifiers to Java identifiers when special characters are used, such as a hyphen, as shown below:

 import org.apache.tools.ant.DynamicElement; import org.apache.tools.ant.Task; public final class AntTask extends Task implements DynamicElement { private ... public void setLayout( String layout ) { } public void setGui( boolean gui ) { } @Override public Object createDynamicElement( String name ) { if( name.equals( "log-server" )) { return new Logserver(); } return null; } ... @Override public void execute() { } } 
+1
source

org.apache.tools.ant.IntrospectionHelper is a class that introspects to extract attributes from setter methods.

From the Javadocs constructor:

void setFoo(Bar) recognized as a method for setting the value of the foo attribute if Bar is nonempty and not an array type. Nonparametric parameter types always overload String parameter types, but this is the only guarantee made in terms of priority.

+1
source

Source: https://habr.com/ru/post/1206548/


All Articles