Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle build.gradle to Maven pom.xml

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?

like image 371
htm01 Avatar asked Oct 15 '12 02:10

htm01


People also ask

How create POM XML from Gradle?

You can name the task createPom to anyTaskName as you like. Then just run gradle clean or grale build or simply gradle createPom . This will generate it as pom. xml in the root of the project.

Can I convert a Gradle project to Maven?

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

Do we have pom XML in Gradle?

It uses a declarative XML file for its POM file and has a host of plugins that you can use. Gradle uses the directory structure you see on Maven, but this can be customized.

How do I change a project from Gradle to Maven in Intellij?

If you have a Maven project, which you want to convert, you need to go into the project folder, and on the cmd line do "gradle init" (obviously make sure Gradle is on your path). Close and reopen the project, and you will be prompted to enable Gradle on the project.


2 Answers

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".

like image 172
csaba.sulyok Avatar answered Sep 19 '22 23:09

csaba.sulyok


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 !

like image 43
dilbertside Avatar answered Sep 19 '22 23:09

dilbertside