2012-11-12 54 views
6

我想从依赖jasperreports.jar 解压文件“default.jasperreports.properties”,并把它用新名称“jasperreports.properties”摇篮 - 从依赖罐子

样品gradle这个构建zip分发解压文件:

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像(错了,但人类可读)

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

PS:尝试调用

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

但它不适用于例外

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

回答

11

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

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' 
    } 
} 
+0

谢谢,你帮了我很多。 – popalka

+0

关于使用 传递=假 它不是托管在googlecode.com 我的自定义Maven仓库工作痘痘注意到我的实际脚本是 http://oracle-ddl2svn.googlecode.com/svn/branches/2。 ?X-gradle这个/的build.gradle R = 202 当我添加 依赖性{ \t分配 'com.googlecode:scheme2ddl:2.0'{ \t及物=假 \t} } 我得到错误 >无法查找方法com.googlecode:scheme2ddl:2.0()参数[build_2 tnud3244b5lndbqkchi9t13at $ _run_closure3_closure6 @ e38fca]在根项目'oracle-ddl2svn-gradle'上。 – popalka

+0

你的语法是错误的 - 与我的相比。 –

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