Reading info from existing pom.xml file using Grad

2019-02-09 13:15发布

问题:

In Ant the Maven Ant Tasks can be used to read maven properties like this:

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

Is there "native" support in Gradle for accessing pom elements from an existing physical pom.xml file or do I need to go through the above Ant approach in my .gradle file to make this work?

This page:

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

has info on generating pom files but thats not what I am looking for. I have tried to create a .gradle file that does the same:

    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 

    }

where I have a simple pom.xml file located next to the build.gradle file. But I can't find any info in the gradle docs on the corresponding ant calls for this task. I have looked at:

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

for how to read ant properties and references but this:

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

seems to be neither a property or reference. I stumbled on this page:

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

where a NamespaceBuilder is used:

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

but when using this approach I get:

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

after a bit of googling I found:

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

which seems related, but the value of the property is still null.

回答1:

Gradle doesn't provide native support for parsing POM files, but Groovy's XmlSlurper makes XML parsing easy and convenient. I'd probably prefer that over the Ant approach.



回答2:

The following code snip should work out.

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
}

Reading pom file by groovy xmlslurper is more forthright way, I think.



回答3:

Have you tried

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

This basically converts your maven project to gradle.



回答4:

Please let me know the entry of the build.gradle file for the below pom.xml content:

            <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>


标签: ant gradle