Skip to content

Commit a791252

Browse files
committed
close #53, do not create source remapped dirs, create dest remapped dirs
1 parent 279572b commit a791252

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

ChangeLog.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ v1.0.0
66
+ Properly configure the ShadowJar task inputs to observe the include/excludes from the `dependencies` block. This
77
allows UP-TO-DATE checking to work properly when changing the `dependencies` rules ([Issue #54](https://github.com/johnrengelman/shadow/issues/54))
88
+ Apply relocation remappings to classes and imports in source project ([Issue #55](https://github.com/johnrengelman/shadow/issues/55))
9+
+ 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))
910

1011
v0.9.0-M5
1112
=========

src/main/groovy/com/github/jengelman/gradle/plugins/shadow/impl/RelocatorRemapper.groovy

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.github.jengelman.gradle.plugins.shadow.impl
2121

2222
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
23+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.RelativeArchivePath
2324
import org.objectweb.asm.commons.Remapper
2425

2526
import java.util.regex.Matcher
@@ -99,4 +100,12 @@ class RelocatorRemapper extends Remapper {
99100
return value
100101
}
101102

103+
String mapPath(String path) {
104+
map(path.substring(0, path.indexOf('.')))
105+
}
106+
107+
String mapPath(RelativeArchivePath path) {
108+
mapPath(path.pathString)
109+
}
110+
102111
}

src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy

+25-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public class ShadowCopyAction implements CopyAction {
183183
}
184184
filteredArchivePaths.each { RelativeArchivePath relativePath ->
185185
if (!relativePath.file) {
186-
visitArchiveDirectory(relativePath)
186+
// visitArchiveDirectory(relativePath)
187187
} else {
188188
visitArchiveFile(relativePath, archive)
189189
}
@@ -219,8 +219,18 @@ public class ShadowCopyAction implements CopyAction {
219219
}
220220
}
221221

222+
private void addParentDirectories(RelativeArchivePath file) {
223+
if (file) {
224+
addParentDirectories(file.parent)
225+
if (!file.file) {
226+
visitArchiveDirectory(file)
227+
}
228+
}
229+
}
230+
222231
private void remapClass(RelativeArchivePath file, ZipFile archive) {
223232
if (file.classFile) {
233+
addParentDirectories(new RelativeArchivePath(new ZipEntry(remapper.mapPath(file) + '.class'), null))
224234
remapClass(archive.getInputStream(file.entry), file.pathString)
225235
}
226236
}
@@ -253,7 +263,7 @@ public class ShadowCopyAction implements CopyAction {
253263
byte[] renamedClass = cw.toByteArray()
254264

255265
// Need to take the .class off for remapping evaluation
256-
String mappedName = remapper.map(path.substring(0, path.indexOf('.')))
266+
String mappedName = remapper.mapPath(path)
257267

258268
try {
259269
// 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 {
266276
}
267277

268278
private void copyArchiveEntry(RelativeArchivePath archiveFile, ZipFile archive) {
279+
addParentDirectories(archiveFile)
269280
zipOutStr.putNextEntry(archiveFile.entry)
270281
IOUtils.copyLarge(archive.getInputStream(archiveFile.entry), zipOutStr)
271282
zipOutStr.closeEntry()
@@ -311,6 +322,18 @@ public class ShadowCopyAction implements CopyAction {
311322
boolean isClassFile() {
312323
return lastName.endsWith('.class')
313324
}
325+
326+
RelativeArchivePath getParent() {
327+
if (!segments) {
328+
return null
329+
} else if (segments.length == 1) {
330+
return new RelativeArchivePath(new ZipEntry('/'), null)
331+
} else {
332+
//Parent is always a directory so add / to the end of the path
333+
String path = segments[0..-2].join('/') + '/'
334+
return new RelativeArchivePath(new ZipEntry(path), null)
335+
}
336+
}
314337
}
315338

316339
class ArchiveFileTreeElement implements FileTreeElement {

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy

+3-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class RelocationSpec extends PluginSpecification {
137137
])
138138
}
139139

140-
@Issue('SHADOW-55')
140+
@Issue(['SHADOW-55', 'SHADOW-53'])
141141
def "remap class names for relocated files in project source"() {
142142
given:
143143
buildFile << """
@@ -179,10 +179,12 @@ class RelocationSpec extends PluginSpecification {
179179
contains(output, [
180180
'shadow/ShadowTest.class',
181181
'shadow/junit/Test.class',
182+
'shadow/junit'
182183
])
183184

184185
and:
185186
doesNotContain(output, [
187+
'junit/framework',
186188
'junit/framework/Test.class'
187189
])
188190

0 commit comments

Comments
 (0)