You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Jonathan Simon <jo...@yahoo.com> on 2003/06/12 22:20:35 UTC

Ant scripting from Jython.

Hello all, 

Ive been working alot with the idea of scripting languages and how they fit
into the community (especially Java scripting languages). I started to look at
when XML is misused and when something like Jython might be a better idea. The
end result was an article on the new java.net site that you can find here:

http://today.java.net/pub/a/today/2003/06/10/jython.html

The last example is about ANT. I spoke to Erik (hatcher) and James (duncan
davidson) about the whole abuse of XML in ANT proper not to mention all of the
stuff on the ant-contrib project. Essentially the idea is that you can use XML
if you are doing straight ahead stuff. But if you are going to do things like
use for loops and write conditionals, you would be better off using a real
scripting language for several reasons. Mainly that the sytax is easier to read
and write and you dont have to implement code to support custom code structures
in XML. 

Cool. That said, I went to write a fictional example of how you could replace
the XML in ANT and run it straight from Jython. I was thinking about a huge
development effort to write some "glue" between AND and Jython. Then I realized
that since its Jython and Jython can talk to Java objects and ANT has
underlying Java objects there IS NO GLUE CODE!!! Here is a basic out of the box
working example calling ANT from Jython: 


project = Project()
copyTask = Copy()
copyTask.project = project
copyTask.file = File("C:\\java\\jython_ant\\src\HelloJython.java")
copyTask.todir = File("C:\\java\\jython_ant\\temp")
copyTask.execute()

Now thats kind of ugly. You have to know alot of the ANT internals to get this
to work. Also, theres too many ant calls muddying up the script. So you can
make a helper method like this: 

def copyWrapper(file, toDir, project):
    copyTask = Copy()
    copyTask.project = project
    copyTask.file = File(file)
    copyTask.todir = File(toDir)
    copyTask.execute()

Then you can call it from your Jython and script(!!!) like this: 

copyWrapper("C:\\java\\jython_ant\\src\HelloJython.java",
"C:\\java\\jython_ant\\temp", project)

This would be even cleaner if you abstract out the variables/properties into
XML.

The point is that calling ANT from Jython is much easier than I initially
thought (you dont have to write ant translation stuff) but harder than I
secondly thought (you have to write some wrappers or something somewhere to
make the code legible). 

So, since I am a long time ANT user, but only a recent ANT developer, Im
looking for some help. I would like to get interested folks together and figure
out the best combination of ANT classes to extend or reimplement and Jython
code to make this Jython stuff a woprking reality and see where it goes. 

Just to finish off, what I really like about this approach is the whole
community aspect. The Jython stuff will be using the same codebase. Using the
same ANT installation you can call ANT from XML or Jython. Additionally, if we
do this right, well be able to plug in any Java scrpting language of choice
(like JRuby). Then, the whole argument of the use of XML goes away and the
purists and iconoclasts can all have their way-- without forking ANT. 

So there you have it. Sorry for the long rant. Lets see what kind of momentum
we can get on this. 

-jonathan



















=====
Jonathan Simon
Home - 732.777.0377  <----------- New!!!
Work - 646.674.2060
Cell - 732.718.8844
Music - www.mp3.com/jonathan_simon

__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

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


Re: Ant scripting from Jython.

Posted by Bruce Atherton <br...@callenish.com>.
At 11:43 AM 6/13/2003 -0700, James Duncan Davidson wrote:


>>Not that useful but a bit more grist for this mill. It certainly showed me
>>that it is hard to cram the expressiveness of the XML approach into method
>>calls. I had to limit myself to a very narrow subset of tasks and their usage
>>patterns.
>
>It is a tradeoff.

I'm curious about this tradeoff and about the "XML Burden" you mention in 
your blog. Is the issue using XML or is it better access to scripting 
facilities within Ant?

The reason I ask is that Jelly already provides an XML scripting language 
that can run Ant tasks. Using Jelly would allow both the expressiveness of 
XML and the flexibility of scripting languages, for those that need it. It 
would also minimize the mental shifts as well as the ugliness of mixing 
different languages in a single file.

I don't know if Jelly is the right answer to the problem you see (I've 
never used it myself). I know that many people are intimidated by XML, 
especially XML that has a lot of namespace stuff going on and Jelly has 
plenty of that. Also, Python is a very intuitive language to write in, 
whereas using tags is awkward. I'm just trying to make sure I understand 
the problem you are identifying.



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


Re: Ant scripting from Jython.

Posted by peter reilly <pe...@corvil.com>.
jython does support named parameters, one
can do some bizarre stuff:

  <script language="jython">
from java.io import File
def twrapper(taskname, **args):
  t = project.createTask(taskname)
  for a in args.keys():
    exec("t.%s = %s" % (a, args[a]))
  t.execute()

twrapper('echo', message="'hello world'")
twrapper('delete', quiet="1", dir="File('todir')")
twrapper('mkdir', dir='File("todir")')
twrapper('copy', file='File("fromfile")', todir='File("todir")')
  </script>

output:
hello world
Deleting directory /home/preilly/proj/learning/outofdate/todir
Created dir: /home/preilly/proj/learning/outofdate/todir
Copying 1 file to /home/preilly/proj/learning/outofdate/todir

Peter.

On Friday 13 June 2003 06:23, Conor MacNeill wrote:
> Hi Duncan,
>
> You all might like to look at two recent threads in ant-dev and ant-user
> that are somewhat related to this topic.
>
> http://marc.theaimsgroup.com/?l=ant-dev&m=105516662409107&w=2
>
> and
>
> http://marc.theaimsgroup.com/?l=ant-user&m=105491111510585&w=2
>
> Instead of the "script driving Ant tasks" approach, <scriptdef> is more
> like a script within a task. As such, the scripts would potentially be more
> focussed on the things that need to be scripty rather than doing the whole
> build in the script. After all, IMHO,
>
> copyWrapper("C:\\java\\jython_ant\\src\HelloJython.java",
> "C:\\java\\jython_ant\\temp", project)
>
> doesn't end up any "better" than
>
> <copy file="C:\java\jython_ant\src\HelloJython.java"
> 	  todir="C:\java\jython_ant\temp"/>
>
> In fact because the attributes are named, the XML is easier to understand
> as it does not rely on implicit position-dependent parameters. IT's more
> redable which is probably counter-intuitive.
>
> In terms of Jonathan's original need for wrappers, there is some
> possibility that they could be auto-generated using the Ant introspection
> facilities. One problem is that most Ant tasks support a multitude of
> attributes. As I said above, XML is quite nice there as it allows
> attributes to be named. You only provide the attributes you want and mostly
> everything else takes sensible defaults. In the copy example, think about
> what the wrapper would look like that exposes all the capabilities of
> <copy>
> (http://ant.apache.org/manual/CoreTasks/copy.html). I think it could be
> quite ugly unless your scripting language also supports named attributes -
> I have limited experience with Jython, so I can't say.
>
> BTW, I once experimented with converting build files to scripts (code
> actually). I  wrote an XSL template to turn a simple Ant build into a Java
> program. It was effectively a build compiler (part of Mutant's botostrap
> process).
>
> Here's the XSL
> http://cvs.apache.org/viewcvs.cgi/*checkout*/ant/proposal/mutant/build/Atti
>c/bootstrap.xsl?rev=1.3&content-type=text/plain
>
> You can see the result here.
> http://cvs.apache.org/viewcvs.cgi/ant/proposal/mutant/src/java/bootstrap/or
>g/apache/ant/builder/Attic/MutantBuilder.java?rev=1.7&content-type=text/vnd.
>viewcvs-markup
>
> Not that useful but a bit more grist for this mill. It certainly showed me
> that it is hard to cram the expressiveness of the XML approach into method
> calls. I had to limit myself to a very narrow subset of tasks and their
> usage patterns.
>
> Conor
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org


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


Re: Ant scripting from Jython.

Posted by James Duncan Davidson <du...@x180.net>.
On Thursday, Jun 12, 2003, at 22:23 US/Pacific, Conor MacNeill wrote:

> http://marc.theaimsgroup.com/?l=ant-dev&m=105516662409107&w=2

> Instead of the "script driving Ant tasks" approach, <scriptdef> is 
> more like a
> script within a task. As such, the scripts would potentially be more 
> focussed
> on the things that need to be scripty rather than doing the whole 
> build in
> the script.

Cool. My only thought here is that I've been doing quite a bit of work 
with Sherlock where you can define functionality in either JavaScript 
or XQuery and all the little scriptlets are embedded in tags. After a 
few months of playing with that approach, I've really decided that I 
dislike writing code between XML tags. Mental shifts and all that. But 
that's just a personal finding. YMMV.

Also, there's an entire discussion that could be had of where the 
control flow should be (inside or outside).


> After all, IMHO,
>
> copyWrapper("C:\\java\\jython_ant\\src\HelloJython.java",
> "C:\\java\\jython_ant\\temp", project)
>
> doesn't end up any "better" than
>
> <copy file="C:\java\jython_ant\src\HelloJython.java"
> 	  todir="C:\java\jython_ant\temp"/>

No, in this example it doesn't. But it'll be interesting to see if the 
full expressiveness of the language can be brought to bear on the 
problem. I'm not endorsing making the big shift now, but I do like 
Jonathan's approach as an experiment. If it turns out that having the 
full expressiveness of the language allows a shift in the way that you 
can do things, then great, it's a success. If all you've done is like 
you've shown above and end up with simple replacements, then it's still 
nice (though maybe not as nice) from the point of shifting logic from 
the inside of the tag to the outside.

> In terms of Jonathan's original need for wrappers, there is some 
> possibility
> that they could be auto-generated using the Ant introspection 
> facilities. One
> problem is that most Ant tasks support a multitude of attributes.

Possible. Though auto-gen code usually doesn't do that good a job at 
allowing a paradigm shift in thinking.

> Not that useful but a bit more grist for this mill. It certainly 
> showed me
> that it is hard to cram the expressiveness of the XML approach into 
> method
> calls. I had to limit myself to a very narrow subset of tasks and 
> their usage
> patterns.

It is a tradeoff. And it's one that will take a bit of experience with 
playing with a new model to figure out if its worthwhile or not. In any 
case, I'm still off playing with Mac stuff so my involvement will be 
limited to just seeing how things turn out.

James Duncan Davidson
Coder, Speaker, Author
http://x180.net/
[life live];


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


Re: Ant scripting from Jython.

Posted by Conor MacNeill <co...@cortexebusiness.com.au>.
Hi Duncan,

You all might like to look at two recent threads in ant-dev and ant-user that 
are somewhat related to this topic.

http://marc.theaimsgroup.com/?l=ant-dev&m=105516662409107&w=2

and

http://marc.theaimsgroup.com/?l=ant-user&m=105491111510585&w=2

Instead of the "script driving Ant tasks" approach, <scriptdef> is more like a 
script within a task. As such, the scripts would potentially be more focussed 
on the things that need to be scripty rather than doing the whole build in 
the script. After all, IMHO,

copyWrapper("C:\\java\\jython_ant\\src\HelloJython.java",
"C:\\java\\jython_ant\\temp", project)

doesn't end up any "better" than

<copy file="C:\java\jython_ant\src\HelloJython.java" 
	  todir="C:\java\jython_ant\temp"/>

In fact because the attributes are named, the XML is easier to understand as 
it does not rely on implicit position-dependent parameters. IT's more redable 
which is probably counter-intuitive.

In terms of Jonathan's original need for wrappers, there is some possibility 
that they could be auto-generated using the Ant introspection facilities. One 
problem is that most Ant tasks support a multitude of attributes. As I said 
above, XML is quite nice there as it allows attributes to be named. You only 
provide the attributes you want and mostly everything else takes sensible 
defaults. In the copy example, think about what the wrapper would look like 
that exposes all the capabilities of <copy> 
(http://ant.apache.org/manual/CoreTasks/copy.html). I think it could be quite 
ugly unless your scripting language also supports named attributes - I have 
limited experience with Jython, so I can't say.

BTW, I once experimented with converting build files to scripts (code 
actually). I  wrote an XSL template to turn a simple Ant build into a Java 
program. It was effectively a build compiler (part of Mutant's botostrap 
process).

Here's the XSL
http://cvs.apache.org/viewcvs.cgi/*checkout*/ant/proposal/mutant/build/Attic/bootstrap.xsl?rev=1.3&content-type=text/plain

You can see the result here.
http://cvs.apache.org/viewcvs.cgi/ant/proposal/mutant/src/java/bootstrap/org/apache/ant/builder/Attic/MutantBuilder.java?rev=1.7&content-type=text/vnd.viewcvs-markup

Not that useful but a bit more grist for this mill. It certainly showed me 
that it is hard to cram the expressiveness of the XML approach into method 
calls. I had to limit myself to a very narrow subset of tasks and their usage 
patterns.

Conor



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


Re: Ant scripting from Jython.

Posted by James Duncan Davidson <du...@x180.net>.
On Thursday, Jun 12, 2003, at 13:20 US/Pacific, Jonathan Simon wrote:

> The last example is about ANT. I spoke to Erik (hatcher) and James 
> (duncan
> davidson) about the whole abuse of XML in ANT proper not to mention 
> all of the
> stuff on the ant-contrib project.

Yep. I've been thinking for a while it's worthwhile to look at using a 
first class scripting language to actually build real "build-scripts". 
Of course, I haven't moved anywhere with it because I've been having 
way too much fun over with Mac OS X and Cocoa lately. :) Lack of itch 
to scratch leads to lack of action.

> The point is that calling ANT from Jython is much easier than I 
> initially
> thought (you dont have to write ant translation stuff) but harder than 
> I
> secondly thought (you have to write some wrappers or something 
> somewhere to
> make the code legible).

I agree with that assessment. I think that you'll end up with quite a 
bit of a "wrapper" library in Jython to make it more "script native" to 
build a build-script--but one that still uses the underlying task 
objects. Whether that library is written in Jython or in additional 
mediator objects will be a subject to be debated.

> Additionally, if we do this right, well be able to plug in any Java 
> scrpting language of choice
> (like JRuby). Then, the whole argument of the use of XML goes away and 
> the purists and iconoclasts can all have their way-- without forking 
> ANT.

And that would be a Really Good Thing(tm). I think such an effort would 
be a great thing to see and I hope that others join you in your quest. 
I do think using something like a BSF would be good so that you could 
use any scripting language--though I would be a bit concerned about 
allowing any scripting language because the n-dimensionality of the 
support burden goes up. And of course if you do use something like BSF, 
you'd want to put the "wrapper" functionality into mediator objects so 
that all languages could take advantage of it.

Anyway--it certainly sounds like a great thing to work out and there 
are other people that have had the same thought. I spent a while 
talking with Ken Arnold about these thoughts and there have been other 
hallway conversations at JavaOne with other folks that touched on this. 
There's a train there building up momentum. Have fun with it!

(cc me on any replies you'd like me to see since I'm not subbed up to 
the ant-dev mailing list right now)

James Duncan Davidson
Coder, Speaker, Author
http://x180.net/
[life live];


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