Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a zip in gradle from a list of files?

Tags:

gradle

This is most certainly a gradle newbie question but it's really beating me up. My source tree looks like this:

|-- master
    buildSrc
    build.gradle
|-- comp1
    !-- filea
    !-- fileb
|-- comp2
    !-- file1
    !-- file2
    !-- etc

I'm running the build.gradle in the master directory which uses a custom task (called in prepBundle (not shown)) to produce a list of files which need to be in a zip file. There is some various external processing that goes into the list so there isn't a way to use any basic includes or closures to come up with the list.

This is what I have:

task showFilelist(dependsOn:prepBundle){ 
  prepBundle.outputs.files.each{ 
    println "including file: ${it}"
  }
}

task createBundle(type:Zip,dependsOn:prepBundle){ 
  inputs.files(prepBundle.outputs.files)
  outputs.file(archiveName)
  baseName = "somebundle"
  from ".."
  include prepBundle.outputs.files
}

If I just run showFilelist, I do get the correctly resolved paths to the files I want to zip:

<snip>
:showFilelist
including file: C:\fullpath\master\build.gradle
including file: C:\fullpath\comp1\filea
including file: C:\fullpath\comp2\file1

But when I run my bundling task, it blows up:

:createBundle

FAILURE: Build aborted because of an internal error.

* What went wrong:
Build aborted because of an unexpected internal error. Please file an issue at: http://forums.gradle.org.

* Try:
Run with --debug option to get additional debug info.

* Exception is:
org.gradle.api.UncheckedIOException: java.io.IOException: The process cannot access the file because another process has locked a portion of the file
        at org.gradle.util.hash.HashUtil.createHash(HashUtil.java:56)
        at org.gradle.util.hash.HashUtil.createHash(HashUtil.java:34)
        at org.gradle.api.internal.changedetection.state.DefaultHasher.hash(DefaultHasher.java:24)
        at org.gradle.api.internal.changedetection.state.CachingHasher.hash(CachingHasher.java:45)
        at org.gradle.api.internal.changedetection.state.DefaultFileSnapshotter$1.run(DefaultFileSnapshotter.j
ava:48)
        at org.gradle.internal.Factories$1.create(Factories.java:22)
        at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAccess.java:143)
        at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAccess.java:131)
        at org.gradle.cache.internal.DefaultPersistentDirectoryStore.useCache(DefaultPersistentDirectoryStore.

In prepBundle I iterate the list of files and there isn't any file being used by another process. I don't believe it's a gradle problem, I think it's something I'm doing wrong with the configuration of the zip task. If I change the createBundle a bit to drop the inputs and outputs, it looks like this:

task createBundle(type:Zip,dependsOn:prepBundle){ 
  baseName = "somebundle"
  include prepBundle.outputs.files
}

But then doesn't do anything:

:createBundle UP-TO-DATE

BUILD SUCCESSFUL

How should I put this zip together when I just have a list of files and the parent root directory for the files are not under my current project?

Thanks for any help!

  • Andy
like image 786
AndyJ Avatar asked Oct 18 '13 15:10

AndyJ


1 Answers

You can easily create a zip file with a task, look at the example below:

task createDist(type: Zip){
  archiveName="dist.zip"
  destinationDir = file('build/')
  from (files('./test-module/build/libs'))
}

You can use variety of methods to include files here, e.g.:

 from fileTree('./libs')
 from project('config').jar.outputs.files
 from project('core').jar.outputs.files
 from project('batch-jobs').jar.outputs.files
 from project('gateway1').jar.outputs.files
 from project('gateway2').jar.outputs.files
like image 153
ankit.ag.in Avatar answered Oct 26 '22 23:10

ankit.ag.in