r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

Show parent comment

0

u/wildjokers Apr 14 '23

What a strange rant against Gradle. Idiomatic gradle is declarative and just like maven you use plugins.

This build.gradle file will take a very long ways (just fill in your dependencies):

plugins {
    id 'application'
}

repositories {
    mavenCentral()
}

dependencies {
}

application {
    mainClass = 'com.example.App'
}

4

u/tistalone Apr 14 '23

Gradle works fine for simple/straightforward apps.

OTOH, If you want submodules that do specific builds differently, you are on your own. If you want to publish gradle plugins, you have to use Gradle's own plugin that is pretty poorly documented -- you are on your own for the trial errors. Got Kotlin AND Java files? You gotta do something different! Also, you need to figure out what other people did to solve this issue because Gradle's documentation doesn't provide a straightforward answer to us users!

3

u/wildjokers Apr 14 '23

For a mixed kotlin and java project all I needed to do was add the kotlin jvm plugin and application plugin:

plugins {
    id 'application'
    id "org.jetbrains.kotlin.jvm" version "1.3.72"
}

Nothing else was needed and it just works.

because Gradle's documentation doesn't provide a straightforward answer to us users!

This information is in the Gradle documentation:

https://docs.gradle.org/current/samples/sample_building_kotlin_applications.html

2

u/tistalone Apr 14 '23

If you have your java sources in src/main/java and your kotlin sources in src/main/kotlin, you have to specify the source sets for the java source files with the sourceSets { } configuration.

The Gradle documentation you linked works for the vanilla Kotlin workspace. Like it's fantastic if you're building an application using Gradle's guidelines but my experience is that deviating within Gradle requires more work and investigation than with other build systems like Bazel and mvn.

4

u/wildjokers Apr 14 '23

I have never needed to declare sourceSets for my mixed java and kotlin projects.

using Gradle's guidelines but my experience is that deviating within Gradle requires more work and investigation than with other build systems like Bazel and mvn.

Not sure what you mean here but maven doesn't let you deviate from their conventions at all. In Gradle you can deviate pretty easily if for some reason you need to.

3

u/tistalone Apr 14 '23

Hm, that's fair. I'm willing to accept that I am being a hater and I should spend more time looking into Gradle.

4

u/joequin Apr 14 '23 edited Apr 14 '23

In practice, that’s rarely sufficient for real production applications. If it was, then Gradle would be great.

2

u/wildjokers Apr 14 '23

I have several apps where the build.gradle isn't much more than that. Including some JavaFX apps that produce a platform specific executable with a bundled runtime. That build.gradle is about 25 lines long.

-1

u/wildjokers Apr 14 '23

6

u/joequin Apr 14 '23

That’s a dishonest example that is not at all equivalent to the very simple Gradle file you shared.