Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make gradle generate a valid pom.xml file at the root of a project for maven users?

While only for two days now, I am definitely sold on using gradle for all of my Java projects, and drop pom.xml from the root of all my projects.

However, I would like to remain maven-compatible, in the sense that I would like for a gradle task to be able to generate a suitable pom.xml at the root of the project should the user want it.

At this moment, the only reference to a pom.xml I have is in this section of the build.gradle file (this is, with very few modifications, what is found here):

uploadArchives {     repositories {         mavenDeployer {             beforeDeployment {                 MavenDeployment deployment -> signing.signPom(deployment);             }              repository(url: sonatypeRepoURI) {                 authentication(userName: sonatypeUsername,                     password: sonatypePassword);             }              pom.project {                 name "${name}";                 packaging "bundle";                 description "${description}";                 url "${projectURL}";                  scm {                     url "${gitroscm}";                     connection "${gitroscm}";                     developerConnection "${gitrwscm}";                 }                  licenses {                     license {                         name "Lesser General Public License, version 3 or greater";                         url "http://www.gnu.org/licenses/lgpl.html";                         distribution "repo";                     }                 }                  developers {                     developer {                         id "whocares";                         name "whocares";                         email "whocares";                     }                 }             }         }     } } 

How would I extract the pom.project out of this very deeply nested construct into a task which could generate a pom.xml (by default, the generated pom.xml is in build/poms/pom-default.xml and looks quite good)?

More importantly, is it possible to extract that pom.project out of uploadArchives while still being able to refer to it?

Full link to the build.gradle file: here.

like image 273
fge Avatar asked Jun 24 '13 17:06

fge


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.

Does Gradle project has POM XML?

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.

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.


2 Answers

You can use the gradle maven plugin. This adds the pom convention method to your project, which you can use in a task to generate a pom.xml file, like

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("pom.xml")     } } 

Then you call gradle createPom to generate the pom.xml in the project root. Of all the things in the pom definition, you should really provide groupId, artifactId and version, other thins like licenses are not that important.

You can also look at this example for a project definition with some dependencies, and try running it to see what it produces.

Some of the new keywords were added and some techniques were deprecated. Please check

like image 150
jmruc Avatar answered Sep 22 '22 04:09

jmruc


Here is my build.gradle

apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'maven'  repositories {  mavenLocal()  mavenCentral() }  dependencies {      compile 'org.springframework:spring-core:4.0.5.RELEASE'     compile 'org.springframework:spring-webmvc:4.0.5.RELEASE'      compile 'org.slf4j:slf4j-api:1.7.5'     runtime 'org.slf4j:slf4j-log4j12:1.7.5'      testCompile 'org.springframework:spring-test:4.0.5.RELEASE'      testCompile 'junit:junit:4.11'     testCompile "org.mockito:mockito-core:1.9.5"     testCompile "org.hamcrest:hamcrest-library:1.3"      testCompile 'javax.servlet:javax.servlet-api:3.0.1' } test {   testLogging {     // Show that tests are run in the command-line output     events 'started', 'passed'   } } task wrapper(type: Wrapper) { gradleVersion = '1.12' } task createPom  {     pom {         project {             groupId 'sg.test.spring.web.guide'             artifactId 'sg-web-initial'             version '1.0.0-SNAPSHOT'              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("pom.xml") } 

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. Although you can replace writeTo("pom.xml") with writeTo("<anyDir>/newpom.xml").

The resulting pom.xml:

<?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>sg.test.spring.web.guide</groupId>     <artifactId>sg-web-initial</artifactId>     <version>1.0.0-SNAPSHOT</version>     <inceptionYear>2008</inceptionYear>     <licenses>         <license>             <name>The Apache Software License, Version 2.0</name>             <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>             <distribution>repo</distribution>         </license>     </licenses>     <dependencies>         <dependency>             <groupId>javax.servlet</groupId>             <artifactId>javax.servlet-api</artifactId>             <version>3.0.1</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-core</artifactId>             <version>4.0.5.RELEASE</version>             <scope>compile</scope>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-webmvc</artifactId>             <version>4.0.5.RELEASE</version>             <scope>compile</scope>         </dependency>         <dependency>             <groupId>org.slf4j</groupId>             <artifactId>slf4j-log4j12</artifactId>             <version>1.7.5</version>             <scope>runtime</scope>         </dependency>         <dependency>             <groupId>org.mockito</groupId>             <artifactId>mockito-core</artifactId>             <version>1.9.5</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.slf4j</groupId>             <artifactId>slf4j-api</artifactId>             <version>1.7.5</version>             <scope>compile</scope>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-test</artifactId>             <version>4.0.5.RELEASE</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.hamcrest</groupId>             <artifactId>hamcrest-library</artifactId>             <version>1.3</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>junit</groupId>             <artifactId>junit</artifactId>             <version>4.11</version>             <scope>test</scope>         </dependency>     </dependencies> </project> 
like image 23
sanjeev Avatar answered Sep 19 '22 04:09

sanjeev