I want to extract some jars from a war as part of my gradle (2.0) build. So far I have this:
task unzip(type: Copy) {
def zipFile = file('D:/external/dependent.war')
def outputDir = file('lib')
from zipTree(zipFile)
into outputDir
include 'WEB-INF/lib/*.jar'
}
This puts the WEB-INF/lib dir in the outputDir. I just want the jars flat.
To do it in Ant I would do this:
<target name="unzip">
<unzip src="D:/external/dependent.war" dest="lib">
<patternset>
<include name="WEB-INF/lib/*.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
</target>
How do I do it in gradle?
But there is no UnZip or UnTar to unpack an archive in Gradle. To unpack an archive we must use the Copy task and copy the contents of the archive to a specified destination directory. In Gradle we can use the zipTree() method to access the contents of an archive.
You can run gradle distZip to package the main distribution as a ZIP, or gradle distTar to create a TAR file. To build both types of archives just run gradle assembleDist . The files will be created at $buildDir/distributions/${project.name}-${project. version}.
Creating directories All core Gradle tasks ensure that any output directories they need are created if necessary using this mechanism. As described in the Apache Ant manual, the mkdir task will automatically create all necessary directories in the given path and will do nothing if the directory already exists.
If you are already using Gradle with your IntelliJ project, you can open it in Android Studio using the following steps: Click File > New > Import Project. Select your IntelliJ project directory, and click OK. Your project will open in Android Studio.
I tried the answer by Al J, but it didn't work with the include.
This worked for me:
task unzip(type: Copy) {
def zipFile = file('D:/external/dependent.war')
def outputDir = file('lib')
from zipTree(zipFile).matching{include 'WEB-INF/lib/*.jar'}.files
into outputDir
}
I believe you can simply append .files
to your zipTree
.
task unzip(type: Copy) {
def zipFile = file('D:/external/dependent.war')
def outputDir = file('lib')
from zipTree(zipFile).files
into outputDir
include 'WEB-INF/lib/*.jar'
}
It seems like there is a way with Gradle. It is more complicated though and not necessarily that obvious.
task unzip(type: Copy) {
def zipFile = file('D:/external/dependent.war')
def outputDir = file('lib')
from zipTree(zipFile)
into outputDir
include '**/*.jar'
includeEmptyDirs false
eachFile { FileCopyDetails fcp ->
// remap the file to the root
String[] segments = [fcp.getName()]
fcp.relativePath = new RelativePath(true, segments)
}
}
includeEmptyDirs false
makes sure that extra dirs aren't copied.
The eachFile
block flattens the structure (you have to put the name into an array to make it work).
It seems like this is a bug/missing feature in Gradle http://issues.gradle.org/browse/GRADLE-3025
For now I am having to delegate to good old reliable Ant to do the work!
task unzip(){
ant.unzip(src: 'D:/external/dependent.war', dest:'lib', overwrite:"true") {
patternset( ) {
include( name: 'WEB-INF/lib/*.jar' )
}
mapper(type:"flatten")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With