Support Maven's outputTimestamp when repackaging jars and wars

Closes gh-20176
This commit is contained in:
Andy Wilkinson
2020-03-16 16:59:34 +00:00
parent df8c25e213
commit ca202ad59f
11 changed files with 319 additions and 6 deletions

View File

@@ -22,6 +22,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;
@@ -44,6 +45,8 @@ public class JarWriter extends AbstractJarWriter implements AutoCloseable {
private final JarArchiveOutputStream jarOutputStream;
private final FileTime lastModifiedTime;
/**
* Create a new {@link JarWriter} instance.
* @param file the file to write
@@ -62,6 +65,21 @@ public class JarWriter extends AbstractJarWriter implements AutoCloseable {
* @throws FileNotFoundException if the file cannot be found
*/
public JarWriter(File file, LaunchScript launchScript) throws FileNotFoundException, IOException {
this(file, launchScript, null);
}
/**
* Create a new {@link JarWriter} instance.
* @param file the file to write
* @param launchScript an optional launch script to prepend to the front of the jar
* @param lastModifiedTime an optional last modified time to apply to the written
* entries
* @throws IOException if the file cannot be opened
* @throws FileNotFoundException if the file cannot be found
* @since 2.3.0
*/
public JarWriter(File file, LaunchScript launchScript, FileTime lastModifiedTime)
throws FileNotFoundException, IOException {
FileOutputStream fileOutputStream = new FileOutputStream(file);
if (launchScript != null) {
fileOutputStream.write(launchScript.toByteArray());
@@ -69,6 +87,7 @@ public class JarWriter extends AbstractJarWriter implements AutoCloseable {
}
this.jarOutputStream = new JarArchiveOutputStream(fileOutputStream);
this.jarOutputStream.setEncoding("UTF-8");
this.lastModifiedTime = lastModifiedTime;
}
private void setExecutableFilePermission(File file) {
@@ -85,7 +104,11 @@ public class JarWriter extends AbstractJarWriter implements AutoCloseable {
@Override
protected void writeToArchive(ZipEntry entry, EntryWriter entryWriter) throws IOException {
this.jarOutputStream.putArchiveEntry(asJarArchiveEntry(entry));
JarArchiveEntry jarEntry = asJarArchiveEntry(entry);
if (this.lastModifiedTime != null) {
jarEntry.setLastModifiedTime(this.lastModifiedTime);
}
this.jarOutputStream.putArchiveEntry(jarEntry);
if (entryWriter != null) {
entryWriter.write(this.jarOutputStream);
}

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.loader.tools;
import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.FileTime;
import java.util.jar.JarFile;
import org.springframework.util.Assert;
@@ -82,6 +83,22 @@ public class Repackager extends Packager {
* @since 1.3.0
*/
public void repackage(File destination, Libraries libraries, LaunchScript launchScript) throws IOException {
this.repackage(destination, libraries, launchScript, null);
}
/**
* Repackage to the given destination so that it can be launched using '
* {@literal java -jar}'.
* @param destination the destination file (may be the same as the source)
* @param libraries the libraries required to run the archive
* @param launchScript an optional launch script prepended to the front of the jar
* @param lastModifiedTime an optional last modified time to apply to the archive and
* its contents
* @throws IOException if the file cannot be repackaged
* @since 2.3.0
*/
public void repackage(File destination, Libraries libraries, LaunchScript launchScript, FileTime lastModifiedTime)
throws IOException {
Assert.isTrue(destination != null && !destination.isDirectory(), "Invalid destination");
destination = destination.getAbsoluteFile();
File source = getSource();
@@ -97,7 +114,7 @@ public class Repackager extends Packager {
destination.delete();
try {
try (JarFile sourceJar = new JarFile(workingSource)) {
repackage(sourceJar, destination, libraries, launchScript);
repackage(sourceJar, destination, libraries, launchScript, lastModifiedTime);
}
}
finally {
@@ -107,11 +124,14 @@ public class Repackager extends Packager {
}
}
private void repackage(JarFile sourceJar, File destination, Libraries libraries, LaunchScript launchScript)
throws IOException {
try (JarWriter writer = new JarWriter(destination, launchScript)) {
private void repackage(JarFile sourceJar, File destination, Libraries libraries, LaunchScript launchScript,
FileTime lastModifiedTime) throws IOException {
try (JarWriter writer = new JarWriter(destination, launchScript, lastModifiedTime)) {
write(sourceJar, libraries, writer);
}
if (lastModifiedTime != null) {
destination.setLastModified(lastModifiedTime.toMillis());
}
}
private void renameFile(File file, File dest) {