It seems that when excluding a module from a configuration (like compile) it also excludes it from extending configurations (like compileOnly). Here is my contrived example.
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
compileOnly group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
}
configurations.compile {
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}
task resolvedArtifacts << {
def sortedArtifacts = new TreeSet()
configurations.compileOnly.resolvedConfiguration.resolvedArtifacts.each { sortedArtifacts.add(it.file.name) }
println 'Compile Only:'
sortedArtifacts.each { println it }
sortedArtifacts.clear()
println ''
configurations.compile.resolvedConfiguration.resolvedArtifacts.each { sortedArtifacts.add(it.file.name) }
println 'Compile:'
sortedArtifacts.each { println it }
}
task dependencyList << {
println 'Compile Only:'
configurations.compileOnly.allDependencies.each { dep -> println "$dep.group:$dep.name:$dep.version" }
println ''
println 'Compile:'
configurations.compile.allDependencies.each { dep -> println "$dep.group:$dep.name:$dep.version" }
}
The dependencyList task outputs the following:
Compile Only:
org.slf4j:slf4j-log4j12:1.7.21
org.slf4j:slf4j-api:1.7.21
Compile:
org.slf4j:slf4j-api:1.7.21
However, the resolvedArtifacts task outputs this:
Compile Only:
slf4j-api-1.7.21.jar
Compile:
slf4j-api-1.7.21.jar
Is this expected?
What I’m actually trying to do is ensure that a particular dependency is globally excluded from the compile and runtime configurations but is included in the compileOnly configuration, similar to how Maven’s provided scope works when defined in the <dependencyManagement> section of the POM. I thought that excluding it from the compile, runtime, and testRuntime configurations but adding it to the compileOnly configuration would work, but it is not because of the behavior described above.