Gradle build.gradle to Maven pom.xml

MavenGradlepom.xml

Maven Problem Overview


I have a Gradle project and I need all its dependencies to be transferred and used with another Maven project. In other words how can I generate (or can I generate) the pom.xml from the build.gradle?

Maven Solutions


Solution 1 - Maven

Since Gradle 7, when using Gradle's Maven-Publish plugin, publishToMavenLocal and publish are automatically added to your tasks, and calling either will always generate a POM file.

So if your build.gradle file looks like this:

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

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    runtimeOnly group: 'ch.qos.logback', name:'logback-classic', version:'1.2.3'
    testImplementation group: 'junit', name: 'junit', version: '4.12'
}

// the GAV of the generated POM can be set here
publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'edu.bbte.gradleex.mavenplugin'
            artifactId = 'gradleex-mavenplugin'
            version = '1.0.0-SNAPSHOT'

            from components.java
        }
    }
}

you can call gradle publishToLocalRepo in its folder, you will find in the build/publications/maven subfolder, a file called pom-default.xml. Also, the built JAR together with the POM will be in your Maven local repo. More exactly the gradle generatePomFileForMavenPublication task does the actual generation, if you want to omit publication to your Maven local repo.

Please note that not all dependencies show up here, since the Gradle "configurations" don't always map one-to-one with Maven "scopes".

Solution 2 - Maven

As I didn't want to install anything in my local repo, I did following, instead, after reading docs. Add in your build.gradle

apply plugin: 'maven'

group = 'com.company.root'
// artifactId is taken by default, from folder name
version = '0.0.1-SNAPSHOT'

task writeNewPom << {
    pom {
        project {
            inceptionYear '2014'
            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
        }
    }.writeTo("pom.xml")
}

to run it gradle writeNewPom

@a_horse_with_no_name

gradle being made with groovy can try to add after ending } project block

build{
  plugins{
    plugin{
      groupId 'org.apache.maven.plugins'
      artifactId 'maven-compiler-plugin'
      configuration{
          source '1.8'
          target '1.8'
      }
    }
  }
}

didn't try, wild guess !

Solution 3 - Maven

When you have no gradle installed the "write gradle task to do this" is not very userful. Instead of installing this 100MB beast with dependecies I made the filter converting gradle dependencies to maven dependencies:

cat build.gradle\
| awk '{$1=$1};1'\
| grep -i "compile "\
| sed -e "s/^compile //Ig" -e "s/^testCompile //Ig"\
| sed -e "s/\/\/.*//g"\
| sed -e "s/files(.*//g"\
| grep -v ^$\
| tr -d "'"\
| sed -e "s/\([-_[:alnum:]\.]*\):\([-_[:alnum:]\.]*\):\([-+_[:alnum:]\.]*\)/<dependency>\n\t<groupId>\1<\/groupId>\n\t<artifactId>\2<\/artifactId>\n\t<version>\3<\/version>\n<\/dependency>/g"

This converts

compile 'org.slf4j:slf4j-api:1.7.+'
compile 'ch.qos.logback:logback-classic:1.1.+'
compile 'commons-cli:commons-cli:1.3'

into

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.+</version>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-classic</artifactId>
	<version>1.1.+</version>
</dependency>
<dependency>
	<groupId>commons-cli</groupId>
	<artifactId>commons-cli</artifactId>
	<version>1.3</version>
</dependency>

The rest of pom.xml should be created by hand.

Solution 4 - Maven

The most built in solution would likely be to use the archiveTask task in the Maven Plugin which will generate a pom in the poms folder in your build dir. http://www.gradle.org/docs/current/userguide/maven_plugin.html#sec:maven_pom_generation

Solution 5 - Maven

Add this to your build.gradle file. I pasted it to the end of mine:

apply plugin: 'maven'
apply plugin: 'java'

task writeNewPom doLast {
    pom {
        project {
            groupId 'org.example'
            artifactId 'test'
            version '1.0.0'
        
            inceptionYear '2008'
            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
        }
    }.writeTo("$buildDir/newpom.xml")
}

Then run the following depending on which you have

$ gradle writeNewPom

or

$ /.gradlew writeNewPom

The file is generated and put in $buildDir/newpom.xml Snagged from this gist

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhtm01View Question on Stackoverflow
Solution 1 - Mavencsaba.sulyokView Answer on Stackoverflow
Solution 2 - MavendilbertsideView Answer on Stackoverflow
Solution 3 - MavenandrejView Answer on Stackoverflow
Solution 4 - MavenMatt WhippleView Answer on Stackoverflow
Solution 5 - MavenBrad ParksView Answer on Stackoverflow