Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading info from existing pom.xml file using Gradle?

Tags:

gradle

ant

In Ant the Maven Ant Tasks can be used to read maven properties like this:

<artifact:pom id="mypom" file="pom.xml" />
<echo>The version is ${mypom.version}</echo>

Is there "native" support in Gradle for accessing pom elements from an existing physical pom.xml file or do I need to go through the above Ant approach in my .gradle file to make this work?

This page:

http://gradle.org/docs/current/userguide/maven_plugin.html

has info on generating pom files but thats not what I am looking for. I have tried to create a .gradle file that does the same:

    repositories {
      mavenCentral()
    }

    configurations {
        mavenAntTasks
    }

    dependencies {
        mavenAntTasks 'org.apache.maven:maven-ant-tasks:2.1.1'
    }

    task hello << {
      ant.taskdef(resource: 'org/apache/maven/artifact/ant/antlib.xml',
                  uri: 'antlib:org.apache.maven.artifact.ant',
                  classpath: configurations.mavenAntTasks.asPath)

     // what is the gradle syntax for this:
     // <artifact:pom id="mypom" file="maven-project/pom.xml" />
     // its not a property or a task...
     def artifact = groovy.xml.NamespaceBuilder.newInstance(ant,'antlib:org.apache.maven.artifact.ant')
     artifact.pom(id:'mypom', file: 'pom.xml')
     def text = properties['mypom.version']
     println "From pom file: " + text 

    }

where I have a simple pom.xml file located next to the build.gradle file. But I can't find any info in the gradle docs on the corresponding ant calls for this task. I have looked at:

http://www.gradle.org/docs/current/userguide/ant.html

for how to read ant properties and references but this:

<artifact:pom id="mypom" file="maven-project/pom.xml" />

seems to be neither a property or reference. I stumbled on this page:

http://snipplr.com/view/4082/

where a NamespaceBuilder is used:

 def mvn = groovy.xml.NamespaceBuilder.newInstance(ant, 'antlib:org.apache.maven.artifact.ant')

but when using this approach I get:

The AbstractTask.getDynamicObjectHelper() method has been deprecated and will be removed in the next version of Gradle. Please use the getAsDynamicObject() method instead.
From pom file: null

after a bit of googling I found:

http://issues.gradle.org/browse/GRADLE-2385

which seems related, but the value of the property is still null.

like image 264
u123 Avatar asked Jul 19 '12 09:07

u123


People also ask

How do you read POM xml?

POM is an acronym for Project Object Model. The pom. xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals etc. Maven reads the pom.

Is POM xml used by 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.

Where is POM xml in Gradle?

xml (by default, the generated pom. xml is in build/poms/pom-default.

Does Gradle read settings xml?

Gradle will read this value from the settings. xml so you can continue to store it there. Alternatively you can specify it via the maven. repo.


2 Answers

Gradle doesn't provide native support for parsing POM files, but Groovy's XmlSlurper makes XML parsing easy and convenient. I'd probably prefer that over the Ant approach.

like image 138
Peter Niederwieser Avatar answered Sep 28 '22 04:09

Peter Niederwieser


The following code snip should work out.

defaultTasks 'hello'

repositories {
  mavenCentral()
}
configurations {
  mavenAntTasks
}
dependencies {
  mavenAntTasks 'org.apache.maven:maven-ant-tasks:2.1.3'
}

task hello << {
  ant.taskdef(
    resource: 'org/apache/maven/artifact/ant/antlib.xml',
    uri: 'antlib:org.apache.maven.artifact.ant',
    classpath: configurations.mavenAntTasks.asPath)

  ant.'antlib:org.apache.maven.artifact.ant:pom'(id:'mypom', file:'pom.xml')
  println ant.references['mypom'].version
}

Reading pom file by groovy xmlslurper is more forthright way, I think.

like image 40
sca. Avatar answered Sep 28 '22 04:09

sca.