Skip to content

Instantly share code, notes, and snippets.

@uklance
Created June 15, 2017 14:01
Show Gist options
  • Save uklance/c601762e7da90751e617072c93477173 to your computer and use it in GitHub Desktop.
Save uklance/c601762e7da90751e617072c93477173 to your computer and use it in GitHub Desktop.
Gradle monkey patch example for org.springframework:spring-context
apply plugin: 'java'
ext {
target = 'org.springframework:spring-context:4.3.9.RELEASE'
}
configurations {
monkeyPatchNonTransitive { transitive = false }
monkeyPatchTransitive
}
dependencies {
monkeyPatchTransitive target
monkeyPatchNonTransitive target
compileOnly(target) {
transitive = false
}
}
ResolvedDependency topDependency = configurations.monkeyPatchTransitive.resolvedConfiguration.firstLevelModuleDependencies.iterator().next()
topDependency.children.each { ResolvedDependency child ->
child.allModuleArtifacts.each { ResolvedArtifact artifact ->
ModuleVersionIdentifier mvi = artifact.moduleVersion.id
def dependency = [
group: mvi.group,
name: mvi.name,
version: mvi.version,
ext: artifact.extension
]
if (artifact.classifier) {
dependency['classifier'] = artifact.classifier
}
println "Adding $dependency"
dependencies.compile(dependency) {
transitive = false
}
}
}
jar {
with copySpec {
from zipTree(configurations.monkeyPatchNonTransitive.singleFile)
eachFile { FileCopyDetails action ->
String path = action.relativePath.pathString
def output = sourceSets.main.output
boolean patched = new File(output.classesDir, path).exists() || new File(output.resourcesDir, path).exists()
if (patched) {
println "Using patched version of $path"
action.exclude()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment