diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy index aa9d0e194..9871996b0 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy @@ -142,39 +142,39 @@ public class ShadowCopyAction implements CopyAction { return visitedFiles.add(path.pathString) } - private void visitFile(FileCopyDetails fileDetails) { - if (!isArchive(fileDetails)) { + private void visitFile(FileCopyDetails mergedJarFileDetails) { + if (!isArchive(mergedJarFileDetails)) { try { - boolean isClass = (FilenameUtils.getExtension(fileDetails.path) == 'class') + boolean isClass = (FilenameUtils.getExtension(mergedJarFileDetails.path) == 'class') if (!remapper.hasRelocators() || !isClass) { - if (!isTransformable(fileDetails)) { - String path = fileDetails.relativePath.pathString + if (!isTransformable(mergedJarFileDetails)) { + String path = mergedJarFileDetails.relativePath.pathString ZipEntry archiveEntry = new ZipEntry(path) - archiveEntry.setTime(fileDetails.lastModified) - archiveEntry.unixMode = (UnixStat.FILE_FLAG | fileDetails.mode) + archiveEntry.setTime(mergedJarFileDetails.lastModified) + archiveEntry.unixMode = (UnixStat.FILE_FLAG | mergedJarFileDetails.mode) zipOutStr.putNextEntry(archiveEntry) - fileDetails.copyTo(zipOutStr) + mergedJarFileDetails.copyTo(zipOutStr) zipOutStr.closeEntry() } else { - transform(fileDetails) + transform(mergedJarFileDetails) } } else { - remapClass(fileDetails) + remapClass(mergedJarFileDetails) } - recordVisit(fileDetails.relativePath) + recordVisit(mergedJarFileDetails.relativePath) } catch (Exception e) { - throw new GradleException(String.format("Could not add %s to ZIP '%s'.", fileDetails, zipFile), e) + throw new GradleException(String.format("Could not add %s to ZIP '%s'.", mergedJarFileDetails, zipFile), e) } } else { - processArchive(fileDetails) + processArchive(mergedJarFileDetails) } } - private void processArchive(FileCopyDetails fileDetails) { + private void processArchive(FileCopyDetails mergedJarFileDetails) { stats.startJar() - ZipFile archive = new ZipFile(fileDetails.file) + ZipFile archive = new ZipFile(mergedJarFileDetails.file) List archiveElements = archive.entries.collect { - new ArchiveFileTreeElement(new RelativeArchivePath(it, fileDetails)) + new ArchiveFileTreeElement(new RelativeArchivePath(it, mergedJarFileDetails)) } Spec patternSpec = patternSet.getAsSpec() List filteredArchiveElements = archiveElements.findAll { ArchiveFileTreeElement archiveElement -> @@ -182,7 +182,7 @@ public class ShadowCopyAction implements CopyAction { } filteredArchiveElements.each { ArchiveFileTreeElement archiveElement -> if (archiveElement.relativePath.file) { - visitArchiveFile(archiveElement, archive) + visitArchiveFile(archiveElement, mergedJarFileDetails.file) } } archive.close() @@ -196,18 +196,19 @@ public class ShadowCopyAction implements CopyAction { } } - private void visitArchiveFile(ArchiveFileTreeElement archiveFile, ZipFile archive) { + private void visitArchiveFile(ArchiveFileTreeElement archiveFile, File mergedJarFile) { + ZipFile mergedJar = new ZipFile(mergedJarFile) def archiveFilePath = archiveFile.relativePath if (archiveFile.classFile || !isTransformable(archiveFile)) { if (recordVisit(archiveFilePath)) { if (!remapper.hasRelocators() || !archiveFile.classFile) { - copyArchiveEntry(archiveFilePath, archive) + copyArchiveEntry(archiveFilePath, mergedJar) } else { - remapClass(archiveFilePath, archive) + remapClass(archiveFilePath, mergedJar) } } } else { - transform(archiveFile, archive) + transform(mergedJarFile.name, archiveFile, mergedJar) } } @@ -289,17 +290,17 @@ public class ShadowCopyAction implements CopyAction { } } - private void transform(ArchiveFileTreeElement element, ZipFile archive) { - transform(element, archive.getInputStream(element.relativePath.entry)) + private void transform(String jarName, ArchiveFileTreeElement element, ZipFile archive) { + transform(jarName, element, archive.getInputStream(element.relativePath.entry)) } private void transform(FileCopyDetails details) { - transform(details, details.file.newInputStream()) + transform(details.name, details, details.file.newInputStream()) } - private void transform(FileTreeElement element, InputStream is) { + private void transform(String jarName, FileTreeElement element, InputStream is) { String mappedPath = remapper.map(element.relativePath.pathString) - transformers.find { it.canTransformResource(element) }.transform(mappedPath, is, relocators) + transformers.find { it.canTransformResource(element) }.transform(jarName, mappedPath, is, relocators) } private boolean isTransformable(FileTreeElement element) { diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformer.groovy index 3e3e6026a..b850f7510 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicenseResourceTransformer.groovy @@ -43,7 +43,7 @@ class ApacheLicenseResourceTransformer implements Transformer { LICENSE_TXT_PATH.regionMatches(true, 0, path, 0, LICENSE_TXT_PATH.length()) } - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { } 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 ab64897ca..2af3a9998 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 @@ -79,7 +79,7 @@ class ApacheNoticeResourceTransformer implements Transformer { return false } - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { if (entries.isEmpty()) { String year = new SimpleDateFormat("yyyy").format(new Date()) if (!inceptionYear.equals(year)) { 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 cb685b465..4a2bc6f9b 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 @@ -47,7 +47,7 @@ class AppendingTransformer implements Transformer { return false } - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { IOUtil.copy(is, data) data.write('\n'.bytes) 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 9bbbaa94e..ed4487668 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 @@ -48,7 +48,7 @@ class ComponentsXmlResourceTransformer implements Transformer { return COMPONENTS_XML_PATH.equals(path) } - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { Xpp3Dom newDom try { 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 4a01031de..b7f12a2e8 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 @@ -44,7 +44,7 @@ class DontIncludeResourceTransformer implements Transformer { return false } - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { // no op } 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 623942510..2e4911f74 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 @@ -54,7 +54,7 @@ class GroovyExtensionModuleTransformer implements Transformer { } @Override - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { def props = new Properties() props.load(is) props.each { String key, String value -> 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 117b8e5fe..44300bfb6 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 @@ -43,7 +43,7 @@ public class IncludeResourceTransformer implements Transformer { return false } - public void transform(String path, InputStream is, List relocators) { + public void transform(String jarName, String path, InputStream is, List relocators) { // no op } 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 d3bfb14a8..7a5e9f76e 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 @@ -62,7 +62,7 @@ class ManifestResourceTransformer implements Transformer { return false } - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { // We just want to take the first manifest we come across as that's our project's manifest. This is the behavior // now which is situational at best. Right now there is no context passed in with the processing so we cannot // tell what artifact is being processed. 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 b8dfe2275..c4f964e7f 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 @@ -131,7 +131,7 @@ class PropertiesFileTransformer implements Transformer { } @Override - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { Properties props = propertiesEntries[path] if (props == null) { props = new Properties() diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/SamePathFilesTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/SamePathFilesTransformer.groovy new file mode 100644 index 000000000..451f07946 --- /dev/null +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/SamePathFilesTransformer.groovy @@ -0,0 +1,204 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.github.jengelman.gradle.plugins.shadow.transformers +import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator +import org.apache.commons.collections.map.MultiValueMap +import org.apache.commons.io.FilenameUtils +import org.apache.commons.lang.StringUtils +import org.apache.tools.zip.ZipEntry +import org.apache.tools.zip.ZipOutputStream +import org.gradle.api.file.FileTreeElement +import org.gradle.api.specs.Spec +import org.gradle.api.tasks.util.PatternFilterable +import org.gradle.api.tasks.util.PatternSet +import org.gradle.mvn3.org.codehaus.plexus.util.IOUtil + +class SamePathFilesTransformer implements Transformer, PatternFilterable { + + private final PatternSet patternSet = new PatternSet() + + private final def serviceEntires = new MultiValueMap() + + @Override + boolean canTransformResource(FileTreeElement element) { + return patternSet.asSpec.isSatisfiedBy(element) + } + + @Override + void transform(String jarName, String path, InputStream is, List relocators) { + serviceEntires.put(path, new InputStreamWithJarName(is, jarName)) + } + + @Override + boolean hasTransformedResource() { + return true; + } + + @Override + void modifyOutputStream(ZipOutputStream os) { + serviceEntires.each { String path, List streams -> + if (streams.size() == 1) { + os.putNextEntry(new ZipEntry(path)) + IOUtil.copy(streams[0].is, os) + os.closeEntry() + } else { + streams.each { + def newPath = createFilePathWithMergedJarName(path, it.jarName) + os.putNextEntry(new ZipEntry(newPath)) + IOUtil.copy(it.is, os) + os.closeEntry() + } + } + } + } + + private String createFilePathWithMergedJarName(String path, String mergedJarName) { + def extension = FilenameUtils.getExtension(path) + def pathWithoutExtension = path + if (StringUtils.isNotEmpty(extension)) { + pathWithoutExtension = path.substring(0, path.length() - extension.length() - 1) + extension = ".$extension" + } + + def mergedJarExtension = FilenameUtils.getExtension(mergedJarName) + if (StringUtils.isNotEmpty(mergedJarExtension)) { + mergedJarName = mergedJarName.substring(0, mergedJarName.length() - mergedJarExtension.length() - 1) + } + + return "${pathWithoutExtension}_$mergedJarName$extension" + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer include(String... includes) { + patternSet.include(includes) + return this + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer include(Iterable includes) { + patternSet.include(includes) + return this + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer include(Spec includeSpec) { + patternSet.include(includeSpec) + return this + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer include(Closure includeSpec) { + patternSet.include(includeSpec) + return this + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer exclude(String... excludes) { + patternSet.exclude(excludes) + return this + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer exclude(Iterable excludes) { + patternSet.exclude(excludes) + return this + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer exclude(Spec excludeSpec) { + patternSet.exclude(excludeSpec) + return this + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer exclude(Closure excludeSpec) { + patternSet.exclude(excludeSpec) + return this + } + + /** + * {@inheritDoc} + */ + @Override + Set getIncludes() { + return patternSet.includes + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer setIncludes(Iterable includes) { + patternSet.includes = includes + return this + } + + /** + * {@inheritDoc} + */ + @Override + Set getExcludes() { + return patternSet.excludes + } + + /** + * {@inheritDoc} + */ + @Override + SamePathFilesTransformer setExcludes(Iterable excludes) { + patternSet.excludes = excludes + return this + } + + private class InputStreamWithJarName { + + private InputStream is; + private String jarName; + + InputStreamWithJarName(InputStream is, String jarName) { + this.is = is + this.jarName = jarName + } + } +} diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.groovy index 69756af8c..27f107f85 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.groovy @@ -66,7 +66,7 @@ class ServiceFileTransformer implements Transformer, PatternFilterable { } @Override - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { serviceEntries[path].append(is) } 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 b81ab83a2..b33b57097 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 @@ -36,7 +36,7 @@ interface Transformer { boolean canTransformResource(FileTreeElement element) - void transform(String path, InputStream is, List relocators) + void transform(String jarName, String path, InputStream is, List relocators) boolean hasTransformedResource() 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 577125c6a..345ed435d 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 @@ -57,7 +57,7 @@ class XmlAppendingTransformer implements Transformer { return false } - void transform(String path, InputStream is, List relocators) { + void transform(String jarName, String path, InputStream is, List relocators) { Document r try { SAXBuilder builder = new SAXBuilder(false) 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 ac05a4a01..cd6911e0d 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 @@ -1,14 +1,11 @@ package com.github.jengelman.gradle.plugins.shadow - import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar -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.XmlAppendingTransformer +import com.github.jengelman.gradle.plugins.shadow.transformers.* import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification import org.gradle.testkit.functional.ExecutionResult import spock.lang.Issue +import java.util.jar.JarFile import java.util.jar.JarInputStream import java.util.jar.Manifest @@ -623,6 +620,100 @@ class TransformerSpec extends PluginSpecification { assert props.getProperty('staticExtensionClasses') == 'com.acme.foo.FooStaticExtension,com.acme.bar.SomeStaticExtension' } + def 'same file transformer'() { + given: + def one = buildJar('one.jar') + .insertFile('fileInJar.txt', 'This is file one content') + .insertFile('toExclude', 'First content to exclude') + .write() + + def two = buildJar('two.jar') + .insertFile('fileInJar.txt', 'This is file two content') + .insertFile('toExclude', 'Second content to exclude') + .write() + + buildFile << """ + |task shadow(type: ${ShadowJar.name}) { + | destinationDir = new File(buildDir, 'libs') + | baseName = 'shadow' + | from('${escapedPath(one)}') + | from('${escapedPath(two)}') + | transform(${SamePathFilesTransformer.name}) { + | exclude 'toExclude' + | } + |} + """.stripMargin() + + when: + runner.arguments << 'shadow' + def result = runner.run() + + JarFile jf = new JarFile(output) + def paths = "" + jf.entries().each { + paths += "\n$it" + } + + then: + success(result) + assert output.exists() + + and: + doesNotContain(output, ['fileInJar']) + doesNotContain(output, ['toExclude_one', 'toExclude_two']) + contains(output, ['toExclude']) + + and: + def text = getJarFileContents(output, 'fileInJar_one.txt') + def text2 = getJarFileContents(output, 'fileInJar_two.txt') + def text3 = getJarFileContents(output, 'toExclude') + assert text == "This is file one content" + assert text2 == "This is file two content" + assert text3 == "First content to exclude" || text3 == "Second content to exclude" + } + + def 'Groovy same file transformer for file without extension'() { + given: + def one = buildJar('one.jar') + .insertFile('fileInJar', + 'This is file one content'.stripMargin()) + .write() + + def two = buildJar('two.jar') + .insertFile('fileInJar', + 'This is file two content'.stripMargin()) + .write() + + buildFile << """ + |task shadow(type: ${ShadowJar.name}) { + | destinationDir = new File(buildDir, 'libs') + | baseName = 'shadow' + | from('${escapedPath(one)}') + | from('${escapedPath(two)}') + | transform(${SamePathFilesTransformer.name}) + |} + """.stripMargin() + + when: + runner.arguments << 'shadow' + def result = runner.run() + + then: + success(result) + assert output.exists() + + and: + doesNotContain(output, ['fileInJar']) + + and: + def text = getJarFileContents(output, 'fileInJar_one') + assert text == "This is file one content" + + and: + def text2 = getJarFileContents(output, 'fileInJar_two') + assert text2 == "This is file two content" + } + def 'Groovy extension module transformer short syntax'() { given: def one = buildJar('one.jar') diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerParameterTests.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerParameterTests.groovy index 2826ec37a..0be278cc9 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerParameterTests.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerParameterTests.groovy @@ -69,7 +69,7 @@ class ApacheNoticeResourceTransformerParameterTests extends TestCase { try { final ByteArrayInputStream noticeInputStream = new ByteArrayInputStream(noticeText.getBytes()) final List emptyList = Collections.emptyList() - subject.transform(NOTICE_RESOURCE, noticeInputStream, emptyList) + subject.transform('anyJarName', NOTICE_RESOURCE, noticeInputStream, emptyList) } catch (NullPointerException e) { fail("Null pointer should not be thrown when no parameters are set.") diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.groovy index 9d533dc3c..d95fbc4f7 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformerTest.groovy @@ -46,9 +46,11 @@ class ComponentsXmlResourceTransformerTest extends TestCase { XMLUnit.setNormalizeWhitespace(true) - transformer.transform("components-1.xml", getClass().getResourceAsStream("/components-1.xml"), + transformer.transform( + 'jar name to be ignored', "components-1.xml", getClass().getResourceAsStream("/components-1.xml"), Collections. emptyList()) - transformer.transform("components-1.xml", getClass().getResourceAsStream("/components-2.xml"), + transformer.transform( + 'jar name to be ignored', "components-1.xml", getClass().getResourceAsStream("/components-2.xml"), Collections. emptyList()) Diff diff = XMLUnit.compareXML( IOUtil.toString(getClass().getResourceAsStream("/components-expected.xml"), "UTF-8"), diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerSpec.groovy index d0dd61ba9..9721dc1ce 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformerSpec.groovy @@ -52,8 +52,8 @@ class PropertiesFileTransformerSpec extends TransformerSpecSupport { when: if (transformer.canTransformResource(element)) { - transformer.transform(path, toInputStream(toProperties(input1)), []) - transformer.transform(path, toInputStream(toProperties(input2)), []) + transformer.transform('jarNameToBeIgnored', path, toInputStream(toProperties(input1)), []) + transformer.transform('jarNameToBeIgnored', path, toInputStream(toProperties(input2)), []) } then: @@ -76,8 +76,8 @@ class PropertiesFileTransformerSpec extends TransformerSpecSupport { when: if (transformer.canTransformResource(element)) { - transformer.transform(path, toInputStream(toProperties(input1)), []) - transformer.transform(path, toInputStream(toProperties(input2)), []) + transformer.transform('jarNameToBeIgnored', path, toInputStream(toProperties(input1)), []) + transformer.transform('jarNameToBeIgnored', path, toInputStream(toProperties(input2)), []) } then: @@ -100,8 +100,8 @@ class PropertiesFileTransformerSpec extends TransformerSpecSupport { when: if (transformer.canTransformResource(element)) { - transformer.transform(path, toInputStream(toProperties(input1)), []) - transformer.transform(path, toInputStream(toProperties(input2)), []) + transformer.transform('jarNameToBeIgnored', path, toInputStream(toProperties(input1)), []) + transformer.transform('jarNameToBeIgnored', path, toInputStream(toProperties(input2)), []) } then: diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/SamePathFilesTransformerSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/SamePathFilesTransformerSpec.groovy new file mode 100644 index 000000000..87741a6cb --- /dev/null +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/SamePathFilesTransformerSpec.groovy @@ -0,0 +1,38 @@ +package com.github.jengelman.gradle.plugins.shadow.transformers + +import spock.lang.Unroll + +class SamePathFilesTransformerSpec extends TransformerSpecSupport { + + @Unroll + def "Path #path should be #status"() { + given: + SamePathFilesTransformer transformer = new SamePathFilesTransformer() + if (includes) { + transformer.include(includes) + } + if (excludes) { + transformer.exclude(excludes) + } + + when: + def actual = transformer.canTransformResource(getFileElement(path)) + + then: + actual == expected + + where: + path | includes | excludes | expected + 'file.txt' | null | null | true + 'dir/file.txt' | '*' | null | false + 'dir/file.txt' | 'otherFile' | null | false + 'dir/file.txt' | '**' | null | true + 'dir/file.txt' | 'dir/*' | null | true + 'dir/file.txt' | '**' | '**' | false + 'dir/file.txt' | '**' | 'dir/*' | false + 'dir2/file.txt' | '**' | 'dir/*' | true + 'dir/file.txt' | 'dir/file.txt' | 'dir/*' | false + + status = expected ? "included" : "excluded" + } +} diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerSpec.groovy index d91670c13..40c9cd58d 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerSpec.groovy @@ -38,8 +38,8 @@ class ServiceFileTransformerSpec extends TransformerSpecSupport { when: if (transformer.canTransformResource(element)) { - transformer.transform(path, toInputStream(input1), []) - transformer.transform(path, toInputStream(input2), []) + transformer.transform('jarNameToBeIgnored', path, toInputStream(input1), []) + transformer.transform('jarNameToBeIgnored', path, toInputStream(input2), []) } then: