通过groovy xmlSurper读取POM xml文件的特定内容并下载dependecy

通过groovy xmlSurper读取POM xml文件的特定内容并下载dependecy,groovy,xml-parsing,Groovy,Xml Parsing,我有一个下面的POM文件。我想使用xmlSlurper groovy代码来读取POM文件的特定内容。有人能告诉我如何编写代码来通过groovy xmlSlurper读取特定内容。我想在下面的POM文件中读取其作用域为运行时的groupid工件id、分类器和类型 <?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m

我有一个下面的POM文件。我想使用xmlSlurper groovy代码来读取POM文件的特定内容。有人能告诉我如何编写代码来通过groovy xmlSlurper读取特定内容。我想在下面的POM文件中读取其作用域为运行时的groupid工件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>

4.0.0
com.ss.engine
nti
4.0.0.110
配置管理Gradle插件
com.ss.engine
许可证
4.0.0.5000
编译
com.as.engine
共同核心
4.0.0.100
编译
com.as.engine
ncdes
4.0.0.97
编译
回写
回溯堆芯
1.1.1
编译
org.codehaus.jackson
杰克逊地图绘制者
1.9.5
编译
com.as.engee
kgfhs
4.0.0.137
编译
com.operasolutions
风险
1.1
编译
org.codehaus.mojo
属性maven插件
1.0-α-2
编译
net.sf.sv
超级csv
2.1.0
编译
com.jamonapi
火腿
2.4
编译
回写
回归经典
1.1.1
编译
com.as.engine
女同性恋
0.0.3
拉链
linux
运行时
朱尼特
朱尼特
4.11
测试
共用配置
共用配置
1.10
编译
args4j
args4j
2.0.28
编译
net.sf.sv
超级推土机
2.1.0
编译

下面是我用来读取POM文件内容并从artifactory下载范围为runtime的依赖项的脚本

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){}
apply插件:“java”
//----告诉脚本从artifactory获取依赖项------
存储库{
马文{
url“http://abc.de.fg:80/artifactory/fes-“本地发布”
}
}
构建脚本{
存储库{
马文{
url“http://abc.de.fg:80/artifactory/libs-快照“
}
}
//----告诉脚本从artifactory获取依赖项------
依赖关系{
类路径([“cop.trn.cm:cmugin:1.2.147”])
}
}
应用插件:“com.uven.eplugin”
/**
*运行此任务需要以下-D参数
*-部署层=验收、最新、生产、测试
*/
//------------------------------------------------------------------------------------------
//读取属性文件并根据环境获取值。
// 
//------------------------------------------------------------------------------------------
如果(!System.properties.deployLayer)引发新异常(“必须设置deployLayer”)
def thePropFile=file(“config/${System.properties.deployLayer}.properties”)
如果(!thePropFile.exists())引发新异常(“无法从${thePropFile}加载指定的环境属性”)
println“将${System.properties.jobName}.${System.properties.buildNumber}部署到${System.properties.deployLayer}”
//从文件中加载部署属性
def deployProperties=新属性()
PropFile.withInputStream{
流->部署属性.加载(流)
} 
//在生成环境中设置它们
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”]
}
任务deleteGraphicsAssets(类型:Delete,dependsOn:deploy){
def dirName=“${deployRoot}”
删除目录名
多拉斯特{
文件(dirName).mkdirs()
}
}
任务下载依赖项(依赖项:deleteGraphicsAssets)
if(dependency.scope==“runtime”){
def dependencySpec=dependencies.create(“${dependency.groupId}:${dependency.artifactId}:${dependency.version}:${dependency.classifier}@${dependency.type}”)
println“找到运行时依赖项:${dependencySpec}”
configuration.dependencies.add(dependencySpec)
}
}
println“解决了以下问题”
configuration.resolvedConfiguration.firstLevelModuleDependencies.each{println it}
抄袭{
从配置
进入(“${buildDir}/artifacts”)
}
}
任务copyartifactZip