|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2015 the original author or authors. |
| 2 | + * Copyright 2012-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
24 | 24 | import java.util.HashSet;
|
25 | 25 | import java.util.List;
|
26 | 26 | import java.util.Set;
|
| 27 | +import java.util.jar.JarEntry; |
27 | 28 | import java.util.jar.JarFile;
|
28 | 29 | import java.util.jar.Manifest;
|
29 | 30 |
|
| 31 | +import org.springframework.boot.loader.tools.JarWriter.EntryTransformer; |
| 32 | + |
30 | 33 | /**
|
31 | 34 | * Utility class that can be used to repackage an archive so that it can be executed using
|
32 | 35 | * '{@literal java -jar}'.
|
@@ -189,7 +192,14 @@ public void library(Library library) throws IOException {
|
189 | 192 | writer.writeManifest(buildManifest(sourceJar));
|
190 | 193 | Set<String> seen = new HashSet<String>();
|
191 | 194 | writeNestedLibraries(unpackLibraries, seen, writer);
|
192 |
| - writer.writeEntries(sourceJar); |
| 195 | + if (this.layout instanceof RepackagingLayout) { |
| 196 | + writer.writeEntries(sourceJar, |
| 197 | + new RenamingEntryTransformer(((RepackagingLayout) this.layout) |
| 198 | + .getRepackagedClassesLocation())); |
| 199 | + } |
| 200 | + else { |
| 201 | + writer.writeEntries(sourceJar); |
| 202 | + } |
193 | 203 | writeNestedLibraries(standardLibraries, seen, writer);
|
194 | 204 | if (this.layout.isExecutable()) {
|
195 | 205 | writer.writeLoaderClasses();
|
@@ -293,4 +303,47 @@ private void deleteFile(File file) {
|
293 | 303 | }
|
294 | 304 | }
|
295 | 305 |
|
| 306 | + /** |
| 307 | + * An {@code EntryTransformer} that renames entries by applying a prefix. |
| 308 | + */ |
| 309 | + private static final class RenamingEntryTransformer implements EntryTransformer { |
| 310 | + |
| 311 | + private final String namePrefix; |
| 312 | + |
| 313 | + private RenamingEntryTransformer(String namePrefix) { |
| 314 | + this.namePrefix = namePrefix; |
| 315 | + } |
| 316 | + |
| 317 | + @Override |
| 318 | + public JarEntry transform(JarEntry entry) { |
| 319 | + if (entry.getName().startsWith("META-INF/") |
| 320 | + || entry.getName().startsWith("BOOT-INF/")) { |
| 321 | + return entry; |
| 322 | + } |
| 323 | + JarEntry renamedEntry = new JarEntry(this.namePrefix + entry.getName()); |
| 324 | + renamedEntry.setTime(entry.getTime()); |
| 325 | + renamedEntry.setSize(entry.getSize()); |
| 326 | + renamedEntry.setMethod(entry.getMethod()); |
| 327 | + if (entry.getComment() != null) { |
| 328 | + renamedEntry.setComment(entry.getComment()); |
| 329 | + } |
| 330 | + renamedEntry.setCompressedSize(entry.getCompressedSize()); |
| 331 | + renamedEntry.setCrc(entry.getCrc()); |
| 332 | + if (entry.getCreationTime() != null) { |
| 333 | + renamedEntry.setCreationTime(entry.getCreationTime()); |
| 334 | + } |
| 335 | + if (entry.getExtra() != null) { |
| 336 | + renamedEntry.setExtra(entry.getExtra()); |
| 337 | + } |
| 338 | + if (entry.getLastAccessTime() != null) { |
| 339 | + renamedEntry.setLastAccessTime(entry.getLastAccessTime()); |
| 340 | + } |
| 341 | + if (entry.getLastModifiedTime() != null) { |
| 342 | + renamedEntry.setLastModifiedTime(entry.getLastModifiedTime()); |
| 343 | + } |
| 344 | + return renamedEntry; |
| 345 | + } |
| 346 | + |
| 347 | + } |
| 348 | + |
296 | 349 | }
|
0 commit comments