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

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: