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.
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.
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.
xml (by default, the generated pom. xml is in build/poms/pom-default.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With