Merge branch '2.4.x'

Closes gh-25701
This commit is contained in:
Scott Frederick
2021-03-19 15:58:14 -05:00
6 changed files with 92 additions and 16 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.util.Assert;
* Utility class that can be used to export a fully packaged archive to an OCI image.
*
* @author Phillip Webb
* @author Scott Frederick
* @since 2.3.0
*/
public class ImagePackager extends Packager {
@@ -38,10 +39,14 @@ public class ImagePackager extends Packager {
*/
public ImagePackager(File source) {
super(source, null);
if (isAlreadyPackaged()) {
Assert.isTrue(getBackupFile().exists() && getBackupFile().isFile(),
"Original source '" + getBackupFile() + "' is required for building an image");
}
}
/**
* Create an packaged image.
* Create a packaged image.
* @param libraries the contained libraries
* @param exporter the exporter used to write the image
* @throws IOException on IO error
@@ -52,8 +57,8 @@ public class ImagePackager extends Packager {
private void packageImage(Libraries libraries, AbstractJarWriter writer) throws IOException {
File source = isAlreadyPackaged() ? getBackupFile() : getSource();
Assert.state(source.exists() && source.isFile(), () -> "Unable to read jar file " + source);
Assert.state(!isAlreadyPackaged(source), () -> "Repackaged jar file " + source + " cannot be exported");
Assert.state(!isAlreadyPackaged(source),
() -> "Repackaged archive file " + source + " cannot be used to build an image");
try (JarFile sourceJar = new JarFile(source)) {
write(sourceJar, libraries, writer);
}

View File

@@ -45,6 +45,7 @@ import org.springframework.util.StringUtils;
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Madhura Bhave
* @author Scott Frederick
* @since 2.3.0
*/
public abstract class Packager {
@@ -69,7 +70,7 @@ public abstract class Packager {
private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication";
private List<MainClassTimeoutWarningListener> mainClassTimeoutListeners = new ArrayList<>();
private final List<MainClassTimeoutWarningListener> mainClassTimeoutListeners = new ArrayList<>();
private String mainClass;
@@ -153,15 +154,18 @@ public abstract class Packager {
this.includeRelevantJarModeJars = includeRelevantJarModeJars;
}
protected final boolean isAlreadyPackaged() throws IOException {
protected final boolean isAlreadyPackaged() {
return isAlreadyPackaged(this.source);
}
protected final boolean isAlreadyPackaged(File file) throws IOException {
protected final boolean isAlreadyPackaged(File file) {
try (JarFile jarFile = new JarFile(file)) {
Manifest manifest = jarFile.getManifest();
return (manifest != null && manifest.getMainAttributes().getValue(BOOT_VERSION_ATTRIBUTE) != null);
}
catch (IOException ex) {
throw new IllegalStateException("Error reading archive file", ex);
}
}
protected final void write(JarFile sourceJar, Libraries libraries, AbstractJarWriter writer) throws IOException {
@@ -287,8 +291,7 @@ public abstract class Packager {
* @return the file to use to backup the original source
*/
public final File getBackupFile() {
File source = getSource();
return new File(source.getParentFile(), source.getName() + ".original");
return new File(this.source.getParentFile(), this.source.getName() + ".original");
}
protected final File getSource() {