Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define a task named with a hyphen?

Tags:

java

ant

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 search the applicable naming rules for elements and attributes and the mapping rules to Java language.

like image 406
Aubin Avatar asked Nov 01 '22 14:11

Aubin


2 Answers

org.apache.tools.ant.IntrospectionHelper is the class that does introspection to extract attributes from setter methods.

From the Javadocs of the constructor:

void setFoo(Bar) is recognised as a method for setting the value of attribute foo, so long as Bar is non-void and is not an array type. Non-String parameter types always overload String parameter types, but that is the only guarantee made in terms of priority.

like image 179
M A Avatar answered Nov 15 '22 04:11

M A


Answer based on matt comment, many thanks to him!

Apache ANT use two ways to identify element and attributes:

  1. Reflection based on Java names, mapped in xml ignoring case as explain in the manouti answer.
  2. Interfaces based. org.apache.tools.ant.DynamicElement and org.apache.tools.ant.AttributeElement

Interfaces must be used to map XML identifiers to Java identifiers when special characters are used, like 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() {
   }
}
like image 39
Aubin Avatar answered Nov 15 '22 03:11

Aubin