TExcelTAD
Home › Java Builds: Maven & Gradle › Migrating a Maven Build to Gradle

Migrating a Maven Build to Gradle

A migration succeeds when, on the day you switch, the new build produces the same artifacts from the same dependencies, and the team can't tell the difference except that builds got faster. The work is mechanical if you go module by module and compare classpaths as you go.

Step 0: gradle init can start it, with the network

Run in a folder with a pom.xml, gradle init offers to convert the Maven build (a feature Gradle marks as incubating). It loads Maven's own libraries to read the POMs, which it downloads on first use, so on this offline machine it stops right there:

bash · try it yourself
mkdir conv && cd conv
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>demo</groupId><artifactId>conv</artifactId><version>1.0</version>
</project>
POM
gradle init --dsl kotlin --no-incubating 2>&1 | grep -E 'incubating feature|Could not resolve org.apache.maven:maven-core'
Maven to Gradle conversion is an incubating feature.
   > Could not resolve org.apache.maven:maven-core:3.9.5.
      > Could not resolve org.apache.maven:maven-core:3.9.5.

With network access it generates settings.gradle.kts, one build script per module and, for a multi-module build, typically a buildSrc convention plugin from the parent POM. Treat its output as a first draft: it translates dependencies and a few well-known plugins, not profiles, custom executions or most plugin configuration. Either way you'll do the steps below.

The checklist

Maven Gradle
aggregator <modules> include(...) in settings.gradle.kts
parent POM properties, pluginManagement, plugins a convention plugin (build-logic)
dependencyManagement with versions version catalog entries (+ constraints/platform where versions must be enforced)
BOM import platform(libs.x.bom)
compile scope implementation (or api if it's in the module's public API)
provided compileOnly (+ testImplementation if tests need it)
runtime / test runtimeOnly / testImplementation
surefire config tasks.test { useJUnitPlatform(); … }
shade application distribution, or the Shadow plugin
distributionManagement publishing { repositories { … } }
profiles Gradle properties (-P), providers, separate tasks
mvnw gradle wrapper

Then the one step people skip: compare what the two builds resolve. Maven picks the nearest version and Gradle the highest, so the same declarations can produce different classpaths.

A worked migration

A small Maven build, core and app, with a BOM and all four everyday scopes:

bash · try it yourself
mkdir -p mig/core/src/main/java/core mig/app/src/main/java/app && cd mig
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mig</groupId><artifactId>parent</artifactId><version>1.0.0</version>
  <packaging>pom</packaging>
  <modules><module>core</module><module>app</module></modules>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencyManagement><dependencies>
    <dependency><groupId>com.fasterxml.jackson</groupId><artifactId>jackson-bom</artifactId><version>2.22.4</version><type>pom</type><scope>import</scope></dependency>
    <dependency><groupId>com.mig</groupId><artifactId>core</artifactId><version>${project.version}</version></dependency>
    <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.20</version></dependency>
    <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.20</version></dependency>
  </dependencies></dependencyManagement>
</project>
POM
cat > core/pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <parent><groupId>com.mig</groupId><artifactId>parent</artifactId><version>1.0.0</version></parent>
  <artifactId>core</artifactId>
  <dependencies>
    <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
    <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></dependency>
    <dependency><groupId>org.jspecify</groupId><artifactId>jspecify</artifactId><version>1.0.0</version><scope>provided</scope></dependency>
  </dependencies>
</project>
POM
cat > app/pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <parent><groupId>com.mig</groupId><artifactId>parent</artifactId><version>1.0.0</version></parent>
  <artifactId>app</artifactId>
  <dependencies>
    <dependency><groupId>com.mig</groupId><artifactId>core</artifactId></dependency>
    <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><scope>runtime</scope></dependency>
  </dependencies>
</project>
POM
cat > core/src/main/java/core/Json.java <<'SRC'
package core;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jspecify.annotations.Nullable;
public class Json {
    public static String of(@Nullable Object o) throws Exception { return new ObjectMapper().writeValueAsString(o); }
}
SRC
cat > app/src/main/java/app/Main.java <<'SRC'
package app;
public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(core.Json.of(java.util.List.of("migrated", 1)));
    }
}
SRC

# --- the Gradle build, written next to the POMs ---
mkdir -p gradle
cat > gradle/libs.versions.toml <<'TOML'
[libraries]
jackson-bom = "com.fasterxml.jackson:jackson-bom:2.22.4"
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind" }
slf4j-api = "org.slf4j:slf4j-api:2.0.20"
slf4j-simple = "org.slf4j:slf4j-simple:2.0.20"
jspecify = "org.jspecify:jspecify:1.0.0"
TOML
cat > settings.gradle.kts <<'KTS'
rootProject.name = "parent"
dependencyResolutionManagement { repositories { mavenCentral() } }
include("core", "app")
KTS
cat > core/build.gradle.kts <<'KTS'
plugins { `java-library` }
dependencies {
    implementation(platform(libs.jackson.bom))
    implementation(libs.jackson.databind)
    implementation(libs.slf4j.api)
    compileOnly(libs.jspecify)
}
KTS
cat > app/build.gradle.kts <<'KTS'
plugins { application }
dependencies {
    implementation(project(":core"))
    runtimeOnly(libs.slf4j.simple)
}
application { mainClass = "app.Main" }
val printRuntime = tasks.register("printRuntime") {
    val files = configurations.runtimeClasspath.map { c -> c.incoming.artifacts.artifactFiles }
    doLast { files.get().map { it.name }.sorted().forEach(::println) }
}
KTS

mvn -q package dependency:3.11.0:copy-dependencies -DincludeScope=runtime
gradle -q build
echo "maven : $(java -cp 'app/target/classes:app/target/dependency/*' app.Main)"
echo "gradle: $(gradle -q :app:run)"
ls app/target/dependency | grep -v '^core' > maven-runtime.txt
gradle -q :app:printRuntime | grep -v '^core' > gradle-runtime.txt
diff maven-runtime.txt gradle-runtime.txt && echo "runtime classpaths are identical:" && cat gradle-runtime.txt
maven : ["migrated",1]
gradle: ["migrated",1]
runtime classpaths are identical:
jackson-annotations-2.22.jar
jackson-core-2.22.4.jar
jackson-databind-2.22.4.jar
slf4j-api-2.0.20.jar
slf4j-simple-2.0.20.jar

Same output, same runtime classpath. (The comparison leaves out core itself, which is core-1.0.0.jar in Maven's copy and core.jar in Gradle's.) jspecify is absent from both: provided and compileOnly agree.

Things this small example shows in miniature:

  • The parent's dependencyManagement became the catalog, and the BOM import became platform(...).
  • Every compile dependency was a candidate for implementation. core exposes no Jackson types in its public API, so implementation is right, and app loses Jackson from its compile classpath. That is stricter than Maven: code in app that silently used Jackson would now fail to compile, and that's a bug the migration found, not one it caused.
  • The printRuntime task is a throwaway check; in a real migration, script the comparison per module and run it in CI until the Maven build is deleted.

Running both builds for a while

For a big build, migrate in stages: keep Maven as the source of truth, add Gradle files next to the POMs, run both in CI and compare artifacts (class lists, MANIFEST.MF, resolved dependency versions). Switch once they've agreed for a few weeks, then delete the POMs in one commit. If both builds share src/, outputs don't clash (target/ vs build/).

Test yourself

Why compare resolved classpaths when migrating from Maven to Gradle?

  1. Gradle uses different repositories
  2. Maven's nearest-wins and Gradle's highest-wins can resolve different versions from the same declarations
  3. Gradle ignores transitive dependencies
  4. It's only needed for tests

What does Maven's provided scope become in Gradle?

  1. implementation
  2. compileOnly
  3. runtimeOnly
  4. api

After switching a compile dependency to implementation, a downstream module fails to compile. What does that usually mean?

  1. Gradle is broken
  2. The downstream module used a library it never declared; declare it there (or use api if it's truly part of the upstream API)
  3. The version catalog is wrong
  4. You must use compileOnly