From 5b5f231cfb8f6bf51abbf4dfa95bbf879f9b4cdd Mon Sep 17 00:00:00 2001 From: Eric Ward Date: Wed, 11 Mar 2020 08:51:42 -0400 Subject: [PATCH] Allow package exclusive for ConfigureShadowRelocation --- .../tasks/ConfigureShadowRelocation.groovy | 12 +++++- .../ConfigureShadowRelocationSpec.groovy | 41 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ConfigureShadowRelocation.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ConfigureShadowRelocation.groovy index 8ff341b3e..d601cece8 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ConfigureShadowRelocation.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ConfigureShadowRelocation.groovy @@ -19,6 +19,9 @@ class ConfigureShadowRelocation extends DefaultTask { @Input String prefix = "shadow" + @Input + List excludedPackages = [] + @InputFiles @Optional List getConfigurations() { return target.configurations @@ -31,7 +34,7 @@ class ConfigureShadowRelocation extends DefaultTask { configuration.files.each { jar -> JarFile jf = new JarFile(jar) jf.entries().each { entry -> - if (entry.name.endsWith(".class")) { + if (entry.name.endsWith(".class") && !matchesExcludeList(entry.name)) { packages << entry.name[0..entry.name.lastIndexOf('/')-1].replaceAll('/', '.') } } @@ -48,4 +51,11 @@ class ConfigureShadowRelocation extends DefaultTask { return "configureRelocation${task.name.capitalize()}" } + boolean matchesExcludeList(String className) { + return excludedPackages.any { + if (className.startsWith(it)) { + return true + } + } + } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigureShadowRelocationSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigureShadowRelocationSpec.groovy index 42cd17b6a..ee5fefea5 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigureShadowRelocationSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigureShadowRelocationSpec.groovy @@ -23,7 +23,6 @@ class ConfigureShadowRelocationSpec extends PluginSpecification { when: runner.withArguments('shadowJar', '-s').build() - then: then: contains(output, [ 'META-INF/MANIFEST.MF', @@ -44,4 +43,44 @@ class ConfigureShadowRelocationSpec extends PluginSpecification { 'shadow/junit/framework/TestSuite.class' ]) } + + def "auto relocate plugin dependencies with exclusion list"() { + given: + buildFile << """ + + task relocateShadowJar(type: com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation) { + target = tasks.shadowJar + excludedPackages = ["junit/framework"] + } + + tasks.shadowJar.dependsOn tasks.relocateShadowJar + + dependencies { + compile 'junit:junit:3.8.2' + } + """.stripIndent() + + when: + runner.withArguments('shadowJar', '-s').build() + + then: + contains(output, [ + 'META-INF/MANIFEST.MF', + 'shadow/junit/textui/ResultPrinter.class', + 'shadow/junit/textui/TestRunner.class', + 'junit/framework/Assert.class', + 'junit/framework/AssertionFailedError.class', + 'junit/framework/ComparisonCompactor.class', + 'junit/framework/ComparisonFailure.class', + 'junit/framework/Protectable.class', + 'junit/framework/Test.class', + 'junit/framework/TestCase.class', + 'junit/framework/TestFailure.class', + 'junit/framework/TestListener.class', + 'junit/framework/TestResult$1.class', + 'junit/framework/TestResult.class', + 'junit/framework/TestSuite$1.class', + 'junit/framework/TestSuite.class' + ]) + } }