使用Gradle从现有的pom.xml文件中读取信息?

时间:2012-07-19 09:48:21

标签: ant gradle

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

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

Gradle中是否存在“本机”支持,用于从现有物理pom.xml文件访问pom元素,还是需要在我的.gradle文件中执行上述Ant方法才能使其正常工作?

此页:

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

有关于生成pom文件的信息,但这不是我想要的。我试图创建一个同样的.gradle文件:

    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文档中找到有关此任务的相应ant调用的任何信息。我看过了:

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

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

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

似乎既不是财产也不是参考。我偶然发现了这个页面:

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

使用 NamespaceBuilder

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

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

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
经过一番谷歌搜索后,我发现:

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

似乎相关,但属性的值仍为null。

4 个答案:

答案 0 :(得分:3)

Gradle不提供解析POM文件的本机支持,但Groovy的XmlSlurper使XML解析变得简单方便。我可能更喜欢Ant方法。

答案 1 :(得分:3)

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

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
}

我认为,通过groovy xmlslurper阅读pom文件更为直接。

答案 2 :(得分:0)

你试过吗

http://www.gradle.org/docs/1.6/userguide/build_setup_plugin.html

这基本上将您的maven项目转换为gradle。

答案 3 :(得分:0)

请让我知道以下pom.xml内容的build.gradle文件的条目:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <additionalClasspathElements>
                        <additionalClasspathElement>resources</additionalClasspathElement>
                    </additionalClasspathElements>
                    <forkCount>5</forkCount>
                    <reuseForks>true</reuseForks>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                    <runOrder>alphabetical</runOrder>
                    <argLine>-Duser.language=en</argLine>
                    <argLine>-Xmx512m</argLine>
                    <argLine>-XX:MaxPermSize=256m</argLine>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <systemPropertyVariables>
                        <!--<cucumber.options>&#45;&#45;tags @example</cucumber.options>-->
                    </systemPropertyVariables>
                </configuration>
                <executions>
                    <execution>
                        <id>failsafe-integration-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.github.temyers</groupId>
                <artifactId>cucumber-jvm-parallel-plugin</artifactId>
                <version>4.2.0</version>
                <executions>
                    <execution>
                        <id>generateRunners</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>generateRunners</goal>
                        </goals>
                        <configuration>
                            <!-- Mandatory -->
                            <glue>com.cucumber.stepdefinitions</glue>
                            <strict>true</strict>
                            <monochrome>true</monochrome>
                             <!-- These are the default values -->
                            <outputDirectory>${project.build.directory}/generated-test-sources/cucumber</outputDirectory>
                            <featuresDirectory>src/test/resources/features/</featuresDirectory>
                            <cucumberOutputDir>target/cucumber-reports</cucumberOutputDir>
                            <format>json</format>
                             <tags>${TestType}</tags>
                            <tags>~@ignore</tags>
                            <customVmTemplate>
                                src/main/resources/cucumber-custom-runner.vm
                            </customVmTemplate>
                          <!--  <filterFeaturesByTags>true</filterFeaturesByTags>-->
                            <namingScheme>pattern</namingScheme>
                            <namingPattern>{f}_{c}IT</namingPattern>
                            <plugins>
                                <plugin>
                                    <name>com.cucumber.listener.ExtentCucumberFormatter</name>
                                    <extension>html</extension>
                                    <outputDirectory>output/</outputDirectory>

                                </plugin>
                            </plugins>
                            <parallelScheme>SCENARIO</parallelScheme>
                        </configuration>
                    </execution>
                </executions>
            </plugin>