Gradle - extract a file from a hanging jar

I want to extract the file "default.jasperreports.properties" from the dependent jasperreports.jar and put it in the mail distribution with the new name "jasperreports.properties"

Gradle build example:

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

How to get FileTree in extractFileFromJar () from jasperreports-2.0.5.jar dependency?

In the script above I use

 FileTree tree = zipTree('someFile.zip') 

but want to use something like (wrong, but human readable)

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

PS: try to call

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

but it does not work with an exception

You cannot change a configuration that is not in an unauthorized state!

+6
source share
2 answers

Here is a possible solution (sometimes the code says more than a thousand words):

 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
source

Alternative solution:

 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 } } 
+1
source

All Articles