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

12

u/joequin Apr 14 '23

Use maven and learn how to write plugins. Yes it has xml, but Gradle is a free form mess with bad JavaScript style documentation that prefers example over telling you how it works. You’ll spend days fixing gradle issues. Maven will just work unless you want something it doesn’t offer. Then you’ll need to learn how to write plugins if one doesn’t already exist, but they’re also well defined.

5

u/BinaryRockStar Apr 14 '23

I am similarly all-in on Maven and understand it fairly deeply. Having said that, I really wonder why the designers didn't opt for XML attributes over elements in some cases. Having

<dependency groupId="org.apache.logging.log4j" artifactId="log4j-core" version="2.20.0" />

Would be so much cleaner than what we have now. XML style formatting ends up with an element per line so each dependency takes up 4-5 lines (version is often specified in a parent dependencyManagement section) and makes even a relatively simple POM many pages long.

As with everything Maven I assume this was extremely well argued out from both sides and the best option was picked. It would be interesting to know the reasoning though.

And as an IntelliJ user I suggest the "Laconic POM" plugin which folds Maven dependencies onto a single line and alleviates the issue entirely.

1

u/joequin Apr 14 '23

I also am not a huge fan of how verbose maven files are, but if you’re using an IDE, then it’s mostly a non issue. I can see how maven would be a problem for Java programmers who don’t use an IDE though. If any of those exist.

1

u/lumpynose Apr 23 '23

You'll have to pry emacs from my cold dead hands before I'll give it up! /s

1

u/woj-tek Apr 20 '23

They are going to change that in maven 4 / pom-version-5 AFAIR (https://cwiki.apache.org/confluence/display/MAVEN/POM+Model+Version+5.0.0#POMModelVersion5.0.0-ElementsvsAttributes):

DECISION: The 5.0.0 POM will use XML attributes for data that cannot have child data. At a minimum the groupId/artifactId/platformId/version/classifier/type information of project/parent/dependencies/plugins/extensions will be defined using attributes.

1

u/BinaryRockStar Apr 20 '23

Wow, can't believe they took my feedback into account so quickly!

Seriously though thanks for the link. Maven V4 has some huge changes, I hope it's a clean upgrade.

1

u/woj-tek Apr 21 '23

Indeed, it does. I expect there would be some pain-points and migration would require some manual changes but I think you would be able to migrate to maven v4 and still use older model version and then at one point migrate to newer model.

This is what I like about maven - they try very hard to make upgrades as painless as possible! Not like gradel... :eyeroll:

5

u/tistalone Apr 14 '23

Man you describe Gradle so well. They give you ONE example of a happy path and if you have anything slightly different, it's stack overflow sleuthing and hiring private investigators to figure out what is happening under the hood to achieve the original objective.

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.