Gradle - 从依赖的jar中提取文件

时间:2012-11-12 06:28:44

标签: zip extract distribution gradle

我想从依赖的jasperreports.jar中提取文件“default.jasperreports.properties” 并使用新名称“jasperreports.properties”

将其放入zip发行版中

Gradle build示例:

apply plugin: 'java'

task zip(type: Zip) {
    from 'src/dist'
  //  from configurations.runtime
    from extractFileFromJar("default.jasperreports.properties");
    rename 'default.jasperreports.properties', 'jasperreports.properties'

}

def extractFileFromJar(String fileName) {
    //    configurations.runtime.files.each { file -> println file} //it's not work 
    // not finished part of build file
    FileTree tree = zipTree('someFile.zip')
    FileTree filtered = tree.matching {
        include fileName
    }

}

repositories {
    mavenCentral()
}

dependencies {
    runtime 'jasperreports:jasperreports:2.0.5'
}

如何从依赖关系jasperreports-2.0.5.jar中获取extractFileFromJar()中的FileTree?

在上面的脚本中我使用

FileTree tree = zipTree('someFile.zip')

但想要使用像(错误的,但人类可读的)

之类的想法
FileTree tree = configurations.runtime.filter("jasperreports").singleFile.zipTree

PS:尝试致电

def extractFileFromJar(String fileName) {
    configurations.runtime.files.each { file -> println file} //it's not work 
...

但它不适用于例外

  

您无法更改未处于未解决状态的配置!

2 个答案:

答案 0 :(得分:13)

这是一个可能的解决方案(有时代码超过千言万语):

apply plugin: "java"

repositories {
    mavenCentral()
}

configurations {
    jasper
}

dependencies {
    jasper('jasperreports:jasperreports:2.0.5') { 
        transitive = false
    }
}

task zip(type: Zip) {
    from 'src/dist'
    // note that zipTree call is wrapped in closure so that configuration
    // is only resolved at execution time
    from({ zipTree(configurations.jasper.singleFile) }) {
        include 'default.jasperreports.properties'
        rename 'default.jasperreports.properties', 'jasperreports.properties'
    }
}

答案 1 :(得分:1)

替代解决方案:

configurations {
    batch
}

dependencies {
    batch 'org.springframework.batch:spring-batch-core:3.0.8.RELEASE' { 
        transitive = false
    }
}

def extractBatchSql(path) {
    def zipFile = configurations.batch.find { it =~ /spring-batch-core/ }
    def zip = new java.util.zip.ZipFile(zipFile)
    def entry = zip.getEntry(path)
    return zip.getInputStream(entry).text
}

task tmp() {
    dependsOn configurations.batch

    doLast {
        def delSql = extractBatchSql("org/springframework/batch/core/schema-drop-oracle10g.sql")
        println delSql
    }
}