Skip to content

Commit ae2ff78

Browse files
committed
Revert "Fail fast when finalName is misconfigured"
This reverts commit 263b7c2. See gh-25590
1 parent 263b7c2 commit ae2ff78

File tree

7 files changed

+34
-107
lines changed

7 files changed

+34
-107
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/ImagePackager.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,6 @@
2828
* Utility class that can be used to export a fully packaged archive to an OCI image.
2929
*
3030
* @author Phillip Webb
31-
* @author Scott Frederick
3231
* @since 2.3.0
3332
*/
3433
public class ImagePackager extends Packager {
@@ -38,18 +37,11 @@ public class ImagePackager extends Packager {
3837
* @param source the source file to package
3938
*/
4039
public ImagePackager(File source) {
41-
Assert.notNull(source, "Source file must not be null");
42-
this.source = source.getAbsoluteFile();
43-
if (isAlreadyPackaged()) {
44-
this.source = getBackupFile();
45-
}
46-
Assert.isTrue(this.source.exists() && this.source.isFile(),
47-
"Source '" + this.source + "' must refer to an existing file");
48-
Assert.state(!isAlreadyPackaged(), () -> "Repackaged archive file " + getSource() + " cannot be exported");
40+
super(source, null);
4941
}
5042

5143
/**
52-
* Create a packaged image.
44+
* Create an packaged image.
5345
* @param libraries the contained libraries
5446
* @param exporter the exporter used to write the image
5547
* @throws IOException on IO error
@@ -59,7 +51,10 @@ public void packageImage(Libraries libraries, BiConsumer<ZipEntry, EntryWriter>
5951
}
6052

6153
private void packageImage(Libraries libraries, AbstractJarWriter writer) throws IOException {
62-
try (JarFile sourceJar = new JarFile(getSource())) {
54+
File source = isAlreadyPackaged() ? getBackupFile() : getSource();
55+
Assert.state(source.exists() && source.isFile(), () -> "Unable to read jar file " + source);
56+
Assert.state(!isAlreadyPackaged(source), () -> "Repackaged jar file " + source + " cannot be exported");
57+
try (JarFile sourceJar = new JarFile(source)) {
6358
write(sourceJar, libraries, writer);
6459
}
6560
}

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
* @author Andy Wilkinson
4646
* @author Stephane Nicoll
4747
* @author Madhura Bhave
48-
* @author Scott Frederick
4948
* @since 2.3.0
5049
*/
5150
public abstract class Packager {
@@ -70,22 +69,35 @@ public abstract class Packager {
7069

7170
private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication";
7271

73-
private final List<MainClassTimeoutWarningListener> mainClassTimeoutListeners = new ArrayList<>();
72+
private List<MainClassTimeoutWarningListener> mainClassTimeoutListeners = new ArrayList<>();
7473

7574
private String mainClass;
7675

77-
protected File source;
76+
private final File source;
7877

7978
private Layout layout;
8079

81-
protected LayoutFactory layoutFactory;
80+
private LayoutFactory layoutFactory;
8281

8382
private Layers layers;
8483

8584
private LayersIndex layersIndex;
8685

8786
private boolean includeRelevantJarModeJars = true;
8887

88+
/**
89+
* Create a new {@link Packager} instance.
90+
* @param source the source JAR file to package
91+
* @param layoutFactory the layout factory to use or {@code null}
92+
*/
93+
protected Packager(File source, LayoutFactory layoutFactory) {
94+
Assert.notNull(source, "Source file must not be null");
95+
Assert.isTrue(source.exists() && source.isFile(),
96+
"Source must refer to an existing file, got " + source.getAbsolutePath());
97+
this.source = source.getAbsoluteFile();
98+
this.layoutFactory = layoutFactory;
99+
}
100+
89101
/**
90102
* Add a listener that will be triggered to display a warning if searching for the
91103
* main class takes too long.
@@ -141,18 +153,15 @@ public void setIncludeRelevantJarModeJars(boolean includeRelevantJarModeJars) {
141153
this.includeRelevantJarModeJars = includeRelevantJarModeJars;
142154
}
143155

144-
protected final boolean isAlreadyPackaged() {
156+
protected final boolean isAlreadyPackaged() throws IOException {
145157
return isAlreadyPackaged(this.source);
146158
}
147159

148-
protected final boolean isAlreadyPackaged(File file) {
160+
protected final boolean isAlreadyPackaged(File file) throws IOException {
149161
try (JarFile jarFile = new JarFile(file)) {
150162
Manifest manifest = jarFile.getManifest();
151163
return (manifest != null && manifest.getMainAttributes().getValue(BOOT_VERSION_ATTRIBUTE) != null);
152164
}
153-
catch (IOException ex) {
154-
throw new IllegalStateException("Error reading archive file", ex);
155-
}
156165
}
157166

158167
protected final void write(JarFile sourceJar, Libraries libraries, AbstractJarWriter writer) throws IOException {
@@ -276,7 +285,8 @@ protected String findMainMethod(JarFile source) throws IOException {
276285
* @return the file to use to backup the original source
277286
*/
278287
public final File getBackupFile() {
279-
return new File(this.source.getParentFile(), this.source.getName() + ".original");
288+
File source = getSource();
289+
return new File(source.getParentFile(), source.getName() + ".original");
280290
}
281291

282292
protected final File getSource() {

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,6 @@
3232
* @author Andy Wilkinson
3333
* @author Stephane Nicoll
3434
* @author Madhura Bhave
35-
* @author Scott Frederick
3635
* @since 1.0.0
3736
*/
3837
public class Repackager extends Packager {
@@ -43,16 +42,8 @@ public Repackager(File source) {
4342
this(source, null);
4443
}
4544

46-
/**
47-
* Create a new {@link Repackager} instance.
48-
* @param source the source archive file to package
49-
* @param layoutFactory the layout factory to use or {@code null}
50-
*/
5145
public Repackager(File source, LayoutFactory layoutFactory) {
52-
Assert.notNull(source, "Source file must not be null");
53-
Assert.isTrue(source.exists() && source.isFile(), "Source '" + source + "' must refer to an existing file");
54-
this.source = source.getAbsoluteFile();
55-
this.layoutFactory = layoutFactory;
46+
super(source, layoutFactory);
5647
}
5748

5849
/**

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildImageTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -158,13 +158,6 @@ void failsWithWarPackaging(MavenBuild mavenBuild) {
158158
(project) -> assertThat(buildLog(project)).contains("Executable jar file required for building image"));
159159
}
160160

161-
@TestTemplate
162-
void failsWhenFinalNameIsMisconfigured(MavenBuild mavenBuild) {
163-
mavenBuild.project("build-image-final-name").goals("package")
164-
.executeAndFail((project) -> assertThat(buildLog(project)).contains("final-name.jar.original")
165-
.contains("must refer to an existing file"));
166-
}
167-
168161
private void writeLongNameResource(File project) {
169162
StringBuilder name = new StringBuilder();
170163
new Random().ints('a', 'z' + 1).limit(128).forEach((i) -> name.append((char) i));

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-final-name/pom.xml

Lines changed: 0 additions & 33 deletions
This file was deleted.

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-final-name/src/main/java/org/test/SampleApplication.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -153,8 +153,7 @@ private void buildImage() throws MojoExecutionException {
153153
}
154154

155155
private BuildRequest getBuildRequest(Libraries libraries) {
156-
ImagePackager imagePackager = new ImagePackager(getJarFile());
157-
Function<Owner, TarArchive> content = (owner) -> getApplicationContent(owner, libraries, imagePackager);
156+
Function<Owner, TarArchive> content = (owner) -> getApplicationContent(owner, libraries);
158157
Image image = (this.image != null) ? this.image : new Image();
159158
if (image.name == null && this.imageName != null) {
160159
image.setName(this.imageName);
@@ -168,8 +167,8 @@ private BuildRequest getBuildRequest(Libraries libraries) {
168167
return customize(image.getBuildRequest(this.project.getArtifact(), content));
169168
}
170169

171-
private TarArchive getApplicationContent(Owner owner, Libraries libraries, ImagePackager imagePackager) {
172-
ImagePackager packager = getConfiguredPackager(() -> imagePackager);
170+
private TarArchive getApplicationContent(Owner owner, Libraries libraries) {
171+
ImagePackager packager = getConfiguredPackager(() -> new ImagePackager(getJarFile()));
173172
return new PackagedTarArchive(owner, libraries, packager);
174173
}
175174

0 commit comments

Comments
 (0)