|
The main class for groovy is supplied from ant with the following lines:
<path id="groovylibs">
<pathelement location="c:/groovy/lib/asm-2.2.3.jar"/>
<pathelement location="c:/groovy/lib/antlr-2.7.7.jar"/>
<pathelement location="c:/groovy/lib/groovy-1.6.5.jar"/>
</path>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="groovylibs"/>
Now suppose that you have written your own java class customlibs.MyClass.class in jar file
c:/customlibs/MyJar.jar and you want to call it from a groovy task within ant. Then you add
the following to your build file:
<path id="mylibs">
<pathelement location="c:/customlibs/MyJar.jar"/>
</path>
<groovy classpathref="mylibs" append="true">
import customlibs.MyClass;
// put the rest of your groovy code here
// ....
</groovy>
The following works as well:
<property name="myClassPath" value="c:/customlibs/MyJar.jar"/>
<groovy classpath="${myClassPath}" append="true">
import customlibs.MyClass;
// put the rest of your groovy code here
// ....
</groovy>
So it is possible to directly supply a classpath to a groovy task but defining a classpath with
a path element gives more possibilities:
- You can add more jar files to the path mylib. These will then be added to the
classpath too. Note also that you can use ant properties in the location attributes of the of the
pathelement.
- You can include other paths inside path mylibs
See the example below. In my case the other libs are the jdom java libraries being used in my
custom classes. By defining the jdom libraries in a separate jar file I can use them in other paths
as well, for instance in a groovy task my friend Pete has written.
<property name="jdomlibs" value="c:/customlibs"/>
<property name="otherlibs" value="c:/otherlibs"/>
<property name="petesLibs" value="c:/peteslibs"/>
<path id="jdomlibs">
<pathelement location="${jdomlibs}/jdom1.jar"/>
<pathelement location="${jdomlibs}/jdom2.jar"/>
</path>
<path id="mylibs">
<pathelement location="${customlibs}/MyJar.jar"/>
<path refid="jdomlibs"/>
</path>
<path id="petesLibs">
<pathelement location="${petesLibs}/aJar.jar"/>
<path refid="jdomlibs"/>
</path>
|