2012-07-19 31 views
7

Trong Ant các Maven Ant Tasks có thể được sử dụng để đọc các thuộc tính maven như thế này:Đọc thông tin từ tệp pom.xml hiện có bằng Gradle?

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

Có hỗ trợ "mẹ đẻ" trong Gradle để truy cập vào các yếu tố pom từ một file pom.xml vật lý hiện có hay tôi cần phải đi qua cách tiếp cận Ant ở trên trong tệp .gradle của tôi để thực hiện công việc này?

trang này:

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

có thông tin về việc tạo ra tập tin pom nhưng thats không phải những gì tôi đang tìm kiếm. Tôi đã cố gắng tạo một tệp .gradle làm như vậy:

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 

    } 

nơi tôi có tệp pom.xml đơn giản nằm bên cạnh tệp build.gradle. Nhưng tôi không thể tìm thấy bất kỳ thông tin trong tài liệu gradle trên các cuộc gọi kiến ​​tương ứng cho nhiệm vụ này. Tôi đã xem xét:

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

cho cách đọc thuộc tính kiến ​​và tài liệu tham khảo nhưng điều này:

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

có vẻ là không phải là một tài sản hoặc tài liệu tham khảo. Tôi vấp trên trang này:

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

nơi một NamespaceBuilder được sử dụng:

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

nhưng khi sử dụng phương pháp này tôi nhận được:

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 

sau một chút googling Tôi đã tìm thấy:

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

dường như có liên quan, nhưng giá trị của thuộc tính vẫn là rỗng.

+0

Tại sao bạn mong đợi điều này được ghi lại trong tài liệu Gradle ?! Bạn sẽ phải xem tài liệu cho 'maven-ant-tasks'. –

+0

Tôi đã đọc các tài liệu cho các nhiệm vụ maven-ant mà chỉ đơn giản nói rằng thông tin pom có ​​thể được đọc bằng cách xác định mà sau đó có thể được sử dụng như Phiên bản là $ {mypom.version}. Nhưng làm cách nào để đọc thẻ sau khi thiết lập classpath để bao gồm các maven-ant-tasks? – u123

+0

'tạo tác: pom' là tác vụ Ant. Xem phần [Sử dụng Ant từ Gradle] (http://gradle.org/docs/current/userguide/userguide_single.html#ant) trong hướng dẫn sử dụng Gradle để biết cách làm việc với các tác vụ Ant. Sau khi chạy tác vụ, bạn có thể lấy tham chiếu 'mypom'. Không chính xác chắc chắn làm thế nào để tiến hành từ đó. Sử dụng 'XmlSlurper' sẽ đơn giản hơn nhiều. –

Trả lời

3

Gradle không cung cấp hỗ trợ gốc để phân tích cú pháp tệp POM, nhưng việc phân tích cú pháp XML dễ dàng và thuận tiện của Groovy XmlSlurper. Tôi có lẽ sẽ thích rằng trên phương pháp tiếp cận Ant.

+0

Tôi muốn dùng thử với kiến ​​từ Gradle. Làm thế nào nó có thể từ Gradle để ánh xạ tệp pom vào id sau khi thiết lập maven-ant-task.jar? – u123

3

Đoạn mã sau đây sẽ hoạt động.

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 
} 

Đọc tệp pom bằng groovy xmlslurper là cách thẳng thắn hơn, tôi nghĩ vậy.

0

Vui lòng cho tôi biết mục nhập của tệp build.gradle cho phần dưới đây.nội dung 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

Vui lòng chia sẻ thêm chi tiết. Cảm ơn! – Saadi

Các vấn đề liên quan