4

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?

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Ryan
  • 51
  • 6
  • 1
    The artefacts should be cached in user.home_dir/.m2/repository... does this program not run faster the second time around? – sjr May 10 '12 at 15:14
  • Unfortunately it runs just as slowly on subsequent runs. Does not appear to be caching, nor do I have a .m2 directory under my user. Is the path different on Win machines? – Ryan May 10 '12 at 15:51
  • 1
    The repo is still under your home directory on windows. All you need is the right jars on your classpath, it doesn't matter how they get there. – Dave Newton May 10 '12 at 16:12
  • Appreciate it Dave, is there any configuration needed after adding the jars to the class path? Furthermore, is there a separate Groovy class path from a Java one? The documentation I've found so far has been lacking – Ryan May 10 '12 at 17:17
  • 1
    There's only one classpath; the JVM one. – Dave Newton May 10 '12 at 22:09

3 Answers3

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
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
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.

Community
  • 1
  • 1
Ryan
  • 51
  • 6
  • 2
    I'd be a little cautious about putting them in an environment-wide location, since you may need different libraries, or different versions of the same libraries, when running other scripts. If you *really* have a restriction of only using the Groovy file (which is a *weird* restriction, btw!) you may not have an option, but it can cause problems down the line. – Dave Newton May 12 '12 at 12:47
  • 1
    Well the idea is to cleanup the current build configuration by replacing .bat and ant .xml files with an all-encompassing groovy script, however replacing the ant sshexec calls simply wasn't working out of the box in groovy. If there is a cleaner way of installing the libraries locally as opposed to downloading each time I'm all ears :) Trying to avoid Grape usage as it just seems to be complicating what I think should be a simple configuration issue. – Ryan May 16 '12 at 15:24
  • It shouldn't be downloading them each time, though. Putting the jars on the classpath (assuming no grape, I mean) should be sufficient. – Dave Newton May 16 '12 at 15:27
  • Perhaps I am misunderstanding the classpath then, when I `echo %CLASSPATH%` in terminal it refers to a specific jar file. Am I supposed to put the jars I downloaded in the same folder as this one? – Ryan May 16 '12 at 15:30
  • No, the classpath contains a list of all the jar files, and class directories, available to a Java app. – Dave Newton May 16 '12 at 15:35
  • So if my classpath says a specific jar then that is the only file available to my Java apps? – Ryan May 16 '12 at 15:42
  • Correct (although it may depend on the Java version; I vaguely remember earlier versions would automatically add the current directory, but I could be completely wrong on that). Not sure if the `-cp` command-line arg replaces `$CLASSPATH` or not; you'd have to check. – Dave Newton May 16 '12 at 15:56
0

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.

Jared
  • 39,513
  • 29
  • 110
  • 145
  • One of the limitations of the system I'm working with is that the groovy script is the only file allowed. Trying to resolve clutter. – Ryan May 10 '12 at 17:19