阅读使用现有的摇篮pom.xml文件信息? [英] Reading info from existing pom.xml file using Gradle?

查看:110
本文介绍了阅读使用现有的摇篮pom.xml文件信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ant中 Maven的Ant任务可以用来读取性能的Maven这样的:

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>

是否有摇篮本地支持从现有物理pom.xml文件访问POM元素还是我需要经过上述方法蚂蚁在我的.gradle文件,使这项工作?

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?

本页面:

<一个href=\"http://gradle.org/docs/current/userguide/maven_plugin.html\">http://gradle.org/docs/current/userguide/maven_plugin.html

对生成POM文件的信息,但是那不是我所期待的。我试图创建一个.gradle文件,该文件确实是相同的:

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 

    }

在那里我有毗邻的build.gradle文件的简单pom.xml文件。但我不能找到此任务的相应来电蚂蚁在文档的gradle任何信息。我看过:

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:

<一个href=\"http://www.gradle.org/docs/current/userguide/ant.html\">http://www.gradle.org/docs/current/userguide/ant.html

如何读取ant属性和引用,但这:

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/

其中的 NamespaceBuilder 使用:

 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:

<一个href=\"http://issues.gradle.org/browse/GRADLE-2385\">http://issues.gradle.org/browse/GRADLE-2385

这似乎有关,但该属性的值仍然为空。

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

推荐答案

以下code剪断应该工作了。

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
}

通过常规的XmlSlurper

阅读POM文件更直接的方式,我想。

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

这篇关于阅读使用现有的摇篮pom.xml文件信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆