From a7912520d3469cb388ea415e41e1afd1dafd072d Mon Sep 17 00:00:00 2001 From: John Engelman Date: Fri, 27 Jun 2014 09:52:02 -0500 Subject: [PATCH] close #53, do not create source remapped dirs, create dest remapped dirs --- ChangeLog.md | 1 + .../shadow/impl/RelocatorRemapper.groovy | 9 +++++++ .../shadow/tasks/ShadowCopyAction.groovy | 27 +++++++++++++++++-- .../plugins/shadow/RelocationSpec.groovy | 4 ++- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 430465e9f..2ceebb5f1 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -6,6 +6,7 @@ v1.0.0 + Properly configure the ShadowJar task inputs to observe the include/excludes from the `dependencies` block. This allows UP-TO-DATE checking to work properly when changing the `dependencies` rules ([Issue #54](https://github.com/johnrengelman/shadow/issues/54)) + Apply relocation remappings to classes and imports in source project ([Issue #55](https://github.com/johnrengelman/shadow/issues/55)) ++ Do not create directories in jar for source of remapped class, created directories in jar for destination of remapped classes ([Issue #53](https://github.com/johnrengelman/shadow/issues/53)) v0.9.0-M5 ========= diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy index 167b8112b..e2008fadd 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy @@ -20,6 +20,7 @@ package com.github.jengelman.gradle.plugins.shadow.impl import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.RelativeArchivePath import org.objectweb.asm.commons.Remapper import java.util.regex.Matcher @@ -99,4 +100,12 @@ class RelocatorRemapper extends Remapper { return value } + String mapPath(String path) { + map(path.substring(0, path.indexOf('.'))) + } + + String mapPath(RelativeArchivePath path) { + mapPath(path.pathString) + } + } 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 f909453ca..a49b7deb3 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 @@ -183,7 +183,7 @@ public class ShadowCopyAction implements CopyAction { } filteredArchivePaths.each { RelativeArchivePath relativePath -> if (!relativePath.file) { - visitArchiveDirectory(relativePath) +// visitArchiveDirectory(relativePath) } else { visitArchiveFile(relativePath, archive) } @@ -219,8 +219,18 @@ public class ShadowCopyAction implements CopyAction { } } + private void addParentDirectories(RelativeArchivePath file) { + if (file) { + addParentDirectories(file.parent) + if (!file.file) { + visitArchiveDirectory(file) + } + } + } + private void remapClass(RelativeArchivePath file, ZipFile archive) { if (file.classFile) { + addParentDirectories(new RelativeArchivePath(new ZipEntry(remapper.mapPath(file) + '.class'), null)) remapClass(archive.getInputStream(file.entry), file.pathString) } } @@ -253,7 +263,7 @@ public class ShadowCopyAction implements CopyAction { byte[] renamedClass = cw.toByteArray() // Need to take the .class off for remapping evaluation - String mappedName = remapper.map(path.substring(0, path.indexOf('.'))) + String mappedName = remapper.mapPath(path) try { // Now we put it back on so the class file is written out with the right extension. @@ -266,6 +276,7 @@ public class ShadowCopyAction implements CopyAction { } private void copyArchiveEntry(RelativeArchivePath archiveFile, ZipFile archive) { + addParentDirectories(archiveFile) zipOutStr.putNextEntry(archiveFile.entry) IOUtils.copyLarge(archive.getInputStream(archiveFile.entry), zipOutStr) zipOutStr.closeEntry() @@ -311,6 +322,18 @@ public class ShadowCopyAction implements CopyAction { boolean isClassFile() { return lastName.endsWith('.class') } + + RelativeArchivePath getParent() { + if (!segments) { + return null + } else if (segments.length == 1) { + return new RelativeArchivePath(new ZipEntry('/'), null) + } else { + //Parent is always a directory so add / to the end of the path + String path = segments[0..-2].join('/') + '/' + return new RelativeArchivePath(new ZipEntry(path), null) + } + } } class ArchiveFileTreeElement implements FileTreeElement { diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy index a5aaa7a77..0b4ca59f2 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy @@ -137,7 +137,7 @@ class RelocationSpec extends PluginSpecification { ]) } - @Issue('SHADOW-55') + @Issue(['SHADOW-55', 'SHADOW-53']) def "remap class names for relocated files in project source"() { given: buildFile << """ @@ -179,10 +179,12 @@ class RelocationSpec extends PluginSpecification { contains(output, [ 'shadow/ShadowTest.class', 'shadow/junit/Test.class', + 'shadow/junit' ]) and: doesNotContain(output, [ + 'junit/framework', 'junit/framework/Test.class' ])