Course – LS – All

Get started with Spring 5 and Spring Boot 2, 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 specified 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>
</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 in yes and press enter. We should see the following output:

$ gradle init

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


> Task :init
Maven to Gradle conversion is an incubating feature.
Get more help with your project: https://docs.gradle.org/6.1/userguide/migrating_from_maven.html

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed

5. Generated Gradle Build Files

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

5.1. build.gradle

The build.gradle file is the core component of our Gradle build process, and it is the direct equivalent of the pom.xml file for Maven builds. We can see here that 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‘ which provides support for building any type of Java project, and ‘maven-publish' which provides support for publishing artifacts to Maven-compatible repositories.

/*
 * This file was generated by the Gradle 'init' task.
 */

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

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

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

group = 'com.baeldung'
version = '0.0.1-SNAPSHOT'<br />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.
 */

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 taking a look 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 over on GitHub.

Course – LS – All

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

>> CHECK OUT THE COURSE
res – Maven (eBook) (cat=Maven)
Comments are closed on this article!