Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle explode a zip into a buildDir

Tags:

gradle

My project has a remote dependency which is really just a zip of certain files, which needs to be unzipped somewhere, so that the build can generate new java sources from the files. (I'm speaking generically to focus on the core problem and not on the specifics)

I wouldn't expect this to be so hard, but I've been unable to get it working. Here's what I've created:

I defined a new configuration:

configurations {
    newConf
}

Later, I've defined a single dependency for this confiuguration. It resolves to the zip file I need to explode:

dependencies {
    newConf "group:name:version@zip"
}

So far this all smells correct to me, though if anybody disagrees, I'm listening.

Lastly I need to define a task that explodes the zip into a directory, which then becomes the input for a later code-generation command.

task explodeModel {
    description = "unzip model into the specified 'modelSrc' directory"

    //input is a "files" collection (usually just one:  the zip)
    //output is the specified modelSrc dir

    File modelSrc = new File("$buildDir/modelSrc")
    outputs.files modelSrc

    doLast {
        configurations.newConf.allArtifacts.each { artifact -> println artifact }
    }

}

Obviously doLast isn't unzipping anything just yet, I'm just trying to get an absolute path to the zipfile itself, and thats where I'm stuck. I have no idea how to get a path to the file so I can unzip it. Any help?

Many thanks

like image 885
romacafe Avatar asked Nov 21 '11 16:11

romacafe


People also ask

How do I UnZip a file 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.

What is zipTree in gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.

How do I create a tar file in gradle?

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

What is gradle afterEvaluate?

The afterEvaluate is useful in the root gradle file of a multi project build when you want to configure specific items based on the configuration made in subprojects. Say you want to add a task for all subprojects that have a specific plugin defined.


1 Answers

Try something along these lines, it should work:

task explodeModel(type: Copy){
  configurations.newConf.filter { it.toString().endsWith(".zip") }.each{
    from zipTree(it)
  }
  into 'output/dir'
}

Of course, you can omit the .zip filtering part if you are sure that artifacts in the configuration are zips.

like image 184
rodion Avatar answered Oct 31 '22 00:10

rodion