Generic Top

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


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 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 {

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

apply plugin: 'java'

dependencies {

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 plugin to our build.gradle file:

apply plugin: 'maven'

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 install task as well. So to convert, let's run the following:

gradle install

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

  • libs – containing the jar with the name ${artifactId}-${version}.jar
  • pomscontaining the converted POM file with the name pom-default.xml
  • tmp/jar – containing the manifest

The generated POM file will look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="" 

The install task also uploads the generated POM file and the JAR to the local Maven 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 install block:

install {
    repositories {
        mavenInstaller {
            pom.version = '0.0.1-maven-SNAPSHOT'
            pom.groupId = 'com.baeldung.sample'
            pom.artifactId = 'gradle-maven-converter'

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


4.2. Directory and Name of the POM

Sometimes, we might need the POM file to be copied to a different directory and with a different name. Therefore, let's add the following to the install block:


The mavenPomDir attribute is exposed by the plugin, which will point to build/poms. We can also give the absolute path of any directory we wish to copy the POM file to.

After running the install task, we can see the pom.xml inside build/poms/com.baeldung/gradle-to-maven.

4.3. Auto-generated Content

The Maven plugin also makes it straightforward to change any of the generated POM elements. For example, to make a dependency optional, we can add the below closure to pom.whenConfigured:

pom.whenConfigured { pom ->
    pom.dependencies.find {dep -> dep.groupId == 'junit' && dep.artifactId == 'junit' }.optional = true

This will produce the optional attribute added to the dependency:


4.4. Additional Information

Finally, if we want to add additional information, we can include any Maven supported element to the pom.project builder.

Let's add some license information:

pom.project {
    inceptionYear '2020'
    licenses {
        license {
            name 'My License'
            url ''
            distribution 'repo'

We can now see license information added to the POM:

      <name>My License</name>

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.

Generic bottom

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

Generic footer banner
Comments are closed on this article!