Extract resources from jar file?


(toolforger) #1

I’m probably too confused about Groovy and how the DSL is constructed, but I’m having trouble specifying what I want. The task at hand: Extract the contents of a jar file as resources. (Can’t simply include the jar as a dependency, some files need to be renamed.)

Here’s what I tried (most likely showing that this is my first interaction with both Gradle and Groovy):

sourceSets {
  main {
    java {
      ...
    }
    resources {
      zipTree 'path/to/libs/mylib.jar'
    }
  }
}

which gets ignored.

Then I tried

sourceSets {
  main {
    java {
      ...
    }
    resources {
      srcDirs = zipTree 'path/to/libs/mylib.jar'
    }
  }
}

but that gave me

Could not determine the dependencies of task ':processResources'.

Finally I tried

sourceSets {
  main {
    java {
      ...
    }
    resources {
      srcDir zipTree 'path/to/libs/mylib.jar'
    }
  }
}

which gave me

No such property: path/to/libs/mylib.jar for class: org.gradle.api.internal.file.DefaultSourceDirectorySet

At that point, I decided that trial-and-error configuring might be less effective than asking on the forum…

I tried looking at working_with_files.html, but I’m having trouble applying task-based examples to DSL configuration inside a sourceSet block (due to being new to Gradle and Groove, my understanding of how the DSL works is improving but still a lot more vague than I feel comfortable with). I know I could simply slap a closure on the sourceSets block that reconfigures things, but the approaches that googling gave me would splitting the configuration for a specific resource in two.

Any (pointers to) working examples appreciated. Thanks!


(René Groeschke) #2

Hello Joachim, The srcDir property in the resources block should point to a directory on your classpath. zipTree returns a FileTree implementation.

Do you need (#1) a copy of the manipulated resources in your resource directory or (#2) is it enough to have the (manipulated) resources in the jar of your application.

for #1 you can put a simple copy operation after your sourceSets definition:

sourcsets{
...
}
copy{
   from(zipTree("path/to/your/lib.jar"))
   into("src/main/resources")
}

for #2 you just need to customize the processResources task:

processResources{
   from(zipTree("path/to/your/lib.jar"))
}

hope that helped.

cheers, René


(toolforger) #3

No. 2 is enough actually :slight_smile:

With the added complication that I’ll need to strip the META-INF directory, and rename a subdirectory. But I guess that processResources is a Copy at heart, so that should work; I’ll be off experimenting.

And this also conveniently covers that case where I need to assemble a jar. I was a bit worried that I wouldn’t find the equivalent of Maven’s assemble plugin, but I see that’s already covered by the standard setup and doesn’t need a plugin - nice!

Thanks for the info, I now know enough to continue. I’ll be back as soon as I hit another problem :slight_smile: