52

I was wondering if there was anyway to exclude specific files,that are inside a dependency (not a transitive dependency), from being downloaded.

I am switching a build from Ant + Ivy to Gradle and this was done with in Ivy before. I ask because I have a single dependency that contains many compiled wsdl jars in Artifactory that we are pulling down, but I do not want to download all of the jars in the dependency.

In Ivy it was setup like:

These 6 artifacts are published to Artifactory in to one directory repo/dep.location/example/7.3/jar.

<publications>
    <artifact name="foo-1-0" type="jar" />
    <artifact name="foo-1-0-async" type="jar" />
    <artifact name="foo-1-0-xml" type="jar" />
    <artifact name="bar-1-0" type="jar" />
    <artifact name="bar-1-0-async" type="jar" />
    <artifact name="bar-1-0-xml" type="jar" />
</publications>

This is how I retrieve only two of the six artifacts.

<dependency org="dep.location" name="example" rev="7.3"
            conf="compile,runtime">
    <include name="foo-1-0-async"/>
    <include name="foo-1-0-xml"/>
</dependency>

Currently if I attempt to do something similar in Gradle the excludes are ignored and all six artifacts are downloaded.

compile (group:"dep.location", name:"example", version:"7.3")
{
    exclude module:'foo-1-0-xml'
    exclude module:'bar-1-0'
    exclude module:'bar-1-0-async'
    exclude module:'bar-1-0-xml'
}

I am using Gradle version 1.8.

bhumphrey
  • 625
  • 1
  • 6
  • 13
  • The things that you are trying to exclude are artifacts, not modules. The only supported way to include/exclude artifacts in Gradle that I'm aware of is by type, which won't help here. – Peter Niederwieser May 13 '14 at 19:45
  • This is not supported in gradle. But an alternative is change how you publish to artifactory. Why don't you start publishing individual jar files in artifactory? – vkg May 13 '14 at 19:55
  • Thats a possibility, but there are a some where around 40-50 services total that are being pre-compiled into the client, async, and xml jars and each with it's own Jenkins jobs and all that good stuff. That is a lot of publishing and extra work. – bhumphrey May 13 '14 at 20:02
  • http://www.gradle.org/docs/current/userguide/dependency_management.html Maybe this will help. – explodingcreeperssss May 13 '14 at 20:05

3 Answers3

6

I don't think Gradle has any built in support for accomplishing this, but you can clean the artifacts out from the classpath yourself.

Inspired by this thread on the Gradle forums I came up with this:

// The artifacts we don't want, dependency as key and artifacts as values
def unwantedArtifacts = [
    "dep.location:example": [ "foo-1-0-xml", "bar-1-0", "bar-1-0-async", "bar-1-0-xml"],
]

// Collect the files that should be excluded from the classpath
def excludedFiles = configurations.compile.resolvedConfiguration.resolvedArtifacts.findAll {
    def moduleId = it.moduleVersion.id
    def moduleString = "${moduleId.group}:${moduleId.name}:${moduleId.version}" // Construct the dependecy string
    // Get the artifacts (if any) we should remove from this dependency and check if this artifact is in there
    it.name in (unwantedArtifacts.find { key, value -> moduleString.startsWith key }?.value)
}*.file

// Remove the files from the classpath
sourceSets {
    main {
        compileClasspath -= files(excludedFiles)
    }
    test {
        compileClasspath -= files(excludedFiles)
    }
}

Note that Gradle will probably still download the files and cache them for you, but they should not be in your classpath.

Raniz
  • 10,882
  • 1
  • 32
  • 64
1

I am not sure if this is what you want, but since we are using Spring Boot and Wildfly, we have to remove the tomcat-starter module from the spring boot standard package, and it looks very similar to what you've done. However, our code states:

configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}

I have not checked if the corresponding jar is not downloaded or just not on the classpath, I know however that it is not used anymore.

0

This question suggested to me , when I'd looking for excluding unwanted jar from one of my dependency group. Yeah, there are ways to exclude specific files,that are inside a dependency.

implementation (group: 'net.sf.jasperreports', name: 'jasperreports', version: '6.1.0'){
//example : org.olap4j:olap4j:0.9.7.309-JS-3
exclude group: 'org.olap4j', module: 'olap4j'

}

DalusC
  • 391
  • 1
  • 12