How can I exclude all transitive dependencies?

I’m writing Gradle scripts for legacy Java projects and want to be sure that all jars referenced in the code are declared in the dependencies section.

Instead of declaring every dependency as intransitive:

dependencies {
    compile('x:y:z') { transitive = false }
    // ...
}

I did this:

configurations.compile.transitive = false

Which seemed to work fine when I started this project many months ago. After a longer break, I’m now continuing and have updated from Gradle 1.6 to 1.8, but noticed that my method of making everything intransitive no longer seems to work.

This:

print(sourceSets.main.compileClasspath.asPath.replaceAll(";", "\n"))

shows jars that are not declared as compile dependencies, and some projects compile even though they shouldn’t because direct dependencies are not declared.

Did anything change from 1.6 to 1.8 that I missed? Or is there another mistake I am making?

‘configurations.compile.transitive = false’ works fine for me, and I’m not aware of any changes in recent Gradle versions. (Another way to avoid transitive dependencies is ‘x:y:z@jar’.) Can you show a minimal self-contained example that demonstrates the issue?

I found the problem with my build file. I was using the Spring PropDeps plugin, which no longer seems to be compatible with Gradle 1.8:

https://github.com/spring-projects/gradle-plugins/tree/master/propdeps-plugin

After removing the plugin and creating my own “provided” configuration, the problem went away.

Would you be able to share your own “provided” configuration?

Sure.

To create the ‘provided’ configuration I basically followed the work around from GRADLE-784:

configurations {

provided

}

sourceSets {

main { compileClasspath += configurations.provided }

}

I put this into a plugin, where it looks like this:

private static void createProvidedConfiguration(Project project) {

Configuration providedConfiguration = project.configurations.create(PROVIDED_CONFIGURATION);

providedConfiguration.transitive = false;

JavaPluginConvention javaConvention = project.convention.getPlugin(JavaPluginConvention.class);

SourceSetContainer sourceSets = javaConvention.sourceSets;

SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);

FileCollection combinedClasspath = new UnionFileCollection(

mainSourceSet.compileClasspath,

providedConfiguration);

mainSourceSet.compileClasspath = combinedClasspath;

// configure tasks

}

The tasks that you need to configure depend on what you are using, might be things like Java doc or Eclipse project generation. Also look at the example on the bottom of this page.

This issue is being fixed in propdeps-plugin. See https://github.com/spring-projects/gradle-plugins/pull/23