From db07762e392640f2419c35c4fe439de61862935d Mon Sep 17 00:00:00 2001 From: Helder Pereira Date: Sat, 13 Mar 2021 16:18:50 +0000 Subject: [PATCH] Fix transformers deprecation warnings --- .../ApacheNoticeResourceTransformer.groovy | 18 +++++++- .../transformers/AppendingTransformer.groovy | 11 +++-- .../ComponentsXmlResourceTransformer.groovy | 6 +-- .../DontIncludeResourceTransformer.groovy | 8 +++- .../GroovyExtensionModuleTransformer.groovy | 2 +- .../IncludeResourceTransformer.groovy | 10 ++++- .../ManifestAppenderTransformer.groovy | 6 ++- .../ManifestResourceTransformer.groovy | 16 +++++-- .../PropertiesFileTransformer.groovy | 17 ++++++-- .../shadow/transformers/Transformer.groovy | 17 +++++--- .../XmlAppendingTransformer.groovy | 11 +++-- .../plugins/shadow/TransformerSpec.groovy | 43 ++++++++++++++++++- 12 files changed, 130 insertions(+), 35 deletions(-) diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.groovy index 03ad9d0b8..20970d76c 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.groovy @@ -23,6 +23,8 @@ import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream import org.codehaus.plexus.util.StringUtils import org.gradle.api.file.FileTreeElement +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional import java.text.SimpleDateFormat @@ -34,34 +36,48 @@ import java.text.SimpleDateFormat * @author John Engelman */ class ApacheNoticeResourceTransformer implements Transformer { + + @Input Set entries = new LinkedHashSet() + @Input Map> organizationEntries = new LinkedHashMap>() + @Input String projectName = "" // MSHADE-101 :: NullPointerException when projectName is missing + @Input boolean addHeader = true + @Input String preamble1 = "// ------------------------------------------------------------------\n" + "// NOTICE file corresponding to the section 4d of The Apache License,\n" + "// Version 2.0, in this case for " + @Input String preamble2 = "\n// ------------------------------------------------------------------\n" + @Input String preamble3 = "This product includes software developed at\n" - //defaults overridable via config in pom + @Input String organizationName = "The Apache Software Foundation" + @Input String organizationURL = "http://www.apache.org/" + @Input String inceptionYear = "2006" + @Optional + @Input String copyright /** * The file encoding of the NOTICE file. */ + @Optional + @Input String encoding private static final String NOTICE_PATH = "META-INF/NOTICE" diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformer.groovy index c75fe9ab6..ca56e7f18 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformer.groovy @@ -24,6 +24,7 @@ import org.apache.tools.zip.ZipOutputStream import org.codehaus.plexus.util.IOUtil import org.gradle.api.file.FileTreeElement import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional /** * A resource processor that appends content for a resource, separated by a newline. @@ -35,9 +36,12 @@ import org.gradle.api.tasks.Input */ @CacheableTransformer class AppendingTransformer implements Transformer { + + @Optional + @Input String resource - ByteArrayOutputStream data = new ByteArrayOutputStream() + private ByteArrayOutputStream data = new ByteArrayOutputStream() boolean canTransformResource(FileTreeElement element) { def path = element.relativePath.pathString @@ -67,9 +71,4 @@ class AppendingTransformer implements Transformer { IOUtil.copy(new ByteArrayInputStream(data.toByteArray()), os) data.reset() } - - @Input - String getResource() { - return resource - } } diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.groovy index 7883be136..60b255a59 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.groovy @@ -22,13 +22,13 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream -import org.gradle.api.file.FileTreeElement import org.codehaus.plexus.util.IOUtil import org.codehaus.plexus.util.ReaderFactory import org.codehaus.plexus.util.WriterFactory import org.codehaus.plexus.util.xml.Xpp3Dom import org.codehaus.plexus.util.xml.Xpp3DomBuilder import org.codehaus.plexus.util.xml.Xpp3DomWriter +import org.gradle.api.file.FileTreeElement /** * A resource processor that aggregates plexus components.xml files. @@ -129,8 +129,8 @@ class ComponentsXmlResourceTransformer implements Transformer { return !components.isEmpty() } - byte[] getTransformedResource() - throws IOException { + private byte[] getTransformedResource() + throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 4) Writer writer = WriterFactory.newXmlWriter(baos) diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/DontIncludeResourceTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/DontIncludeResourceTransformer.groovy index e8f68daeb..e0f78f4cd 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/DontIncludeResourceTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/DontIncludeResourceTransformer.groovy @@ -20,7 +20,10 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import org.apache.tools.zip.ZipOutputStream +import org.codehaus.plexus.util.StringUtils import org.gradle.api.file.FileTreeElement +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional /** * A resource processor that prevents the inclusion of an arbitrary @@ -31,11 +34,14 @@ import org.gradle.api.file.FileTreeElement * @author John Engelman */ class DontIncludeResourceTransformer implements Transformer { + + @Optional + @Input String resource boolean canTransformResource(FileTreeElement element) { def path = element.relativePath.pathString - if (path.endsWith(resource)) { + if (StringUtils.isNotEmpty(resource) && path.endsWith(resource)) { return true } diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/GroovyExtensionModuleTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/GroovyExtensionModuleTransformer.groovy index 7a6a3418a..5d814bb39 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/GroovyExtensionModuleTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/GroovyExtensionModuleTransformer.groovy @@ -21,8 +21,8 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream -import org.gradle.api.file.FileTreeElement import org.codehaus.plexus.util.IOUtil +import org.gradle.api.file.FileTreeElement /** * Modified from eu.appsatori.gradle.fatjar.tasks.PrepareFiles.groovy diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/IncludeResourceTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/IncludeResourceTransformer.groovy index 3e273aaf1..4eb61e5c3 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/IncludeResourceTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/IncludeResourceTransformer.groovy @@ -21,8 +21,12 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream -import org.gradle.api.file.FileTreeElement import org.codehaus.plexus.util.IOUtil +import org.gradle.api.file.FileTreeElement +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity /** * A resource processor that allows the addition of an arbitrary file @@ -33,8 +37,12 @@ import org.codehaus.plexus.util.IOUtil * @author John Engelman */ public class IncludeResourceTransformer implements Transformer { + + @InputFile + @PathSensitive(PathSensitivity.NAME_ONLY) File file + @Input String resource public boolean canTransformResource(FileTreeElement element) { diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer.groovy index 21502ff3f..325c5ea6a 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer.groovy @@ -23,9 +23,10 @@ import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream import org.codehaus.plexus.util.IOUtil import org.gradle.api.file.FileTreeElement +import org.gradle.api.tasks.Input -import static java.nio.charset.StandardCharsets.* -import static java.util.jar.JarFile.* +import static java.nio.charset.StandardCharsets.UTF_8 +import static java.util.jar.JarFile.MANIFEST_NAME /** * A resource processor that can append arbitrary attributes to the first MANIFEST.MF @@ -42,6 +43,7 @@ class ManifestAppenderTransformer implements Transformer { private byte[] manifestContents = [] private final List>> attributes = [] + @Input List>> getAttributes() { attributes } ManifestAppenderTransformer append(String name, Comparable value) { diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer.groovy index 552ae3439..7be6ef897 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer.groovy @@ -21,11 +21,15 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream -import org.gradle.api.file.FileTreeElement import org.codehaus.plexus.util.IOUtil +import org.gradle.api.file.FileTreeElement +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional -import java.util.jar.* +import java.util.jar.Attributes import java.util.jar.Attributes.Name +import java.util.jar.JarFile +import java.util.jar.Manifest /** * A resource processor that allows the arbitrary addition of attributes to @@ -39,9 +43,13 @@ import java.util.jar.Attributes.Name class ManifestResourceTransformer implements Transformer { // Configuration - private String mainClass + @Optional + @Input + String mainClass - private Map manifestEntries + @Optional + @Input + Map manifestEntries // Fields private boolean manifestDiscovered diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer.groovy index 8c4e63e24..31e1a5452 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer.groovy @@ -21,8 +21,10 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream -import org.gradle.api.file.FileTreeElement import org.codehaus.plexus.util.IOUtil +import org.gradle.api.file.FileTreeElement +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Internal import static groovy.lang.Closure.IDENTITY @@ -115,14 +117,21 @@ import static groovy.lang.Closure.IDENTITY class PropertiesFileTransformer implements Transformer { private static final String PROPERTIES_SUFFIX = '.properties' - // made public for testing - Map propertiesEntries = [:] + private Map propertiesEntries = [:] - // Transformer properties + @Input List paths = [] + + @Input Map> mappings = [:] + + @Input String mergeStrategy = 'first' // latest, append + + @Input String mergeSeparator = ',' + + @Internal Closure keyTransformer = IDENTITY @Override diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/Transformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/Transformer.groovy index 2c610b9c7..630d41301 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/Transformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/Transformer.groovy @@ -20,7 +20,9 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import org.apache.tools.zip.ZipOutputStream +import org.gradle.api.Named import org.gradle.api.file.FileTreeElement +import org.gradle.api.tasks.Internal /** * Modified from org.apache.maven.plugins.shade.resource.ResourceTransformer.java @@ -29,13 +31,18 @@ import org.gradle.api.file.FileTreeElement * @author Charlie Knudsen * @author John Engelman */ -interface Transformer { +trait Transformer implements Named { - boolean canTransformResource(FileTreeElement element) + abstract boolean canTransformResource(FileTreeElement element) - void transform(TransformerContext context) + abstract void transform(TransformerContext context) - boolean hasTransformedResource() + abstract boolean hasTransformedResource() - void modifyOutputStream(ZipOutputStream jos, boolean preserveFileTimestamps) + abstract void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) + + @Internal + String getName() { + return getClass().simpleName + } } diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformer.groovy index d971421f5..092472c0a 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformer.groovy @@ -23,6 +23,7 @@ import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream import org.gradle.api.file.FileTreeElement import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional import org.jdom2.Attribute import org.jdom2.Content import org.jdom2.Document @@ -46,11 +47,14 @@ import org.xml.sax.SAXException class XmlAppendingTransformer implements Transformer { static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance" + @Input boolean ignoreDtd = true + @Optional + @Input String resource - Document doc + private Document doc boolean canTransformResource(FileTreeElement element) { def path = element.relativePath.pathString @@ -112,9 +116,4 @@ class XmlAppendingTransformer implements Transformer { doc = null } - - @Input - String getResource() { - return resource - } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy index 46575b669..0d4423b75 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy @@ -6,6 +6,7 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransf import com.github.jengelman.gradle.plugins.shadow.transformers.XmlAppendingTransformer import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification import spock.lang.Issue +import spock.lang.Unroll import java.util.jar.JarInputStream import java.util.jar.Manifest @@ -597,7 +598,7 @@ two # NOTE: No newline terminates this line/file jis?.close() jis2?.close() } - + def 'Groovy extension module transformer'() { given: def one = buildJar('one.jar') @@ -684,6 +685,46 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( assert props.getProperty('staticExtensionClasses') == 'com.acme.foo.FooStaticExtension,com.acme.bar.SomeStaticExtension' } + @Unroll + def '#transformer should not have deprecated behaviours'() { + given: + if (configuration.contains('test/some.file')) { + file('test/some.file') << 'some content' + } + buildFile << """ + import com.github.jengelman.gradle.plugins.shadow.transformers.${transformer} + + shadowJar { + transform(${transformer})${configuration} + } + """.stripIndent() + + when: + def result = runner.withArguments('shadowJar', '--warning-mode=all', '--stacktrace').build() + + then: + assert output.exists() + if (result.getOutput()) { + result.getOutput().eachLine { assert !it.contains('This behaviour has been deprecated') } + } + + where: + transformer | configuration + 'ApacheLicenseResourceTransformer' | '' + 'ApacheNoticeResourceTransformer' | '' + 'AppendingTransformer' | '' + 'ComponentsXmlResourceTransformer' | '' + 'DontIncludeResourceTransformer' | '' + 'GroovyExtensionModuleTransformer' | '' + 'IncludeResourceTransformer' | '{ resource = "test.file"; file = file("test/some.file") }' + 'Log4j2PluginsCacheFileTransformer' | '' + 'ManifestAppenderTransformer' | '' + // 'ManifestResourceTransformer' | '' + 'PropertiesFileTransformer' | '{ keyTransformer = { it.toLowerCase() } }' + 'ServiceFileTransformer' | '' + 'XmlAppendingTransformer' | '' + } + private String escapedPath(File file) { file.path.replaceAll('\\\\', '\\\\\\\\') }