r/java • • Mar 30 '21

The Problem with Gradle

https://www.bruceeckel.com/2021/01/02/the-problem-with-gradle/
48 Upvotes

48 comments sorted by

40

u/Infeligo Mar 30 '21

This article has already been discussed 2 month ago:
https://www.reddit.com/r/java/comments/ksf3ku/the_problem_with_gradle/

There were some great comments and a link to the answer by one of Gradle developers.

2

u/cryptos6 Mar 30 '21

I wonder why reddit didn't recognize it as a known URL then 🤔

1

u/dpash Mar 31 '21

Duplicate detection is a per-subreddit setting.

26

u/dpash Mar 30 '21

A lot of the confusion comes from the fact that while Gradle has a vision, it should be better shared and explained. It’s also victim of its age which means success: the road towards fine grained execution, safe parallelism, configuration, … is hard and there are some APIs which are sub-optimal for this, and maintaining backwards compatibility is a day-to-day challenge.

Which I think is the biggest problem with Gradle: a lack of documentation about how to do things the modern way, but lots of articles on the web on how to do it the old wrong way.

Gradle has evolved and so has the best way to write your build scripts. Sadly there articles haven't. It'll take a while for people to catch up.

(As a maintainer of a project with 62 Gradle subprojects, I can confirm when you get it right, Gradle is hands down better than any other tool in the Java space. It just takes a while to get there)

1

u/flavius-as Mar 30 '21

I've never figured out how to use the new module system and my own directory layout.

The only thing that works exactly the way I want is writing my own shell scripts.

3

u/dpash Mar 30 '21 edited Mar 30 '21

You don't need to use the module system, and I honestly don't care where my files are as long as the IDE can find them. The Maven layout is as good as any other and there's an advantage for standardising file locations.

1

u/[deleted] Mar 30 '21

I recently replaced a shell script of ~700+ lines with gradle. It was difficult and time-consuming, but it works. (But only sometimes :))

0

u/[deleted] Mar 30 '21

[deleted]

3

u/segv Apr 01 '21

There's also this thing i've noticed when working with teams that use Gradle: They tend to gravitate towards a state where there's "that one person" that know wtf is actually going on in the build scripts - not because lack of talent or anything, other developers are just busy doing other stuff.

When "that one person" is not around (busy with some other Important Thingtm, on vacation, in different team or moved on completely) and there's a problem with the build, work grinds down to a halt, nobody seems to know what is going on and absolute chaos ensues.

Maven has the advantage with its declarative and relatively rigid workflow - if you know Maven, you can very quickly get up to speed with whatever is in the build files.

More anecdotal evidence: My corp requires any and all access to our Nexus to be authenticated (including reads/dependency fetch) via access tokens. With Maven I was able to figure out how to do it in a couple of hours - declare server ids in the project pom, declare servers in settings.xml with ${env.nexusUsername} and ${env.nexusToken} as server user/pass, and pass the actual tokens as secrets (well, special environment variables) from the jenkins build - not super obvious, but works and you can keep tokens secret. With Gradle the team that used it spent like 3 days trying to figure it out and the best solution they ended up was hardcoding and commiting the access tokens to the repo... yeah, it wasn't a great experience :(

But am I saying that Maven is perfect? Heck no. It has a ton of flaws, but once you set it up, you don't have to fiddle with it as much.

2

u/wildjokers Apr 03 '21

With Gradle the team that used it spent like 3 days trying to figure it out and the best solution they ended up was hardcoding and commiting the access tokens to the repo... yeah, it wasn't a great experience :(

Then you need a new gradle team. It works for gradle just as you describe for maven.

2

u/dpash Mar 30 '21

Which is why you keep logic in one or more plug-ins and keep your build scripts very lean. Changes then only need to happen to the plug-in.

2

u/oblio- Mar 31 '21

Plugins are annoying to write and test. Much more annoying than scripts.

1

u/cryptos6 Mar 31 '21

Thanks for sharing the link to the answer of a (former) Gradle developer. I think the central point is this:

A lot of the criticism we see on Gradle is because there’s a mismatch between the mental model of what Gradle is, and the surface, which is the language(s) it uses to configure the actual model.

You can not easily understand the underlying model if the language describing this model in a bad way. Maybe some more usability testing at that level should have happened (or did it happen at all?). The Kotlin DSL was in the works for years and the result is just mediocre. It looks like a DSL for the sake of being a DSL and not to make developers lives easier. Let's say it loud and clear: the Gradle DSL is awful. In many cases it would be much better to just use usual language constructs (of Kotlin).

2

u/RockleyBob Mar 30 '21

Did... did you just “stackoverflow” this reddit post?

9

u/ziano_x Mar 30 '21

I share the author's sentiment. After working with maven for 5+ years and recently moving a project to Gradle, I find myself stumped every day. Maybe it is just me. The docs are pretty good but it is always a chore to go through the docs and try to understand what went wrong. Intellij integration for multi-module gradle projects is also not that great.

Also, I think that gradle is more resource intensive than maven. But I might be wrong on that.

5

u/dpash Mar 31 '21

Gradle builds are generally significantly faster than Maven. Firstly, Gradle is really good at avoiding doing tasks it doesn't need to. It knows what inputs a task has and what outputs and wont bother running a task of nothing has changed.

Secondly, it's much better at parallelizing your build because it does it at the task level. Maven can only do it at the module level. Have a single maven module? No parallelization for you.

The Gradle daemon can also reduce start up time by avoiding reconfiguring your project. It's also worth spending some time to make sure your project is configured to avoid creating tasks during the configuration process of it doesn't need to. The scan feature is very useful for profiling your build and improving it.

1

u/ziano_x Apr 01 '21

Thanks! I would love to read up on these performance related features. Is there a guide/blog focused mainly on these performance enhancing bits of Gradle?

1

u/dpash Apr 01 '21

The Gradle blog is useful, as is the gradle documentation, although the latter can be a bit dry. I'd also recommend Cedric Champeau's blog.

1

u/wildjokers Apr 03 '21

Intellij integration for multi-module gradle projects is also not that great.

Can you expand on this? IntelliJ support for gradle multi-project and composite builds is outstanding.

8

u/wsppan Mar 30 '21

To do anything you have to know everything

You’re not Configuring, You’re Programming

Groovy is Not Java

Gradle Uses a Domain-Specific Language

There are Many Ways to do the Same Thing

Magic

Looks like a laundry list of why you should not use Gradle. This is my problem with Gradle and to a lesser extent, Maven. When I started writing Rust code I was very pleasantly surprised with Cargo. Its pretty much the anti-pattern of the above points.

4

u/neutronbob Mar 30 '21

This is my problem with Gradle and to a lesser extent, Maven

Not sure I understand how Maven is only 'to a lesser extent'. Maven does not suffer at all from some of these problems, and only to a minor extent the ones that remain.

Numbers correspond to the points you cite:

  1. This is certainly not true of Maven: you can do plenty knowing just a few basics.

  2. Maven is convention over configuration. There is effectively no programming, certainly not in the sense there is in Gradle

  3. Maven doesn't use Groovy

  4. Maven uses XML. There is no DSL

  5. In Maven, there is generally one usual way of doing something. You bring in a plugin for what you need and you're done.

  6. There's a little magic, but it's mostly a blue-collar solution that does its work in straightforward fashion.

6

u/wsppan Mar 30 '21

5 and 6 are what I mostly mean. Can get very complicated still, especially with multi-project repos. Plugins are some black box magic. But overall a much better build environment than gradle and ant but really wish there was a cargo for java.

1

u/ForeverAlot Mar 31 '21

The plugins you need to build a basic Java project are included in the "default" POM; although, due to backwards compatibility, with versions you will typically want to update. You only really need to add plugins when stepping off the beaten path. In comparison, build.rs supports arbitrary code execution.

1

u/wsppan Mar 31 '21

The plugins you need to build a basic Java project are included in the "default" POM

Well, there's the rub. Since I started using maven over a decade ago the default pom has never been adequate. My current code base that I load an run in my IDE are 15 microservice projects, each with their own pom file and each pom file has 24 dependencies, profiles for development, acceptance testing, systems testing, and integration testing. Each of these has various plugins outside the core like builder-helper, jetty, shade, spring-boot, etc... and all require configuration and in our case outside shell scripts to run in order to build all these separate projects in sync. It's all working now and configured appropriately but I always dread the times we need to upgrade versions etc.. I do like maven but definitely not hands off, just run the default pom, kind of scenario like those I am finding in my Rust project. Though those may get complicated too sometime in the future but I am enjoying the simplicity of a TOML file and Cargo.

2

u/dpash Mar 31 '21

Maven is great if you have a simple build. Once you need more complicated things, it gets really really verbose and there's not much you can do if your build doesn't fit within the Maven lifecycle, because you can't change it. Want several tasks between a compile and test? Tough, there's not enough lifecycle steps to make it work properly.

Gradle on the other hand has no lifecycle; it just has tasks that depend on each other. It also lets you remove everything but whats different between module into a plugin that each module shares. And if you have different types of modules (say quarkus services and shared libraries), you create a plugin for each type. Then each build.gradle is just "apply a single custom 'quarkus service' plugin, declare your dependencies, set your service specific config" and that's it.

1

u/wsppan Mar 31 '21

I have a few friends that made the switch to gradle and for those with complicated builds like mine struggle with it. Except one. Her team was able to hire a full time build engineer. We are not quite there yet.

2

u/dpash Mar 31 '21

https://youtu.be/HXAV9pL5Rf8 was the video that gave me the light bulb moment. My builds are so much simpler as a result. My most complicated build is 62 modules, but most of them are under 20 lines. And most of that is dependency information.

2

u/segv Mar 31 '21

Re: maven dependencies - If those microservices are under one reactor project then <dependencyManagement/> in parent and not declaring versions in child projects might solve some of your issues. If the microservices are in independent projects then i guess a bom (dependency with type pom and scope import) could help.

1

u/wsppan Mar 31 '21

Yea, we figured it out and are pretty happy with it but it's complicated and a bit fragile. I should embrace gradle for in theory it is made for this kind of thing but not ready to substitute one kind of complication for another (or others.)

7

u/rzwitserloot Mar 30 '21

I wonder if the best way to approach 'have a really simple build tool that is nevertheless endlessly expandable' is as follows:

  1. Move base info into the simplest possible file format: colon-separated info files. They are mandatory, so that the build tool can interactively prompt you for this info and write it, instead of making assumptions where no sane assumption is possible. Most obvious culprit: Which -release flag should I pass to javac when compiling your code? No default is sane here, and yet I bet you need to search the web to remember precisely how to configure this in a pom file. (Dependencies would also be in this extremely simple format. Just a listing of GAV coords, one per line, that is all).

  2. No build files. At all. It's 100% those text files and conventions.

  3. Also by convention: There is src/build which is optional, but if it is there, it is compiled first and run immediately after. It's plain java (or plain whatever language you want - whatever language the build tool supports), and is compiled with the build tool's own API on the searchpath. This is how you customize the build. Yes, complicated, but in the end having a programmable build just is, and trying to paper over the fact by using a language that lends itself well to DSLs and then going ham on writing a DSL leads to precisely the problem Bruce is talking about. It LOOKS simple but without fully grokking what's going on, it really isn't. Thus, why not 'lean in', and say: Look, just go with conventions and it truly will be easy. And if you can't do that, then dig into the API. There is nothing in between.

5

u/DrDeathDefying1 Mar 30 '21

This approach reminds me very much of how Rust handles compilation, through a single .toml file that controls the entire package and its configuration.

3

u/sindisil Mar 30 '21

100% this. This is almost exactly what Rust's Cargo tool does: Cargo.toml files to describe the build, build.rs for those rare cases where a build script is necessary.

I'd love to use a system for the JVM platform that worked the same: TOML files to describe the build, src/build to contain build scripts for those rare times they're needed.

I do think it'd be advantageous for the the build scripts to use the "native" language for which the build tool is intended (Rust for Cargo, Java for the JVM).

Maven isn't that far from this ideal, though, isn't it (with declaritive pom.xml files and plugins written in Java)?

3

u/rzwitserloot Mar 30 '21

No, it's far removed from this ideal.

The sheer amount of plugin config you can pile into pom.xml is staggering: Both that you want a plugin to be applied as well as a vagary of settings for them. In this model if you want plugin action to occur you'd have to program it. On one hand, annoying. On the other hand: Hey, your java smart editor knows exactly what to do and can give you auto-complete stuff. The source file in your src/build would presumably be extremely short. A one-liner, even, for the simplest of 'plugins' (just make a new instance and call its go method or whatever).

Then, in maven, if you DO want to write some java code to become part of the build, it's decidedly not so simple to set that up properly and ensure that maven can build these plugins and then include them automatically - mostly you're now signed up to have 2 completely separate projects: One for your maven plugin, and then your real project, which depends on the built artefact of your maven plugin.

1

u/vikarjramun Mar 30 '21

I love the idea of src/build! Taking a page from Gradle, you could write your entire build system as a set of Groovy scripts in that directory, which would be consise, powerful, and easy to understand.

2

u/beatbrot Mar 30 '21

Which is exactly what grade offers with the buildSrc folder :)

1

u/alwaysoverneverunder Mar 31 '21

Is there a way to simply reuse a buildSrc folder in multiple projects that's not copy/paste? Or is a plugin the better solution then?

2

u/beatbrot Mar 31 '21

Yeah...this use case screams "plugin" imo.

2

u/dpash Mar 31 '21 edited Mar 31 '21

The buildSrc directory is just one way to use a plug-in in your build. You can create the plug-in as a standalone project that you publish in a repository and then reuse across multiple projects like you would any other Gradle plugin.

buildSrc is useful when that would be an overkill for just a single project.

I would recommend trying to make a plugin do one thing and one thing well and then build your build up from several plugins. Gradle very much prefers a composition model of creating builds.

You may also be interested in "precomputed script plugins" which are misnamed in my opinion. They're basically plugins written in the same syntax as you'd use for a build.gradle(.kt) which makes writing them much easier.

1

u/alwaysoverneverunder Mar 31 '21

The plugin route is what I used to get all my Spring Boot projects in line and in order to get their build.gradle as small as possible. Still... after 4 years I still like Maven more... but some comments here seem to indicate that there are some things that might change my mind, but the 'rigidity and structure' of Maven was always a plus for me.

2

u/dpash Mar 31 '21

Yes, I loved Maven for the longest time.

Gradle can be more flexible, so you need to restrict yourself from doing things too differently from the defaults yourself. And make sure you only use declarative items in your build.

Gradle can represent dependencies better than Maven can. The best example is dealing with slf4j and all its dependencies.

And it's much quicker for rebuilding projects.

1

u/alwaysoverneverunder Mar 31 '21

Well those defaults were kinda my problem... I couldn’t really figure them out or find them and when you take X amount of Gradle files they all are different and for Maven this is less the case.

When I needed flexibility for in Maven I just used the Groovy plugin, but only had to resort to that on maybe three occasions... all the rest was achieved with existing plugins. And I did some pretty complicated stuff like patching a class file in a JAR that itself was in a JAR that in turn was in a WAR (fun times with Liferay...).

Build speed wise Gradle indeed seems a bit quicker, but dependency management in Maven never was a problem for me, not even with SLF4J... or at the least not simpler/more difficult than I found it in Gradle.

What annoyed me too was that IDE support of Maven was still way better than for Gradle. But both beat Ant by miles.

4

u/istarian Mar 30 '21

Never used Gradle, but I find Maven annoying. Automatic build dependency retrieval is nice, but enforcing a seemingly less sane project structure is not.

7

u/[deleted] Mar 30 '21

Maven doesn't "enforce" that project structure. Those are defaults, which if you really felt inclined to do so, could change.

However, one thing that is cool about Maven setting those defaults and basically everyone sticking to them, is that Java projects are basically starting to look the same. One less thing to figure out about a new codebase.

3

u/transcend Mar 30 '21

I became happier with Gradle once I switched from Groovy to Kotlin DSL build files. I was never interested in learning Groovy TBH, and like the author of the article said, you can't really understand Gradle's DSL syntax unless you know Groovy and grok that Gradle's build files are actually scripts full of imperative programming. Switching to the Kotlin DSL syntax allows IntelliJ to infer variable types, and constrains the build file syntax a bit more.

4

u/dpash Mar 30 '21

Gradle gets even better when you keep task definitions out of your build.gradles. I highly recommend the talk that Cedric Champeau gave to the Madrid groovy users group last year on cleaning up the groovy build.

https://youtu.be/HXAV9pL5Rf8

1

u/transcend Mar 31 '21

Thanks very much for the link. I enjoyed the video and have started restructuring my project's build.gradle files as Cedric Champeau suggests, using plugins.

1

u/dpash Mar 31 '21

Once you're done, don't forget to use the scans to profile your build to prevent task creation, which can help improve the performance of your build.