首页 文章

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

提问于
浏览
6

我想从依赖的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'
}

如何从依赖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 回答

  • 1

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

    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'
        }
    }
    
  • 11

    替代方案:

    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
        }
    }
    

相关问题