Zara
Zara

Reputation: 43

Build Zip task with gradle including selected dependencies

I want to create zip/war/jar artifact in gradle that includes few classes and only required dependencies

I have simple zip task as below that works:

 task myZip(type: Zip) {
    from sourceSets.main.output
    include 'com/mypackage/*'   
    archiveName 'myzip.zip'
    }

Now I want to include few of compile dependencies Sample build.gradle skeleton:

 apply plugin : 'java'
    apply plugin : 'eclipse'
    apply plugin: 'maven-publish'
    repositories{
    }
    configurations
    {
    }
    dependencies
    {
    compile <dependency1>
    compile <dependency2>
    compile <dependency3>
    ...

    compile <dependency20>
    myzipjar <dependency3>
    myzipjar <dependency6>
    myzipjar <dependency13>
    }

And to my zip task:

task myZip(type: Zip) {
      from sourceSets.main.output
     include 'com/mypackage/*'
    **into 'lib'
    from configurations.myzipjar**   
     archiveName 'myzip.zip'

    }

Upvotes: 1

Views: 1215

Answers (1)

Igor Melnichenko
Igor Melnichenko

Reputation: 145

The solution depends on required selection criteria.

You can either treat Configuration as a regular FileCollection from which you can query File instances:

configurations.compile.findAll
{
    it.name.startsWith == "required-prefix" // it is an instance of File
}

...or you can use its getResolvedConfiguration() method to filter resolution result by originating Dependency instances:

configurations.compile.resolvedConfiguration.getFiles
{
    it.group == "target-group" // it is an instance of Dependency
}

Or maybe you should consider using a separate configuration that will be included into both compile configuration and yout zip task:

configurations
{
    zippableCompile
    compile.extendsFrom zippableCompile
}

dependencies
{
    zippableCompile <...>
}

P. S. The compile configuration is deprecated since Gradle 3.4, use implementation instead.

Upvotes: 1

Related Questions