通过groovy xmlSurper读取POM xml文件的具体内容并下载依赖性

时间:2015-06-02 21:42:23

标签: groovy xml-parsing

我有一个下面的POM文件。我想使用xmlSlurper groovy代码来读取POM文件的具体内容有人可以告诉我如何编写代码来通过groovy xmlSlurper读取特定内容。我想读取groupid工件范围在POM文件下面的运行时的工件ID,分类器和类型。

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ss.engine</groupId>
  <artifactId>nti</artifactId>
  <version>4.0.0.110</version>
  <description>Configuration Management Gradle Plugin</description>
  <dependencies>
    <dependency>
      <groupId>com.ss.engine</groupId>
      <artifactId>License</artifactId>
      <version>4.0.0.5000</version>
      <scope>compile</scope>
    </dependency>
      <dependency>
      <groupId>com.as.engine</groupId>
      <artifactId>commoncore</artifactId>
      <version>4.0.0.100</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.as.engine</groupId>
      <artifactId>ncdes</artifactId>
      <version>4.0.0.97</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.1.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.as.enginee</groupId>
      <artifactId>kgfhs</artifactId>
      <version>4.0.0.137</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.operasolutions</groupId>
      <artifactId>Risk</artifactId>
      <version>1.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>properties-maven-plugin</artifactId>
      <version>1.0-alpha-2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>net.sf.supercsv</groupId>
      <artifactId>super-csv</artifactId>
      <version>2.1.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.jamonapi</groupId>
      <artifactId>jamon</artifactId>
      <version>2.4</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.1</version>
      <scope>compile</scope>
    </dependency>
     <dependency>
  <groupId>com.as.engine</groupId>
  <artifactId>les</artifactId>
  <version>0.0.3</version>
  <type>zip</type>
  <classifier>linux</classifier>
  <scope>runtime</scope>
</dependency>
 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-configuration</groupId>
      <artifactId>commons-configuration</artifactId>
      <version>1.10</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>args4j</groupId>
      <artifactId>args4j</artifactId>
      <version>2.0.28</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>net.sf.supercsv</groupId>
      <artifactId>super-csv-dozer</artifactId>
      <version>2.1.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

1 个答案:

答案 0 :(得分:1)

下面是我用来读取POM文件内容的脚本,并从神器中下载范围为运行时的依赖项。

apply plugin: 'java'

// ------ Tell the script to get dependencies from artifactory ------
    repositories {
         maven {
        url "http://abc.de.fg:80/artifactory/fes-release-local"
              }
        }

    buildscript {
    repositories {
      maven {
        url "http://abc.de.fg:80/artifactory/libs-snapshot"
         }
    }
    // ------ Tell the script to get dependencies from artifactory ------
    dependencies {
    classpath ([ "cop.trn.cm:cmugin:1.2.147" ])
  }
}

apply plugin: 'com.uven.eplugin'

/**
 * The folloing -D parameters are required to run this task
 *  - deployLayer = one of acceptance, latest, production, test
 */
//------------------------------------------------------------------------------------------
// Read the properties file and take the value as per the environment.
// 
//------------------------------------------------------------------------------------------
if(!System.properties.deployLayer) throw new Exception ("deployLayer must be set")
def thePropFile = file("config/${System.properties.deployLayer}.properties")
if(!thePropFile.exists()) throw new Exception("Cannot load the specified environment properties from ${thePropFile}")
println "Deploying ${System.properties.jobName}.${System.properties.buildNumber} to ${System.properties.deployLayer}"

// load the deploy properties from the file
def deployProperties = new Properties()
thePropFile.withInputStream { 
    stream -> deployProperties.load(stream) 
} 
// set them in the build environment
project.ext {
  deployProps = deployProperties
  deployRoot = deployProperties["${System.properties.jobName}.deployroot"]
  deploydir = deployProperties["${System.properties.jobName}.deploydir"]
  deployFolder = deployProperties["${System.properties.jobName}.foldername"]
  deployPostInstallSteps = deployProperties["${System.properties.jobName}.postInstallSteps"]
}

task deleteGraphicsAssets(type: Delete, dependsOn: deploy) {
    def dirName = "${deployRoot}"
    delete dirName

    doLast {
        file(dirName).mkdirs()
    }
}

task downloaddependecies(dependsOn: deleteGraphicsAssets) << {
  def pomFile = file("${buildDir}/artifacts").listFiles().find { it.name.endsWith 'pom' }
  def pomXml = new XmlSlurper().parse(pomFile.absolutePath)
  def pomDependencies = pomXml.dependencies.dependency
  Configuration configuration = project.configurations.detachedConfiguration()
  configuration.transitive = false
  pomDependencies.each { dependency ->
    if(dependency.scope == 'runtime') {
    def dependencySpec = dependencies.create("${dependency.groupId}:${dependency.artifactId}:${dependency.version}:${dependency.classifier}@${dependency.type}")
      println "Found Runtime Dependency: ${dependencySpec}"
      configuration.dependencies.add(dependencySpec)
                  }
                }
                println "RESOLVED THE FOLLOWING"
                configuration.resolvedConfiguration.firstLevelModuleDependencies.each { println it }
                copy {
                  from configuration
    into ( "${buildDir}/artifacts" )
                }
}


task copyartifactZip << {
    copy {
        from "${deployRoot}"
        into "${deploydir}/"
    }
}

task copyLicenseZip << {
    copy {
        from "${deployRoot}"
        into "${deploydir}/${deployFolder}"
    }
}

task myCustomTask(dependsOn: downloaddependecies) << {
    copy {
        from 'deploymentfiles'
        into "${deployRoot}"
    }
}
task unzipArtifact(dependsOn: myCustomTask) << {
  def theZip = file("${buildDir}/artifacts").listFiles().find { it.name.endsWith('.zip') }
  println "Unzipping ${theZip} the artifact to: ${deployRoot}"
  ant.unzip(src: theZip, dest: "${deployRoot}", overwrite: true)
}

task setPerms(type:Exec, dependsOn: unzipArtifact) {
  workingDir "${deployRoot}"
  executable "bash"
  args "-c", "chmod -fR 755 *"

  }
def dirName = "${deploydir}/${deployFolder}"
task zipDeployment(type: GradleBuild, dependsOn: setPerms) { GradleBuild gBuild ->
    def env = System.getenv()
    def jobName=env['jobName']
if (jobName.equals("LicenseGenerator")) {
    delete dirName
    file(dirName).mkdirs()
    gBuild.tasks = ['copyLicenseZip']
    } else {
   gBuild.tasks = ['copyartifactZip']
}
}

task deployAll(dependsOn: zipDeployment){}