Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll learn how to convert a Maven build to a Gradle build. For this purpose, we will use the gradle init command on an existing Maven project.

2. Gradle Setup

Let’s install Gradle on our machine by downloading a Gradle distribution and following the instructions. We can also take a deep dive to learn more about Gradle.

3. Maven Build File

Let’s start with a standard Maven Java project which the following pom.xml file in its root directory:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.baeldung</groupId>
    <artifactId>maven-to-gradle</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
    </dependencies><br /><br />    <properties><br />        <maven.compiler.source>17</maven.compiler.source><br />        <maven.compiler.target>17</maven.compiler.target><br />    </properties>
</project>

4. Converting to Gradle

Let’s go to the root directory of this Maven project, containing the master pom.xml, and execute the gradle init command. When prompted for a response, let’s type yes and press enter. We should see the following output:

$ ./gradlew init
This will be executed during the initialization phase.

Found a Maven build. Generate a Gradle build from this? (default: yes) [yes, no] yes

Select build script DSL:
  1: Kotlin
  2: Groovy
Enter selection (default: Kotlin) [1..2] 2

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] yes

> Task :init
Maven to Gradle conversion is an incubating feature.
For more information, please refer to https://docs.gradle.org/8.2-rc-2/userguide/migrating_from_maven.html in the Gradle documentation.

5. Generated Gradle Build Files

Now let’s recheck the contents of our project’s root directory. We should now see several new files in our root directory. If we want to go deeper, we can look at gradle-build-settings-properties.

5.1. build.gradle

The build.gradle file is the core component of our Gradle build process and is the direct equivalent of the pom.xml file for Maven builds. Here, pom.xml attributes like groupId, version, and dependencies are translated to their Gradle equivalents. Also present is the attribute of sourceCompatibility, which tells us which Java version to use when compiling Java sources. In the plugins section, we have ‘java-library‘, which supports building any type of Java project, and ‘maven-publish’, which supports publishing artifacts to Maven-compatible repositories.

/*
 * This file was generated by the Gradle 'init' task.<br /> * This project uses @Incubating APIs which are subject to change.
 */

plugins {
    id 'java-library'
    id 'maven-publish'
}

repositories {
    mavenLocal()
    maven {
        url = uri('https://repo.maven.apache.org/maven2/')
    }
}

dependencies {
    api 'org.apache.commons:commons-lang3:3.12.0'
}

group = 'com.baeldung'
version = '0.0.1-SNAPSHOT'
description = 'maven-to-gradle'
java.sourceCompatibility = JavaVersion.VERSION_1_8

publishing {
    publications {
        maven(MavenPublication) {
            from(components.java)
        }
    }
}

5.2. settings.gradle

The settings.gradle file is used by Gradle during the initialization phase to identify which projects are included in the build.

/*
 * This file was generated by the Gradle 'init' task.<br /> * This project uses @Incubating APIs which are subject to change.
 */

rootProject.name = 'maven-to-gradle'

5.3. gradlew and gradlew.bat

Two startup scripts, one for Windows and one for Unix, are also generated by Gradle. These scripts can be used to run the project on a machine that does not have a prior Gradle setup. We can learn more about the Gradle wrapper files by looking at gradle-wrapper.

6. Conclusion

In this article, we learned how a Maven build containing pom.xml can be converted to a Gradle build having multiple build files such as build.gradle, settings.gradle, gradlew, and gradlew.bat. The source code from this article can be found on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Maven (eBook) (cat=Maven)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.