You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Bickford, Mark" <Ma...@WrightExpress.com> on 2010/07/28 16:36:16 UTC

SCRIPTDEF using Java: how to access attributes?

Hi,

I'm a newbie to the list, but not to Ant; this is the first time I
haven't been able to find an answer on my own.  I've tried everything I
can think of to get this to work, but so far all have failed.  I am
writing a scriptdef using Java as the language, but I can't find how to
access any of the predefined beans like attributes and elements.  This
much works:

<project name="javaScriptingTest" default="script.test">

   <scriptdef name="javatest" manager="javax" language="java">
      <attribute name="teststring"/>
      <![CDATA[
      import org.apache.tools.ant.taskdefs.optional.*;
      class javatest
      {
      	public static void main(String[] args) {
      		System.out.println("Number of args: "+args.length);
      		//System.out.println(attributes.toString());
      		//System.out.println(elements.toString());
      		//System.out.println("The list is: "+
attributes.get("teststring").toString());
      		System.out.println("Hello world!");
      		}
      	}
      	]]>
   </scriptdef>
   
   <target name="script.test">
      <javatest teststring="Success!!!  Can you believe it???"/>
   </target>
</project>

The command-line I am using (on WinXP) is 
> ant -lib C:\work\lib\java-script-engine\ -f mini-build.xml

(java-script-engine holds the java-engine.jar from
https://scripting.dev.java.net/ ). 

When I run it as I have things above, it works (shows 0 args and the
"Hello world" message).  When I uncomment any of the println's shown, I
get (e.g.):

script.test:
 [javatest] mfm:///$unnamed.java:9: cannot find symbol
 [javatest] symbol  : variable attributes
 [javatest] location: class javatest

I realize that println of attributes wouldn't give anything really
meaningful, but it would prove the object exists.  How can we access
those beans when Java is the script language?  Am I missing an import,
or is there something else that needs to happen?

Java and ant info:
C:\Subversion>java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

C:\Subversion>ant -version
Apache Ant version 1.7.0 compiled on December 13 2006


Thanks, 
Mark Bickford 



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org