4

我正在用 groovy 编写我的第一个自动化脚本,但遇到了障碍。在使用 AntBuilder 类运行 sshexec() 时,我遇到了以下错误:

: 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

到目前为止,我为此找到的最佳解决方案是使用

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

为了加载所需的模块。但是,我想消除 Grape 从远程 Maven 存储库下载 jar 的延迟时间。

有没有办法下载和保存模块以供将来使用,也许在 JAVA_PATH 或类似的东西中?

4

3 回答 3

2

使用 Grape 注解在运行时下载脚本依赖项:

@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")

如果您在防火墙后面,您还可以配置 Maven 存储库的位置:

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

最后提示,属性groovy.grape.autoDownload可用于控制葡萄是进行远程下载还是仅使用其缓存文件。

groovy -Dgroovy.grape.autoDownload=false myscript.groovy
于 2012-05-10T22:02:45.597 回答
1

将所需的 jar 添加到 %ANT_HOME% 和 %GROOVY_HOME% 不起作用。

解决方案是将罐子放在 %USERPROFILE%.groovy\lib 下 - 之后不再需要 Grape 调用。希望这对其他有同样问题的人有用。

感谢戴夫让我走上正轨。

于 2012-05-11T13:16:56.150 回答
0

假设您只在几台机器上运行您的代码,我将创建一个 grapeConfig.xml 文件并将 ivy.cache.ttl.default 设置为 30 天左右。这将告诉 Grape,如果您有一个使用版本范围的依赖项,仅每 30 天检查一次远程存储库以获取更新的依赖项。有关更多详细信息,请参阅此帖子

于 2012-05-10T17:10:31.110 回答