Discussion:
optional task in ant build.xml
midnec
2009-07-29 20:20:04 UTC
Permalink
I love gradle, but I have hit a problem. I am trying to execute an ant
target in a build.xml file from gradle.
When I execute the task from ant, it works. From gradle it does not work.

The build.gradle just imports the ant script (it is a sub-project). The ant
task uses an optional ant task (sshexec). The error is that ant can't find
the jar for the optional component.

The last wrinkle is that I would like to be able to use gradlew, but this is
not a hard requirement. I have the jar I need, but I cannot figure out how
to get antbuilder to see it.

I have read through the manual (sec 30.3. Ant optional dependencies in
particular), searched the web, but I can't find a solution. The closest
thing I have found referred to an ftp task, but I can't translate that into
a solution for my problem.

Thanks
--
View this message in context: http://www.nabble.com/optional-task-in-ant-build.xml-tp24726793p24726793.html
Sent from the gradle-user mailing list archive at Nabble.com.
Adam Murdoch
2009-07-30 00:19:45 UTC
Permalink
Post by midnec
I love gradle, but I have hit a problem. I am trying to execute an ant
target in a build.xml file from gradle.
When I execute the task from ant, it works. From gradle it does not work.
The build.gradle just imports the ant script (it is a sub-project). The ant
task uses an optional ant task (sshexec). The error is that ant can't find
the jar for the optional component.
There's no really nice solution to this problem. Here are some potential
work-arounds:

An easy fix for this is to drop ant-jsch.jar and jsch.jar into
$GRADLE_HOME/lib. This has a few problems, the main one in your case
that it won't work with gradlew.

Another option is to inject the JARs into the appropriate classloader.
Here's some code to do this. You only need to do this in one project per
build - the root project would be a good option:

configurations { antssh }
dependencies { antssh group: 'org.apache.ant', name: 'ant-jsch',
version: '1.7.0' }

configurations.antssh.each { File jar ->
ant.getClass().classLoader.addURL(jar.toURL()) }

This should work fine with gradle or gradlew.

We will make it easier to use ant optional tasks in a later Gradle
release. See http://jira.codehaus.org/browse/GRADLE-574


Adam
midnec
2009-07-30 13:34:58 UTC
Permalink
Thank you for the quick and helpful reply! Everything works fine now. Gradle
is very powerful, but the documentation must be a never ending task (and the
documentation is good). For those of us new to dependencies, and not used
to Maven, it can be a little confusing.

Thanks again,

-midnec
Post by Adam Murdoch
Post by midnec
I love gradle, but I have hit a problem. I am trying to execute an ant
target in a build.xml file from gradle.
When I execute the task from ant, it works. From gradle it does not work.
The build.gradle just imports the ant script (it is a sub-project). The ant
task uses an optional ant task (sshexec). The error is that ant can't find
the jar for the optional component.
There's no really nice solution to this problem. Here are some potential
An easy fix for this is to drop ant-jsch.jar and jsch.jar into
$GRADLE_HOME/lib. This has a few problems, the main one in your case
that it won't work with gradlew.
Another option is to inject the JARs into the appropriate classloader.
Here's some code to do this. You only need to do this in one project per
configurations { antssh }
dependencies { antssh group: 'org.apache.ant', name: 'ant-jsch',
version: '1.7.0' }
configurations.antssh.each { File jar ->
ant.getClass().classLoader.addURL(jar.toURL()) }
This should work fine with gradle or gradlew.
We will make it easier to use ant optional tasks in a later Gradle
release. See http://jira.codehaus.org/browse/GRADLE-574
Adam
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
View this message in context: http://www.nabble.com/optional-task-in-ant-build.xml-tp24726793p24738564.html
Sent from the gradle-user mailing list archive at Nabble.com.
Taytay
2012-02-09 13:33:25 UTC
Permalink
I am responding to this old post because the archive for it was the closest
thing I found to an answer, and I'm hoping this helps someone else with the
same issue I was having!

In short, the above didn't work for me.

Here's what I added to my root gradle.build:


//BEGIN make sure that the optional ant stuff works:

configurations { antssh }

dependencies {
antssh group: 'org.apache.ant', name: 'ant-jsch', version: '1.8.2'
}

// add additional jars to the AntBuilder classpath for use within the
imported build.
ClassLoader antClassLoader = org.apache.tools.ant.Project.class.classLoader

configurations.antssh.each { File f ->
antClassLoader.addURL(f.toURI().toURL())
}
//END: Make sure that the optional ant stuff work

I posted about it on my blog: http://taytay.com/?p=309

Thanks to this response by TheKaptain, I was alerted to the answer:
http://gradle.1045684.n5.nabble.com/Calling-ant-test-target-fails-with-junit-classpath-issue-newbie-td4385167.html

Cheers

--
View this message in context: http://gradle.1045684.n5.nabble.com/optional-task-in-ant-build-xml-tp1434718p5469431.html
Sent from the gradle-user mailing list archive at Nabble.com.
electrikshepherd
2014-04-18 21:17:10 UTC
Permalink
To anyone who lands on this post via a Internet search, there's now a more
elegant solution as described at the bottom of this page:
http://www.gradle.org/docs/current/userguide/organizing_build_logic.html

Here's an example:

configurations { antssh }
dependencies {
antssh group: 'com.jcraft', name: 'jsch', version: '0.1.51'
antssh group: 'org.apache.ant', name: 'ant-jsch', version: '1.9.3'
}

task ftp << {
ant {
taskdef(name: 'scp',
classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp',
classpath: configurations.antssh.asPath)
scp(localFile: "myfile.war", remoteTofile: "***@server:myfile.war",
keyfile: "mykeyfile", trust: true, verbose: true, sftp: true)
}
}



--
View this message in context: http://gradle.1045684.n5.nabble.com/optional-task-in-ant-build-xml-tp1434718p5712598.html
Sent from the gradle-user mailing list archive at Nabble.com.

Loading...