Issues using the sshexec optional ant task


(luis) #1

Hi,

If I do this

task ls << {
  ant.sshexec(host: 'myhost', username: 'myuser', trust: 'true', keyfile: "\${user.home}/.ssh/my-key.pem", command: 'ls /')
}

then gradle complains about the ant-jsch jar not being on the classpath:

Execution failed for task ':ls'.
> 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

If I explicitely add a configuration for the jar and use taskdef like this:

configurations {
  sshAntTask
}
  repositories {
  mavenCentral()
}
  dependencies {
  sshAntTask 'org.apache.ant:ant-jsch:1.8.4'
}
  task ls << {
  ant.taskdef(name: 'sshexec',
      classname: 'org.apache.tools.ant.taskdefs.optional.ssh.SSHExec',
      classpath: configurations.sshAntTask.asPath)
  ant.sshexec(host: 'myhost', username: 'myuser', trust: 'true', keyfile: "\${user.home}/.ssh/my-key.pem", command: 'ls /')
}

Then the ls command works but I get the following warning just before that:

Trying to override old definition of datatype sshexec

I don’t mind the warning if its a known issue but I’d like make sure I’m not doing something wrong. This is just a little test case I resorted to try and clear things up because I was having problems using ant similarly from a custom task’s method.


(Peter Niederwieser) #2

Do you also get this message when running without the Gradle daemon?


(luis) #3

I do. Using

gradle ls --no-daemon --debug

I see the lines near that message are

10:40:23.516 [DEBUG] [org.gradle.api.internal.project.ant.AntLoggingAdapter] Could not load class (org.apache.tools.ant.taskdefs.optional.ssh.SSHExec) for type sshexec
10:40:23.517 [DEBUG] [org.gradle.api.internal.project.ant.AntLoggingAdapter] Could not load class (org.apache.tools.ant.taskdefs.optional.ssh.SSHExec) for type sshexec
10:40:23.517 [WARN] [org.gradle.api.internal.project.ant.AntLoggingAdapter] Trying to override old definition of datatype sshexec

I can send you the full trace privately if you wish.


(Peter Niederwieser) #4

We’ll have to look deeper into this. By the way, there’s a great SSH plugin for Gradle: https://github.com/int128/gradle-ssh-plugin


(luis) #5

Thanks yes I discovered the SSH plugin yesterday while trying to figure this out. It looks brilliant but I’m calling ant from within a utility class that is used by a custom task that’s in buildSrc. Maybe I can call the SSH plugin’s api.

Otherwise I just noticed some examples of using ant from within a custom task in the gradle source code like the JacocoMerge task so I’ll see if tweaking my code to fit that avoids the issue. It looks like the Jacoco plugin is explicitly configuring a configuration and dependencies so it can taskdef the different Jacoco ant tasks.