0

In our ANT build files we sometimes call groovy scripts

<groovy src="${groovy.script.dir}/DoIt.groovy"/>

Is there a way to specify one or more additional Groovy files? Our use case is that we define some constants in a separate that should be used in several scripts.

Do we have to compile your scripts to .class files in order to use them as intended, or is there a groovier way of achieving our goal?

4

2 回答 2

1

The docs for the <groovy> task say that when you specify the src attribute:

The directory containing the file is added to the classpath

So other groovy classfiles in the same folder should be accessible from the script you declare.

Is this not working?

于 2013-01-11T11:28:11.610 回答
0

I had the same kind of issue in the past, using Groovy 1.8.1. I couldn't find in the documentation how to use several script files within the same <groovy> task (see also the ant <script> task's doc). Using several tasks didn't work either for me, because the context was reset between them.

I haven't upgraded to Groovy 2.x.y as I don't use it that much. Below I illustrate a possible workaround with 1.8.1; perhaps it's still valid.

<property name="home" location="/Users/coyote/mytest/ant/src"/>
<property name="gscript1" location="${home}/my/own/pack/foo.groovy"/>
<property name="gscript2" location="${home}/my/own/pack/bar.groovy"/>

<groovy>
  evaluate( new File(properties.'gscript1') );
  evaluate( new File(properties.'gscript2') );
</groovy>

evaluate() takes either a String or a Script file, on which it performs a dynamic evaluation -according to the API doc- using the current bindings. Those are the bindings of the <groovy> task, I presume.

I'm not sure I understand your use case, but with this model you can of course define constants first; hardcoded, as properties + code, or in a groovy-script-file + evaluate.

于 2013-02-13T10:28:20.543 回答