hublo
hublo

Reputation: 1180

Gradle exclude module for Copy task

I have a Copy task set as follow:

task copyToLib( type: Copy ) {
   into "$buildDir/myapp/lib"
   from configurations.runtime

   // We only want jars files to go in lib folder
   exclude "*.exe"
   exclude "*.bat"
   exclude "*.cmd"
   exclude "*.dll"

   // We exclude some lib
   exclude group: "org.slf4j", name: "slf4j-api", version: "1.6.2"
}

And i'm getting the following error:

Could not find method exclude() for arguments [{group=org.slf4j, name=slf4j-api, version=1.6.2}] on task ':copyToLib' of type org.gradle.api.tasks.Copy

I have the feeling that it's only a syntax issue, any hint?

Upvotes: 2

Views: 10389

Answers (3)

Shandilya
Shandilya

Reputation: 103

For Gradle 7.4 with Groovy:

task copyLibs(type: Copy){

    from configurations.externalLib{
        into '<dest-dir-name>'

        exclude('slf*.jar', '<other jars if needed>')
    }
}

FYI:
configurations {
    externalLib.extendsFrom(implementation)
}

Upvotes: 0

LazerBanana
LazerBanana

Reputation: 7211

Exclude by group: exclude group: org.slf4j

Exclude by module: exclude module: slf4j-api

Exclude by file name: exclude { it.file.name.contains('slf4j-api') }

Exclude a file: exclude "slf4j-api.jar"

You can exclude by group and module but it needs to go into configurations exclude like this. Then it's gonna restrict the configuration before copying.

task copyToLib( type: Copy ) {
    into "$buildDir/myapp/lib"
    from configurations.runtime {
        exclude group: 'org.slf4j'
    }

    // We only want jars files to go in lib folder
    exclude "*.exe"
    exclude "*.bat"
    exclude "*.cmd"
    exclude "*.dll"

}

And remember to make sure that the directory exists $buildDir/myapp/lib

And maybe instead of excluding all other files just include jars?

Upvotes: 6

aristotll
aristotll

Reputation: 9177

Maybe write a method to help you

static String generateExcludeJar(Map<String, String> map) {
    "$map.name-${map.version}.jar" 
   // All jar names are like name-version.jar when downloaded by Gradle.
}

exclude generateExcludeJar(group: "org.slf4j", name: "slf4j-api", version: "1.6.2")

Upvotes: 0

Related Questions