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

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

问题描述

在 Ant 中,Maven Ant Tasks 可用于读取 maven 属性,如下所示:

<echo>版本是${mypom.version}</echo>

Gradle 是否有本机"支持从现有的物理 pom.xml 文件访问 pom 元素,或者我是否需要在我的 .gradle 文件中通过上述 Ant 方法来完成这项工作?

本页:

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

有关于生成 pom 文件的信息,但这不是我要找的.我试图创建一个 .gradle 文件,它的作用相同:

 存储库 {MavenCentral()}配置{mavenAntTasks}依赖{mavenAntTasks 'org.apache.maven:maven-ant-tasks:2.1.1'}任务你好<<{ant.taskdef(资源: 'org/apache/maven/artifact/ant/antlib.xml',uri: 'antlib:org.apache.maven.artifact.ant',类路径:configuration.mavenAntTasks.asPath)//这是什么 gradle 语法://<artifact:pom id="mypom" file="maven-project/pom.xml"/>//它不是属性或任务...def artifact = groovy.xml.NamespaceBuilder.newInstance(ant,'antlib:org.apache.maven.artifact.ant')artifact.pom(id:'mypom',文件:'pom.xml')def text = properties['mypom.version']println "来自 pom 文件:" + 文本}

我在 build.gradle 文件旁边有一个简单的 pom.xml 文件.但是我在 gradle 文档中找不到有关此任务的相应 ant 调用的任何信息.我看过:

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

关于如何读取 ant 属性和引用,但是这个:

似乎既不是属性也不是引用.我偶然发现了这个页面:

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

使用 NamespaceBuilder 的地方:

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

但是当使用这种方法时,我得到:

AbstractTask.getDynamicObjectHelper() 方法已被弃用,并将在下一版本的 Gradle 中删除.请改用 getAsDynamicObject() 方法.来自 pom 文件:空

经过一番谷歌搜索后,我发现:

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

看似相关,但该属性的值仍然为空.

解决方案

以下代码片段应该可以解决.

defaultTasks '你好'存储库{MavenCentral()}配置{mavenAntTasks}依赖{mavenAntTasks 'org.apache.maven:maven-ant-tasks:2.1.3'}任务你好<<{ant.taskdef(资源:'org/apache/maven/artifact/ant/antlib.xml',uri: 'antlib:org.apache.maven.artifact.ant',类路径:configuration.mavenAntTasks.asPath)ant.'antlib:org.apache.maven.artifact.ant:pom'(id:'mypom', file:'pom.xml')println ant.references['mypom'].version}

我认为通过 groovy xmlslurper 读取 pom 文件更直接.

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.

解决方案

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.

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

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