2012-07-19 1 views
7

は次のようにMavenのプロパティを読み取るために使用することができます。Gradleを使用して既存のpom.xmlファイルから情報を読み取りますか? Antの<a href="http://maven.apache.org/ant-tasks/examples/pom.html">Maven Ant Tasks</a>で

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

は、既存の物理的なpom.xmlファイルからポンポンの要素にアクセスするためのGradleで「ネイティブ」サポートがありますか、私が通過する必要があります上記のAntのアプローチは、私の.gradleファイルでこの作業をするためには?

このページ:

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

がポンポンファイルを生成する上での情報を持っていますが、私は探していますものではありませんthatsの。私は同じことを.gradleファイルを作成しようとしました:私はbuild.gradleファイルの隣に位置するシンプルなpom.xmlファイルを持っている

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 

    } 

。しかし、私は、このタスクの対応するant呼び出しでgradle docsの情報を見つけることができません。 Antプロパティと参照を読む方法については

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

が、この:私は見てきました

<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です。

+0

なぜこれがGradleドキュメントに記載されると思われますか? 'maven-ant-tasks'については、ドキュメントを参照する必要があります。 –

+0

私は、単にを定義することによってpom情報を読むことができると言っているmaven-ant-tasksのドキュメントを読んでいます。のようなバージョン$ {mypom.version}です。しかし、クラスパスにmaven-ant-tasksを含めるように設定した後、どのようにしてgradleのタグを読むのですか? – u123

+0

'artifact:pom'はAntタスクです。 Antタスクの操作方法については、Gradleユーザーガイドの[GradleからAntを使用する](http://gradle.org/docs/current/userguide/userguide_single.html#ant)の章を参照してください。タスクを実行した後、 'mypom'リファレンスを取得できます。そこから進んでいく方法を正確には分からない。 'XmlSlurper'を使用する方がはるかに簡単です。 –

答えて

3

GradleはPOMファイルの解析をネイティブサポートしていませんが、GroovyのXmlSlurperはXML解析を簡単で便利にします。私はおそらく、Antのアプローチよりも好むでしょう。

+0

私はそれをGradleのアリで試してみたいと思います。 Maven-ant-task.jarを設定した後、Gradleからpomファイルをidにマップするにはどうすればいいですか? – u123

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

関連する問題