r/java • • 17d ago

Beyond mvn install: Maven Practices That Pay Off

https://foojay.io/today/beyond-mvn-install-maven-practices/
132 Upvotes

19 comments sorted by

4

u/znpy 16d ago

I loathe the fact that pretty much all java build systems needs this level of complexity in order to get even a dumb hello world built and packaged.

no offense, but the article opens with "Most Maven builds start as a copy of a copy." and then proceeds to show you how to use some pre-packaged pom files, which are still copies of copies, just better packaged.

the point i'm trying to make is that java has now a multitude of build systems (apache ant, maven, gradle etc) but what is really needed is an official (in the sense of "built-in") and simple tool to build java software. Maybe extensible as well.

Most likely cargo from rust is a good example of what such a built-in system could be.

Java is in the unique position of being late to this kind of party and can to an holistic review of other similar systems in order to make the decisions on the basis of mistakes made by (and lessons learned in) other projects.

1

u/woj-tek 15d ago

Most likely cargo from rust is a good example of what such a built-in system could be.

While I agree (and suggested something like that in other thread: https://reddit.com/r/java/comments/1xp0byp/ask_the_architects_at_devoxx_2026/19sdu3vhakdpc/ ;) ) I'm not sure it will happen... Ron hinted at it so maybe but it's a long shot…

On the other hand there is polyglot-maven (https://github.com/takari/polyglot-maven) and IMHO it could be quite feasible to have basic cargo-like TOML do the basic stuff (with sane defaults)... IMHO the problem is that setting up polyglot already requires a bit of "magic" (and copying-pasting) so it's a catch22 problem :/

Would be nice to have it as OOTB experience (and possibly detected by pom file extension?

1

u/znpy 14d ago

I don't think a facelift on maven is the correct approach.

Maven is complex on its own. I had to spend a couple of weekends just to get a basic understanding of it.

Something radically different is needed in my opinion.

1

u/woj-tek 14d ago

YMMV and I'm probably biased after using maven for more than a decade.

However, for something very basic why do you thing "facelift" is not a good approach? (I'm generally very annoyed by NIH syndrome…)

I had to spend a couple of weekends just to get a basic understanding of it.

Hmm... how come? What was the main issue?

In general simple pom.xml (even with copy paste) + mvn instal is more than enough.

Nooooow, if you need something more complex (plugins, phases, whatnot) then if we were to start with something "basic like cargo or uv" we would hit a wall…

In general maven and grade are the way they are because they required to build more than just "spit out some binary"

21

u/hadrabap 17d ago

I wish more projects follow your patterns. Nice writeup.

1

u/Plane-Bake3604 17d ago

is it usually the multi-module pom layout that gets messy on the projects youre looking at, or people just defaulting to mvn install everywhere

1

u/FriendOfMandela 17d ago

I'll adopt some of these

11

u/agentoutlier 17d ago edited 17d ago

I tried to quickly scan if you had some of my tricks:

<phase>none</phase> the true skip

In most cases the property based skip are actually slower than:

 <phase>none</phase>

Why? Well because the plugin still has to be wired for the property based skip ones to work. However phase none can be fairly tricky to set this up because some plugins expect others to still be in the lifecycle even if they are not needed. For example cannot deploy if some assembly plugins are skipped because the metadata is missing even if you already have the jar built.

I'm fairly sure BTW that phases cannot be set with property other than profiles and that properties don't always work for profile triggering.

Only the fastest trivial tests should be in the same module as your "core" module

If you are using -T and friends.... actually what the hell the article does not cover this?

Well you can set that in .mvn/maven.config:

-T2C

Anyway assuming you are running your build concurrently Maven is not smart enough to run unit tests while a dependent module is building even though all it needs is the code (public API).

So I recommend making test modules for your core modules. Then your core will build fast and the rest of your project that depends on it can build concurrently including the tests.

Pinning and trigger dependabot to update Maven

This I think has been fixed recently as I think dependabot now checks maven wrapper.... which holy shit again this article does not mention but you can do this trick.

In properties:

<!--
This property is to pin the build to a specific version of maven for reproducible builds
as well as let dependabot check for maven updates.
-->
<maven.core.version>3.10.0</maven.core.version>

In dependency management:

  <!-- to make dependabot check for latest maven version -->
  <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-core</artifactId>
      <version>${maven.core.version}</version>
  </dependency>

Then in plugins:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <executions>
          <execution>
            <id>enforce-maven</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireMavenVersion>
                  <version>${maven.core.version}</version>
                </requireMavenVersion>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>

Parent root pattern

I think they fixed this in Maven 4? The problem is that in a sub submodule you cannot get the root of the project directory easy.

/root/pom.xml
/root/etc/crap-you-need-in-every-module.txt
/root/core/pom.xml
/root/test/pom.xml
/root/test/core-test/pom.xml

So the trick is some property you put in an aggregator or each project itself like:

<parent.root>${basedir}/..</parent.root>
<parent.root>${basedir}/../..</parent.root>

And yeah you just have to suck it up and copy that property to each module or aggregator.

Inheriting URLs is generally broken for most multimodules

If you have an opensource project and you have named any of your modules different than the artifactId you should be doing this:

<scm child.scm.connection.inherit.append.path="false" child.scm.url.inherit.append.path="false">

At the root pom.

I rarely learn new shit about Maven but this bug report for SLF4J made me update Rainbow Gums pom because indeed I had this issue in my pom.

The issue is that security and license tools cannot find the correct website or source control URLs for your project based on an artifact.

5

u/GermanBlackbot 16d ago

I usually don't bother with <phase>none</phase> because when I actually tried to measure the difference it was negligable. If your MM contains a hundred or so modules it adds up of course, but most of the time it's more of a headache to fiddle around with the phase than to just throw in a skip.

The one exception are plugins that for some reason verify a bunch of stuff before determining the skip flag.

"OH MY GOD, ERROR, I CANNOT RUN! I'M MISSING AN IMPORTANT RESOURCE!"
"You are not supposed to run. The skip flag is set."
"I know, but I'm missing a resource to run!"
"Yes. That's why I want you to skip your execution."
"I hear you, but don't you get it, I'M MISSING AN IMPORTANT RESOURCE!"

Had to ban this one plugin into some profile activated with a marker file. Grrrr.

2

u/agentoutlier 16d ago

Love this comment because yeah actually your point is stronger and kind of what I mean to lead with!

2

u/chabala 17d ago

Only tangentially related, you might be surprised how often folks get URLs wrong, in general, in the standard pom tags.

I run maven-linkcheck-plugin on my project, and it struggles with the 'dependencies' page. Here's an example (not mine): https://www.joda.org/joda-beans/dependencies.html

Every dependency has two important URLs that manifest as links on this page, the project.url and the license.url.

If you click through the transitive compile dependencies for joda-beans, three of the six don't link anywhere. I'm not picking on joda-beans, I literally searched for some boilerplate text from the dependency page and found a good example. It's just a persistent problem. Is it link rot or misconfiguration, hard to tell.

I've opened PRs with projects to get them to fix their license declaration too, because they just write 'GPL' in there instead of a URL to the license, and then tooling gets confused because that's not a URL, but it'll pretend I'm trying to link to a /GPL page on my site and 404.

Other lolz, take a look at the 'Licenses' section. It's too bad we couldn't decide how to say 'Apache 2', so now there's five different listings for it.

3

u/Filipehdbr 16d ago

The enforcer plugin is the only one that changes behaviour instead of taste. Banning dynamic versions alone pays for the config

2

u/definitelyoneof 16d ago

I was sure this will be another one of those articles that tries to convince you to run arcane commands that turn your build into a nightmarish, stateful guessing game, just to shave off 2s once in a blue moon. But nope. Genuinely great pieces of advice.

1

u/Square-Yak-6725 14d ago

Great write-up. I maintain XChange (about 80 modules) and I'm taking a few of these straight into our build: defaultGoal, requirePluginVersions, and plugin versions in properties. Checking against the article, I found the same plugin pinned at two different versions in our parent POM, which is exactly the drift you describe.

A few things that have paid off for us and might be worth a follow-up:

Enforcer dependencyConvergence (plus banDuplicatePomDependencyVersions and reactorModuleConvergence). For a library, this is the rule I'd miss most. It recently caught a transitive version conflict that would otherwise have landed on our users' classpaths.

BOM imports in dependencyManagement (e.g. netty-bom, junit-bom). Version properties keep your direct dependencies aligned, but only a BOM aligns the ones that arrive transitively.

Surefire/Failsafe split. Tests that hit live services run under Failsafe behind a skip property, so the default build stays fast and offline-safe.

Binding sortpom:sort to verify instead of running it by hand, so POMs can't drift out of order.

Thanks for the SBOM and outputTimestamp pointers too. Both are on my list now.

1

u/r410b 13d ago

figlet seems like a nice project.

1

u/Paw565 17d ago

Will you do a follow up for gradle?

0

u/[deleted] 13d ago

[deleted] — view deleted comment

2

u/henk53 9d ago

We did it the other way around. Replaced tons of old and complex gradle build files with modern clean maven poms.