2012-07-19 111 views
7

在蚂蚁的Maven Ant Tasks可以用这样的读取性能行家:使用Gradle从现有的pom.xml文件中读取信息?

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

是否有摇篮“原生”支持从现有物理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文档中找到有关此任务的相应蚂蚁调用的任何信息。我看过:

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

这似乎是相关的,但属性的值仍然为空。

+0

为什么你期望在Gradle文档中记录这个?你必须查看'maven-ant-tasks'的文档。 – 2012-07-19 13:55:00

+0

我已经阅读了maven-ant-tasks的文档,它只是说可以通过定义来读取pom信息,然后可以像那样使用版本是$ {mypom.version}。但是,如何在设置类路径包含maven-ant-tasks之后,如何读取gradle中的标记? – u123 2012-07-19 18:06:48

+0

'artifact:pom'是一个Ant任务。请参阅Gradle用户指南中的[从Gradle使用Ant](http://gradle.org/docs/current/userguide/userguide_single.html#ant)一章,了解如何使用Ant任务。运行任务后,您可以获取'mypom'引用。不完全确定如何从那里继续。使用'XmlSlurper'会简单得多。 – 2012-07-20 04:17:57

回答

3

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

+0

我想尝试一下来自Gradle的ant。在设置maven-ant-task.jar之后,Gradle如何将pom文件映射到id中? – u123 2012-07-19 13:33:31

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文件是更直接的方式,我认为。

0

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

  <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>[email protected]</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> 
+0

请分享更多详情。谢谢! – Saadi 2017-11-13 10:48:26