Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this tutorial, we’ll take a look at how to convert a Gradle build file to a Maven POM file. We’ll use Gradle version 7.2 for our example and we’ll also explore a few available customization options.

2. Gradle Build File

Let’s start with a standard Gradle Java project, gradle-to-maven, with the following build.gradle file:

repositories {
    mavenCentral()
}

group = 'com.baeldung'
version = '0.0.1'

apply plugin: 'java'

dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.25'
    testImplementation 'junit:junit:4.12'
}

3. Maven Plugin

Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories.

To use this, let’s add the Maven Publish plugin to our build.gradle file:

apply plugin: 'maven-publish'

The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.

The plugin automatically adds the publish task as well. So to convert, let’s add a basic definition for our publishing to the POM file:

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

    repositories {
        maven {
            name = 'sampleRepo'
            url = layout.buildDirectory.dir("repo")
        }
    }
}

Now we can publish our customLibrary to a local directory-based repository for demonstration purpose:

gradle publish

Running the above command creates a build directory with these sub-directories:

  • libs – containing the jar with the name ${artifactId}-${version}.jar
  • publications/customLibrary – containing the converted POM file with the name pom-default.xml
  • tmp/jar – containing the manifest
  • repo – the file system location used as the repository containing the published artifact

The generated POM file will look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.baeldung</groupId>
  <artifactId>gradle-to-maven</artifactId>
  <version>0.0.1</version>
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

Note that test scope dependencies are not included in the POM and the default runtime scope is assigned to all other dependencies.

The publish task also uploads this POM file and the JAR to the specified repository.

4. Customizing the Maven Plugin

In some cases, it may be useful to customize the project information in the generated POM file. Let’s take a look.

4.1. groupId, artifactId, and version

Changing the groupId, artifactId and the version of the POM can be handled in the publications/{publicationName} block:

publishing {
    publications {
        customLibrary(MavenPublishing) {
            groupId = 'com.baeldung.sample'
            artifactId = 'gradle-maven-converter'
            version = '0.0.1-maven'
        }
    }
}

Running the publish task now produces the POM file with the information provided above:

<groupId>com.baeldung.sample</groupId>
<artifactId>gradle-maven-converter</artifactId>
<version>0.0.1-maven</version>

4.2. Auto-generated Content

The Maven plugin also makes it straightforward to change any of the generated POM elements. For example, to change the default runtime scope to compile, we can add the below closure to the pom.withXml method:

pom.withXml {
    asNode()
      .dependencies
      .dependency
      .findAll { dependency ->
        // find all dependencies with runtime scope
        dependency.scope.text() == 'runtime'
      }
      .each { dependency ->
        // set the scope to 'compile'
        dependency.scope*.value = 'compile'
      }
}

This will change the scope for all the dependencies in the generated POM file:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.25</version>
  <scope>compile</scope>
</dependency>

4.3. Additional Information

Finally, if we want to add additional information, we can include such Maven-supported elements to the pom function.

Let’s add some license information:

...
pom {
    licenses {
        license {
            name = 'The Apache License, Version 2.0'
            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
        }
    }
}
...

We can now see license information added to the POM:

... 
<licenses>
  <license>
    <name>The Apache License, Version 2.0</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
  </license>
</licenses>
...

5. Conclusion

In this quick tutorial, we learned how to convert a Gradle build file to Maven POM.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW