optional task in ant build.xml

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

optional task in ant build.xml

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.  

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
Reply | Threaded
Open this post in threaded view
|

Re: optional task in ant build.xml

Adam Murdoch-2


midnec wrote:
> 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


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: optional task in ant build.xml

midnec
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


Adam Murdoch-2 wrote

midnec wrote:
> 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


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: optional task in ant build.xml

Taytay
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
Reply | Threaded
Open this post in threaded view
|

Re: optional task in ant build.xml

electrikshepherd
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: "user@server:myfile.war", keyfile: "mykeyfile", trust: true, verbose: true, sftp: true)
    }
}