'Can I avoid using Grape to load modules in Groovy?

I am writing my first automation script in groovy and I've hit a roadblock. While making use of the AntBuilder class to run sshexec() I run into the following error:

: Problem: failed to create task or type sshexec
Cause: the class org.apache.tools.ant.taskdefs.optional.ssh.SSHExec was not found.
    This looks like one of Ant's optional components.
Action: Check that the appropriate optional JAR exists in
    -ANT_HOME\lib
    -the IDE Ant configuration dialogs

Do not panic, this is a common problem.
The commonest cause is a missing JAR.

This is not a bug; it is a configuration problem

So far the best solution I've found for this is to use

Grape.grab(group : "com.jcraft", module : "jsch", classLoader : this.class.classLoader.rootLoader)
Grape.grab(group:"ant", module:"ant-jsch", classLoader:this.class.classLoader.rootLoader)

in order to load the required modules. However, I would like to eliminate the lag time of Grape downloading the jars from the remote Maven repository.

Is there a way to download and save modules for future use, perhaps in the JAVA_PATH or something to that effect?



Solution 1:[1]

Adding the required jars to %ANT_HOME% and %GROOVY_HOME% wasn't working.

The solution is to put the jars under %USERPROFILE%.groovy\lib - after which the Grape calls are no longer necessary. Hopefully this is useful to others with the same issue.

Thanks to Dave for getting me on the right track.

Solution 2:[2]

Use the Grape annotations to download the scripts dependencies at runtime:

@Grapes([
    @Grab(group='org.apache.ant', module='ant-jsch', version='1.8.3'),
    @GrabConfig(systemClassLoader=true)
])

def ant = new AntBuilder()

ant.sshexec(host:"somehost", username:"yo", password:"dude", command:"ls")

If you're behind a firewall, you could also configure the location of your Maven repository:

@GrabResolver(name="my-repo", root="http://my.hostname/maven/repo")

Final tip, the property groovy.grape.autoDownload can be used to control whether grape does a remote download or just uses its cached files.

groovy -Dgroovy.grape.autoDownload=false myscript.groovy

Solution 3:[3]

Assuming your only going to be running your code on a couple of machines I would create a grapeConfig.xml file and set the ivy.cache.ttl.default to something like 30 days. This will tell Grape that if you have a dependency which uses a version range to only check remote repositories for updated dependencies every 30 days. See this post for more details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3 Jared