Field Notes: Wrappers, Versions & Build Logic
Habits from teams that maintain large Maven and Gradle builds. Each one prevents a specific, recurring kind of breakage.
1. Commit the wrapper, and only ever call the wrapper
mvnw/gradlew, their properties file and (for Gradle) gradle-wrapper.jar belong in Git. CI scripts, READMEs and IDE settings all use ./mvnw and ./gradlew, never a mvn or gradle from the PATH. Upgrades of the build tool then arrive as a reviewed one-line change, and "it builds on my machine" includes the tool version. Add distributionSha256Sum (Maven, Gradle) and validate the Gradle wrapper JAR in CI.
2. No allprojects {} / subprojects {} in new builds
Cross-project configuration from the root script makes every project depend on the root's evaluation, hides what applies where, and blocks Gradle's isolated and parallel configuration. Use:
dependencyResolutionManagement { repositories { … } }in settings for repositories;- convention plugins (
build-logic) for shared configuration, applied explicitly by each project; - the version catalog for versions.
The Maven equivalent of this advice: keep parent POMs thin (properties, pluginManagement, dependencyManagement, enforcer) and don't put <dependencies> in a parent, which forces them on every child.
3. No dynamic versions: latest.release, 1.+, [1.0,)
A dynamic version makes the build depend on when it runs. Watch the same, untouched Gradle build change its classpath because someone published a library:
publish() { # publish org.acme:greet:<version> to /srv/maven-repo with Maven
mkdir -p greet-$1/src/main/java/acme && cd greet-$1
cat > pom.xml <<POM
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId><artifactId>greet</artifactId><version>$1</version>
<properties><maven.compiler.release>21</maven.compiler.release></properties>
</project>
POM
echo "package acme; public class Greet { public static String v() { return \"$1\"; } }" > src/main/java/acme/Greet.java
mvn -q -ntp deploy -DaltDeploymentRepository=team::file:///srv/maven-repo
cd ..
}
publish 1.0.0
mkdir -p app && cd app
cat > settings.gradle.kts <<'KTS'
rootProject.name = "app"
dependencyResolutionManagement { repositories { maven { url = uri("/srv/maven-repo") } } }
KTS
cat > build.gradle.kts <<'KTS'
plugins { java }
dependencies { implementation("org.acme:greet:1.+") }
KTS
resolved() { gradle -q dependencies --configuration runtimeClasspath | grep -o 'org.acme:greet:1.+ -> .*'; }
echo "monday: $(resolved)"
(cd .. && publish 1.1.0)
echo "tuesday: $(resolved)"
echo "--- with dependency locking"
cat >> build.gradle.kts <<'KTS'
dependencyLocking { lockAllConfigurations() }
KTS
gradle -q dependencies --write-locks > /dev/null
grep org.acme gradle.lockfile
(cd .. && publish 1.2.0)
echo "wednesday: $(resolved)"
gradle -q dependencies --update-locks org.acme:greet > /dev/null
echo "after an explicit --update-locks: $(resolved)"monday: org.acme:greet:1.+ -> 1.0.0
tuesday: org.acme:greet:1.+ -> 1.1.0
--- with dependency locking
org.acme:greet:1.1.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
wednesday: org.acme:greet:1.+ -> 1.1.0
after an explicit --update-locks: org.acme:greet:1.+ -> 1.2.0
On Tuesday the build used code nobody on the team had reviewed or tested. With a dynamic version you can't rebuild last week's release and get last week's bytes.
- Gradle: write exact versions (in the catalog). If you must use ranges (some libraries publish them), turn on dependency locking as above:
gradle.lockfileis committed, resolution is pinned, and updates are deliberate (--update-locks). - Maven: avoid version ranges (
[1.0,2.0)) and the removedLATEST/RELEASEkeywords; the enforcer'srequireReleaseDepsandbanDynamicVersionsrules catch them. - Snapshots are dynamic versions too: never depend on a
-SNAPSHOTin a release.
4. Pin plugin versions as strictly as libraries
Maven: every plugin in pluginManagement, including the default ones (why), and requireMavenVersion in the enforcer. Gradle: plugin versions in the catalog's [plugins] or in build-logic's dependencies; never version "+". A plugin upgrade is a build-logic change, reviewed like code.
5. Keep the build declarative at the edges
The build script of a project should read like a description: plugins, dependencies, a few settings. Logic (loops, conditions, file manipulation, custom tasks) lives in build-logic, written as typed task classes with declared inputs and outputs, so it's testable, cacheable and lazy. A doLast { exec { … } } in a project script is a sign something belongs elsewhere.
6. Know where time goes before optimising
Measure first (--profile, a build scan, mvn -Dorg.slf4j.simpleLogger.showDateTime=true or the Maven profiler extension), then apply the big levers in order: Gradle's build cache and configuration cache, compile avoidance through implementation, parallel execution (org.gradle.parallel=true, mvn -T), and only then hardware. In CI, keep ~/.gradle/caches and ~/.m2/repository between runs.
Test yourself
Why are dynamic versions such as 1.+ or latest.release harmful?
- They are slower to download
- The same source can resolve different dependency versions depending on when it is built
- Gradle doesn't support them
- They break the wrapper
What does Gradle dependency locking do?
- Prevents downloads
- Records resolved versions in a committed lockfile and resolves exactly those until you update the locks
- Locks the wrapper version
- Makes dependencies read-only on disk
What should replace subprojects { apply(plugin = "java-library") } in a modern Gradle build?
- allprojects {}
- A convention plugin applied in each project's plugins block
- A larger root build script
- A Maven parent POM