Gradle 'exclude' local `fileTree` based transitive dependency from sub-project

Viewed 774

For some weird reasons, I'm using local filetree based jars in the gradle classpath.

from sub-module(project), I want to exclude one of the local jar. but I'm missing the syntax for it, can someone provide me correct syntax to exclude the local jar file(s) as a transitive dependency here?

dependencies {
    implementation(project(":my-simple-project")) {
        exclude fileTree(dir: "../lib/", include: ["axis2-1.7.8/axis2-transport-http-1.7.8.jar"])
    }
   }

OR

dependencies {
    implementation(project(":my-simple-project")) {
        exclude files("../lib/axis2-1.7.8/axis2-transport-http-1.7.8.jar"])
    }
   }

I got an error as

* What went wrong:
A problem occurred evaluating project ':my-simple-project'.
> Could not find method exclude() for arguments [directory '../lib/'] on DefaultProjectDependency{dependencyProject='project ':my-simple-project'', configuration='default'} of type org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependency.

2 Answers

You can only exclude module dependencies, not file dependencies.


If you cannot resolve your dependencies from public repositories like Maven Central or JCenter, you can create a local Maven repository (even inside your project structure) and resolve the files directly from there:

repositories {
    maven {
        url uri("${projectDir}/lib")
    }
}

dependencies {
    implementation 'org.apache.axis2:axis2-transport-http:1.7.8'
}

Please note, that you must modify the directory structure in your lib folder to match the Maven directory structure (or alternatively define an Ivy repository with a custom structure).

You can exclude nested jar using the following syntax:

fileTree(dir: 'lib').exclude {details ->
    (details.file.canonicalPath.contains("axis2-1.7.8")
            && details.file.canonicalPath.contains('axis2-transport-http-1.7.8'))}

OR

fileTree(dir: 'lib').exclude { details ->
        details.file.text.contains('axis2-transport-http')
}

Reference : Gradle fileTree exclude all except certain directories

Related