4

If you want to run a Groovy script in Ant, you can either use the script task like this: ..

<script language="groovy">
//foo
</script>

..or the groovy task like that:

<groovy>
//foo
</groovy>

Both ways require the Groovy libraries to be downloaded. I found a promising looking Ant config that does this automatically in this answer: Execute my groovy script with ant or maven

Now for my question:

Which of the two Ant tasks is meant to be used for running Groovy scripts? script or groovy?

Also, what is the purpose of the "additional" groovy task, if there's a script task included in Ant that supports groovy?

Also I'd like to quote from a blog post I found here: http://jbetancourt.blogspot.co.at/2012/03/run-groovy-from-ants-script-task.html

Of course, why would you use the 'script' task when the 'groovy' task is available? You wouldn't.

Does anyone agree with the author of this post? If so - could you explain the idea behind it?

Community
  • 1
  • 1
UnPlan2ned
  • 183
  • 1
  • 7

1 Answers1

5

+1 for Josef's statement about the groovy task (btw. his blogs http://josefbetancourt.wordpress.com/ and http://octodecillion.com/ are worth reading)
Using groovy a lot for several purposes, in ant i exclusively use the groovy task because of his slick syntax providing simple access to ant api, consider this example :

<project>
  <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

  <property name="foo" value="bar"/>

  <script language="groovy">
   project.setProperty 'foo', 'baz'
   echo = project.createTask 'echo'
   echo.setMessage 'Howdie :-)'
   echo.execute()
  </script>

  <echo>1. $${foo} => ${foo}</echo>

  <groovy>
    properties.'foo' = 'baaz'
    ant.echo 'Howdie :-)'
  </groovy>

  <echo>2. $${foo} => ${foo}</echo>

</project>

Which do you prefer ? OK, normally instead of echo. ... you would use print or println,
it's just for demonstrating the access to ant api.

Rebse
  • 10,307
  • 2
  • 38
  • 66
  • But can't you also run scripts from a file using the script task and defining the file in the src attribute? Here it says so: http://ant.apache.org/manual/Tasks/script.html `src The location of the script as a file, if not inline`. – UnPlan2ned May 22 '13 at 20:18
  • of course, you're right, forgot about that because i never use script task :) deleted that part of my answer – Rebse May 22 '13 at 20:29
  • Thanks for the example, I played around with it a bit and decided to use the groovy task! Accessing properties is really much easier that way. – UnPlan2ned May 22 '13 at 21:05