2012-07-19 108 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