From cbc6327f79954d334aee326a5fa5e6587305511c Mon Sep 17 00:00:00 2001 From: John Engelman Date: Fri, 14 Nov 2014 08:56:32 -0600 Subject: [PATCH] Convert ShadowJar to Java to workaround Gradle 2.2 binary issue. --- .../plugins/shadow/tasks/ShadowJar.groovy | 186 ---------- .../plugins/shadow/tasks/ShadowJar.java | 321 ++++++++++++++++++ .../plugins/shadow/tasks/ShadowSpec.groovy | 36 -- .../plugins/shadow/tasks/ShadowSpec.java | 40 +++ .../plugins/shadow/ShadowPluginSpec.groovy | 2 +- 5 files changed, 362 insertions(+), 223 deletions(-) delete mode 100644 src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.groovy create mode 100644 src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.java delete mode 100644 src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec.groovy create mode 100644 src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec.java diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.groovy deleted file mode 100644 index 9dc1409db..000000000 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.groovy +++ /dev/null @@ -1,186 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.tasks - -import com.github.jengelman.gradle.plugins.shadow.ShadowStats -import com.github.jengelman.gradle.plugins.shadow.internal.DefaultDependencyFilter -import com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter -import com.github.jengelman.gradle.plugins.shadow.internal.GradleVersionUtil -import com.github.jengelman.gradle.plugins.shadow.internal.ZipCompressor -import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator -import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator -import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer -import com.github.jengelman.gradle.plugins.shadow.transformers.GroovyExtensionModuleTransformer -import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer -import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer -import org.gradle.api.artifacts.Configuration -import org.gradle.api.file.FileCollection -import org.gradle.api.internal.DocumentationRegistry -import org.gradle.api.internal.file.FileResolver -import org.gradle.api.internal.file.copy.CopyAction -import org.gradle.api.tasks.InputFiles -import org.gradle.api.tasks.Optional -import org.gradle.api.tasks.TaskAction -import org.gradle.api.tasks.bundling.Jar -import org.gradle.api.tasks.util.PatternSet -import org.gradle.util.ConfigureUtil - -class ShadowJar extends Jar implements ShadowSpec { - - List transformers = [] - List relocators = [] - List configurations = [] - DependencyFilter dependencyFilter - - private final ShadowStats shadowStats = new ShadowStats() - private final GradleVersionUtil versionUtil - - ShadowJar() { - versionUtil = new GradleVersionUtil(project.gradle.gradleVersion) - dependencyFilter = new DefaultDependencyFilter(project) - manifest = new DefaultInheritManifest(getServices().get(FileResolver)) - } - - @Override - protected CopyAction createCopyAction() { - DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry) - return new ShadowCopyAction(getArchivePath(), getInternalCompressor(), documentationRegistry, - transformers, relocators, rootPatternSet, shadowStats) - } - - protected ZipCompressor getInternalCompressor() { - versionUtil.getInternalCompressor(entryCompression, this) - } - - @TaskAction - protected void copy() { - from(includedDependencies) - super.copy() - logger.info(shadowStats.toString()) - } - - @InputFiles @Optional - public FileCollection getIncludedDependencies() { - dependencyFilter.resolve(configurations) - } - - /** - * Utility method for assisting between changes in Gradle 1.12 and 2.x - * @return - */ - protected PatternSet getRootPatternSet() { - versionUtil.getRootPatternSet(mainSpec) - } - - /** - * Configure inclusion/exclusion of module & project dependencies into uber jar - * @param c - * @return - */ - ShadowJar dependencies(Closure c) { - ConfigureUtil.configure(c, dependencyFilter) - return this - } - - /** - * Add a Transformer instance for modifying JAR resources and configure. - * @param clazz - * @param c - * @return - */ - ShadowJar transform(Class clazz, Closure c = null) { - Transformer transformer = (Transformer) clazz.newInstance() - ConfigureUtil.configure(c, transformer) - transformers << transformer - return this - } - - /** - * Add a preconfigured transformer instance - * @param transformer - * @return - */ - ShadowJar transform(Transformer transformer) { - transformers << transformer - return this - } - - /** - * Syntactic sugar for merging service files in JARs - * @return - */ - ShadowJar mergeServiceFiles() { - transform(ServiceFileTransformer) - } - - /** - * Syntactic sugar for merging service files in JARs - * @return - */ - ShadowJar mergeServiceFiles(String rootPath) { - transform(ServiceFileTransformer) { - path = rootPath - } - } - - /** - * Syntactic sugar for merging service files in JARs - * @return - */ - ShadowJar mergeServiceFiles(Closure configureClosure) { - transform(ServiceFileTransformer, configureClosure) - } - - /** - * Syntactic sugar for merging Groovy extension module descriptor files in JARs - * @return - */ - ShadowJar mergeGroovyExtensionModules() { - transform(GroovyExtensionModuleTransformer) - } - - /** - * Syntax sugar for merging service files in JARs - * @return - */ - ShadowJar append(String resourcePath) { - transform(AppendingTransformer) { - resource = resourcePath - } - } - - /** - * Add a class relocator that maps each class in the pattern to the provided destination - * @param pattern - * @param destination - * @param configure - * @return - */ - ShadowJar relocate(String pattern, String destination, Closure configure = null) { - SimpleRelocator relocator = new SimpleRelocator(pattern, destination, [], []) - ConfigureUtil.configure(configure, relocator) - relocators << relocator - return this - } - - /** - * Add a relocator instance - * @param relocator - * @return - */ - ShadowJar relocate(Relocator relocator) { - relocators << relocator - return this - } - - /** - * Add a relocator of the provided class and configure - * @param relocatorClass - * @param configure - * @return - */ - ShadowJar relocate(Class relocatorClass, Closure configure = null) { - Relocator relocator = (Relocator) relocatorClass.newInstance() - ConfigureUtil.configure(configure, relocator) - relocators << relocator - return this - } -} diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.java b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.java new file mode 100644 index 000000000..c2fcce695 --- /dev/null +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.java @@ -0,0 +1,321 @@ +package com.github.jengelman.gradle.plugins.shadow.tasks; + +import com.github.jengelman.gradle.plugins.shadow.ShadowStats; +import com.github.jengelman.gradle.plugins.shadow.internal.DefaultDependencyFilter; +import com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter; +import com.github.jengelman.gradle.plugins.shadow.internal.GradleVersionUtil; +import com.github.jengelman.gradle.plugins.shadow.internal.ZipCompressor; +import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator; +import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator; +import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer; +import com.github.jengelman.gradle.plugins.shadow.transformers.GroovyExtensionModuleTransformer; +import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer; +import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer; +import groovy.lang.MetaClass; +import org.codehaus.groovy.runtime.InvokerHelper; +import org.gradle.api.Action; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.file.FileCollection; +import org.gradle.api.internal.DocumentationRegistry; +import org.gradle.api.internal.file.FileResolver; +import org.gradle.api.internal.file.copy.CopyAction; +import org.gradle.api.tasks.InputFiles; +import org.gradle.api.tasks.Optional; +import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.bundling.Jar; +import org.gradle.api.tasks.util.PatternSet; + +import java.util.ArrayList; +import java.util.List; + +public class ShadowJar extends Jar implements ShadowSpec { + + private List transformers; + private List relocators; + private List configurations; + private DependencyFilter dependencyFilter; + + private final ShadowStats shadowStats = new ShadowStats(); + private final GradleVersionUtil versionUtil; + + public ShadowJar() { + versionUtil = new GradleVersionUtil(getProject().getGradle().getGradleVersion()); + dependencyFilter = new DefaultDependencyFilter(getProject()); + setManifest(new DefaultInheritManifest(getServices().get(FileResolver.class))); + transformers = new ArrayList(); + relocators = new ArrayList(); + configurations = new ArrayList(); + } + + @Override + protected CopyAction createCopyAction() { + DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry.class); + return new ShadowCopyAction(getArchivePath(), getInternalCompressor(), documentationRegistry, + transformers, relocators, getRootPatternSet(), shadowStats); + } + + protected ZipCompressor getInternalCompressor() { + return versionUtil.getInternalCompressor(getEntryCompression(), this); + } + + @TaskAction + protected void copy() { + from(getIncludedDependencies()); + super.copy(); + getLogger().info(shadowStats.toString()); + } + + @InputFiles @Optional + public FileCollection getIncludedDependencies() { + return dependencyFilter.resolve(configurations); + } + + /** + * Utility method for assisting between changes in Gradle 1.12 and 2.x + * @return + */ + protected PatternSet getRootPatternSet() { + return versionUtil.getRootPatternSet(getMainSpec()); + } + + /** + * Configure inclusion/exclusion of module & project dependencies into uber jar + * @param c + * @return + */ + public ShadowJar dependencies(Action c) { + if (c != null) { + c.execute(dependencyFilter); + } + return this; + } + + /** + * Add a Transformer instance for modifying JAR resources and configure. + * @param clazz + * @return + */ + public ShadowJar transform(Class clazz) throws InstantiationException, IllegalAccessException { + return transform(clazz, null); + } + + /** + * Add a Transformer instance for modifying JAR resources and configure. + * @param clazz + * @param c + * @return + */ + public ShadowJar transform(Class clazz, Action c) throws InstantiationException, IllegalAccessException { + T transformer = clazz.newInstance(); + if (c != null) { + c.execute(transformer); + } + transformers.add(transformer); + return this; + } + + /** + * Add a preconfigured transformer instance + * @param transformer + * @return + */ + public ShadowJar transform(Transformer transformer) { + transformers.add(transformer); + return this; + } + + /** + * Syntactic sugar for merging service files in JARs + * @return + */ + public ShadowJar mergeServiceFiles() { + try { + transform(ServiceFileTransformer.class); + } catch (IllegalAccessException e) { + } catch (InstantiationException e) { + } + return this; + } + + /** + * Syntactic sugar for merging service files in JARs + * @return + */ + public ShadowJar mergeServiceFiles(final String rootPath) { + try { + transform(ServiceFileTransformer.class, new Action() { + + @Override + public void execute(ServiceFileTransformer serviceFileTransformer) { + serviceFileTransformer.setPath(rootPath); + } + }); + } catch (IllegalAccessException e) { + } catch (InstantiationException e) { + } + return this; + } + + /** + * Syntactic sugar for merging service files in JARs + * @return + */ + public ShadowJar mergeServiceFiles(Action configureClosure) { + try { + transform(ServiceFileTransformer.class, configureClosure); + } catch (IllegalAccessException e) { + } catch (InstantiationException e) { + } + return this; + } + + /** + * Syntactic sugar for merging Groovy extension module descriptor files in JARs + * @return + */ + public ShadowJar mergeGroovyExtensionModules() { + try { + transform(GroovyExtensionModuleTransformer.class); + } catch (IllegalAccessException e) { + } catch (InstantiationException e) { + } + return this; + } + + /** + * Syntax sugar for merging service files in JARs + * @return + */ + public ShadowJar append(final String resourcePath) { + try { + transform(AppendingTransformer.class, new Action() { + @Override + public void execute(AppendingTransformer transformer) { + transformer.setResource(resourcePath); + } + }); + } catch (IllegalAccessException e) { + } catch (InstantiationException e) { + } + return this; + } + + /** + * Add a class relocator that maps each class in the pattern to the provided destination + * @param pattern + * @param destination + * @return + */ + public ShadowJar relocate(String pattern, String destination) { + return relocate(pattern, destination, null); + } + + /** + * Add a class relocator that maps each class in the pattern to the provided destination + * @param pattern + * @param destination + * @param configure + * @return + */ + public ShadowJar relocate(String pattern, String destination, Action configure) { + SimpleRelocator relocator = new SimpleRelocator(pattern, destination, new ArrayList(), new ArrayList()); + if (configure != null) { + configure.execute(relocator); + } + relocators.add(relocator); + return this; + } + + /** + * Add a relocator instance + * @param relocator + * @return + */ + public ShadowJar relocate(Relocator relocator) { + relocators.add(relocator); + return this; + } + + /** + * Add a relocator of the provided class and configure + * @param relocatorClass + * @return + */ + public ShadowJar relocate(Class relocatorClass) throws InstantiationException, IllegalAccessException { + return relocate(relocatorClass, null); + } + + /** + * Add a relocator of the provided class and configure + * @param relocatorClass + * @param configure + * @return + */ + public ShadowJar relocate(Class relocatorClass, Action configure) throws InstantiationException, IllegalAccessException { + R relocator = relocatorClass.newInstance(); + if (configure != null) { + configure.execute(relocator); + } + relocators.add(relocator); + return this; + } + + public List getTransformers() { + return this.transformers; + } + + public void setTransformers(List transformers) { + this.transformers = transformers; + } + + public List getRelocators() { + return this.relocators; + } + + public void setRelocators(List relocators) { + this.relocators = relocators; + } + + public List getConfigurations() { + return this.configurations; + } + + public void setConfigurations(List configurations) { + this.configurations = configurations; + } + + public DependencyFilter getDependencyFilter() { + return this.dependencyFilter; + } + + public void setDependencyFilter(DependencyFilter filter) { + this.dependencyFilter = filter; + } + + // This code is only to make IntelliJ happy. + private transient MetaClass metaClass = InvokerHelper.getMetaClass(this.getClass()); + + public Object getProperty(String property) { + return this.getMetaClass().getProperty(this, property); + } + + public void setProperty(String property, Object newValue) { + this.getMetaClass().setProperty(this, property, newValue); + } + + public Object invokeMethod(String name, Object args) { + return this.getMetaClass().invokeMethod(this, name, args); + } + + public MetaClass getMetaClass() { + if(this.metaClass == null) { + this.metaClass = InvokerHelper.getMetaClass(this.getClass()); + } + + return this.metaClass; + } + + public void setMetaClass(MetaClass metaClass) { + this.metaClass = metaClass; + } +} diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec.groovy deleted file mode 100644 index 63b37a6f7..000000000 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec.groovy +++ /dev/null @@ -1,36 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.tasks - -import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator -import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer -import org.gradle.api.file.CopySpec - -interface ShadowSpec extends CopySpec { - - ShadowSpec dependencies(Closure configure) - - ShadowSpec transform(Class clazz) - - ShadowSpec transform(Class clazz, Closure configure) - - ShadowSpec transform(Transformer transformer) - - ShadowSpec mergeServiceFiles() - - ShadowSpec mergeServiceFiles(String rootPath) - - ShadowSpec mergeServiceFiles(Closure configureClosure) - - ShadowSpec mergeGroovyExtensionModules() - - ShadowSpec append(String resourcePath) - - ShadowSpec relocate(String pattern, String destination) - - ShadowSpec relocate(String pattern, String destination, Closure configure) - - ShadowSpec relocate(Relocator relocator) - - ShadowSpec relocate(Class clazz) - - ShadowSpec relocate(Class clazz, Closure configure) -} diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec.java b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec.java new file mode 100644 index 000000000..8874fc3ef --- /dev/null +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec.java @@ -0,0 +1,40 @@ +package com.github.jengelman.gradle.plugins.shadow.tasks; + +import com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter; +import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator; +import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator; +import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer; +import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer; +import org.gradle.api.Action; +import org.gradle.api.file.CopySpec; + +public interface ShadowSpec extends CopySpec { + + public ShadowSpec dependencies(Action configure); + + public ShadowSpec transform(Class clazz) throws InstantiationException, IllegalAccessException; + + public ShadowSpec transform(Class clazz, Action configure) throws InstantiationException, IllegalAccessException; + + public ShadowSpec transform(Transformer transformer); + + public ShadowSpec mergeServiceFiles(); + + public ShadowSpec mergeServiceFiles(String rootPath); + + public ShadowSpec mergeServiceFiles(Action configureClosure); + + public ShadowSpec mergeGroovyExtensionModules(); + + public ShadowSpec append(String resourcePath); + + public ShadowSpec relocate(String pattern, String destination); + + public ShadowSpec relocate(String pattern, String destination, Action configure); + + public ShadowSpec relocate(Relocator relocator); + + public ShadowSpec relocate(Class clazz) throws InstantiationException, IllegalAccessException; + + public ShadowSpec relocate(Class clazz, Action configure) throws InstantiationException, IllegalAccessException; +} diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy index 8d75b52fe..094398c93 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy @@ -84,7 +84,7 @@ class ShadowPluginSpec extends PluginSpecification { assert output.exists() where: - version << ['1.11', '1.12', '2.0', '2.1'] + version << ['1.11', '1.12', '2.0', '2.1', '2.2'] } def 'shadow copy'() {