2
votes

I'm converting our build from Ant to Gradle. Our first step is to add the Gradle build files so we can start using Gradle as our build tool. This lets us use our existing build scripts to build using Ant and convert them to Gradle over time. I want to simply have Gradle call the existing Ant build files. Our projects are all NetBeans projects, which have build.xml and nbproject/build-impl.xml files. Some of the projects require NetBeans build properties, which can be found at ~/.netbeans/6.5.1/build.properties. I have the NetBeans user.properties.file property successfully set to ~/.netbeans/6.5.1/build.properties.

When I build with Ant, I invoke this:

ant -Duser.properties.file=~/.netbeans/6.5.1/build.properties dist

This executes the dist target, which depends on the init target which depends on the targets listed below:

pre-init, init-private, init-userdir, init-user, init-project, do-init, post-init, init-check, -init-taskdefs

The targets listed above are executed in the order specified. When I invoke 'gradle dist', it invokes the init Ant target, but then it executes the targets listed above in reverse order, starting with -init-taskdefs. There are required properties which are setup in the targets before the -init-taskdefs target which aren't being setup when run from gradle.

Really, all I want to do right now is to use gradle to invoke Ant to build my projects. What's the best way to do this since using gradle to build using Ant build.xml files doesn't seem to work as expected? Do I have to resort to using exec? (I hope not).

1
Unfortunately, this is not helpful. I've already read this chapter and it doesn't address the issue. I can call my Ant targets from Gradle, but Gradle isn't executing the build the way Ant does. Gradle executes the targets in a different order then Ant, causing the build to fail. - user2180144
More information...the Ant build-impl.xml file has many targets. The dist target depends on init, and the init target depends on a list of targets including '<a bunch of targets>,init-user,<a bunch of targets>,-init-taskdefs'. -init-taskdefs tests for a property which is set by loading a properties file in the init-user target. So init-user must be processed before -init-taskdefs. Gradle seems to process the hyphenated targets first (unlike Ant), regardless of the order they are listed in. init-user must be executed before -init-taskdef for the build to work. - user2180144
The correct solution to this is to make the -init-taskdefs task dependent upon the target(s) which setup the properties init-taskdefs is dependent upon. In this case, I added 'depends="init-check"' to the -init-taskdefs target and the build now works using Gradle. One workaround is to rename the -init-taskdefs target to init-taskdefs (remove the hyphen). This also works. I'm a bit confused as to why Gradle builds targets in a different order then Ant. - user2180144

1 Answers

0
votes

I found trying to use Gradle's integration of Ant with a Netbeans project was too difficult and error prone. Instead, I use Gradle's exec() command. Below is an example, lifted from my code that builds a NetBeans Project named 'Common Library'.

task commonLibrary {
    doLast {
        ant.echo("Building Common Library")
        exec () {
            workingDir = "../netbeans/nb/CommonLibrary"
            executable = "ant"
            args = ['clean', 'jar']
        }
    }
}

I realize that wasn't the answer you were hoping for, but posted it as a possible solution for other people, particular people who aren't in a position to start reworking their build.xml files.