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

pop*_*lka 9 zip extract distribution gradle

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

示例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'
}
Run Code Online (Sandbox Code Playgroud)

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

在我上面的脚本中使用

FileTree tree = zipTree('someFile.zip')
Run Code Online (Sandbox Code Playgroud)

但是想要使用一些想法(错误,但人类可读)

FileTree tree = configurations.runtime.filter("jasperreports").singleFile.zipTree
Run Code Online (Sandbox Code Playgroud)

PS:试着打电话

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

但它不能用于例外

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

Pet*_*ser 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'
    }
}
Run Code Online (Sandbox Code Playgroud)