11

我想从依赖的 jasperreports.jar 中提取文件“default.jasperreports.properties”并将其放入新名称“jasperreports.properties”的 zip 分发中

示例 gradle 构建:

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

但想使用 somethink like(错误,但人类可读)

FileTree tree = configurations.runtime.filter("jasperreports").singleFile.zipTree

PS:试着打电话

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

但它不适用于异常

您不能更改未处于未解决状态的配置!

4

2 回答 2

14

这是一个可能的解决方案(有时代码会说一千多个单词):

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'
    }
}
于 2012-11-12T16:05:38.040 回答
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
    }
}
于 2017-08-06T15:34:45.553 回答