2012-11-12 150 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 
    } 
}