Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE 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 {
    mavenCentral()
}

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

apply plugin: 'java'

dependencies {
    compile('org.slf4j:slf4j-api')
    testCompile('junit:junit')
}

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="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-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

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:

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

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:

pom.writeTo("${mavenPomDir}/${project.group}/${project.name}/pom.xml")

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:

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
      <optional>true</optional>
</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 'http://www.mycompany.com/licenses/license.txt'
            distribution 'repo'
        }
    }
}

We can now see license information added to the POM:

<inceptionYear>2020</inceptionYear>
<licenses>
    <license>
      <name>My License</name>
      <url>http://www.mycompany.com/licenses/license.txt</url>
      <distribution>repo</distribution>
    </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.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!