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.

As always, the source code from this article can be found over on GitHub.