Aubin
Aubin

Reputation: 14863

How can I define a task named 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 search the applicable naming rules for elements and attributes and the mapping rules to Java language.

Upvotes: 5

Views: 553

Answers (2)

Aubin
Aubin

Reputation: 14863

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() {
   }
}

Upvotes: 1

M A
M A

Reputation: 72874

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.

Upvotes: 1

Related Questions