Looking for gradle examples for ant users

(rjcarr) #1

So I’ve been using ant for ages (10+ years) and I’m generally happy but I typically have a bunch of conditional compilations and custom tasks and I know things would be so much better if and straight forward if I could just do the simple scripting right in the build file.

However, as I’m learning about gradle, there doesn’t seem to be a guide that really helps me. I feel like in ant I maybe do a dozen different things, and that’s probably pretty typical, and I haven’t found the guide that says “if you do this in ant you do this in gradle”. Does this guide exist? All of the guides seem to be about coming from maven and your project has to be in a special way and then you just use this plug-in, etc … but those things don’t work for me.

One thing in particular, for example, is it looks like you can use some sort of built-in gradle facility to do copies, but you could also use the ant builder. How would I know what the built-in facilities are and when to use them over the ant builder?

It feels like there’s a 400 page user guide but none of this is really addressed. What am I missing?

(Benjamin Muschko) #2

As a general rule I’d say try to use Gradle’s functionality over Ant’s if you can. A typical example is copying one or multiple files. If you used to use the Ant copy task before, I’d prefer Gradle’s Copy task instead. The reason for choosing Gradle’s custom task is that you get incremental build functionality for free and are automatically more expressive. When migrating from Ant you don’t need to rewrite everything in Gradle. You can choose to partially migrate the logic or even decide to keep parts of your code in Ant.

I am not aware of a guide that maps Ant concepts to Gradle in detail. The book “Gradle in Action” describes migration strategies in chapter 9 that might help you answer those questions. I don’t really want to sell you on the book, so it’s up to you whether you buy it or not. Manning is planning to give away another chapter for free soon and it is going to be that one. So if you wait another one or two weeks, you might be able to just download it from the page.

(rjcarr) #3

Thanks for the quick feedback and hopefully the book gives the information I need. I find it strange there is nothing that is like:

  • To create a property in ant you do this but in gradle you do this.

  • To do a conditional compile in ant (using if, unless) you do this but in gradle you do this.

  • To build a path in ant by specifying files you do this but in gradle you do this.

  • To copy files or folders in ant you do this but in gradle you do this.

  • Etc, etc, etc …

Like I said, I feel like a typical ant file maybe has about a dozen concepts so it shouldn’t be that involved. Also, as you said, it’d be useful to know when to use gradle functionality directly instead of using the redundant ant builder functionality.

I guess I’m just really surprised nothing like this exists, but again, thanks for the quick reply.

(Peter Niederwieser) #4

We don’t have an Ant migration guide, although it might be a good thing to have. Gradle is a higher-level build tool than Ant; often, you would use a Gradle plugin instead of declaring a bunch of Ant targets. The user guide isn’t targeted at people coming from Maven. It’s just that Gradle is closer to Maven than Ant in that it provides high-level abstractions (that eventually get mapped down to tasks). (In some other ways, Gradle is closer to Ant than Maven. For example, Gradle executes a build as a graph of dependent tasks, similar to Ant’s graph of targets.)

If you don’t use Gradle’s plugins (where they fit your needs), you miss out on some of its biggest benefits. (Note that unlike in Maven, the defaults provided by Gradle plugins can almost always be changed to your liking.) An idiomatic Gradle build will generally prefer Gradle’s own plugins/tasks/methods over Ant tasks, except in cases where an equivalent feature doesn’t exist in Gradle (e.g. when incorporating a third-party Ant task).

Apart from the Gradle User Guide, another very useful document is the Gradle Build Language Reference. Also make sure to check out the many sample builds in the full Gradle distribution.

(Benjamin Muschko) #5

Gradle doesn’t really force you to use one or the other approach. It is happy with either way. Some organizations do partial migrations from Ant to Gradle so it really depends on what you use.

It’s too bad I didn’t have this posting when I wrote the book. It would have been worth to create a mapping table for users trying to find their way. Maybe for the second edition. Why does such a guide not exist otherwise? It’s simply a matter of time and priorities. We’d welcome every contribution.