Skip to content

Commit ca4f885

Browse files
committed
Detect backup jar when the package lifecycle is forked
Previously, if a classifier was set and build-image was executed from the command-line, the build will fail as the plugin was unable to find the original jar. This is because it relies on the attached artifacts of the project, and those are not set when package has run as part of a forked lifecycle. This commit makes sure that the backup file is found by convention first, the same way it is done for the target file. Closes spring-projectsgh-26721
1 parent 59b47e3 commit ca4f885

File tree

4 files changed

+90
-6
lines changed

4 files changed

+90
-6
lines changed

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void whenBuildImageIsInvokedOnTheCommandLineWithoutRepackageTheArchiveIsRepackag
9696
}
9797

9898
@TestTemplate
99-
void whenBuildImageIsInvokedWithClassifierWithoutRepackageTheArchiveIsRepackagedOnTheFly(MavenBuild mavenBuild) {
99+
void whenPackageIsInvokedWithClassifierTheOriginalArchiveIsFound(MavenBuild mavenBuild) {
100100
mavenBuild.project("dockerTest", "build-image-classifier")
101101
.goals("package")
102102
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
@@ -115,6 +115,26 @@ void whenBuildImageIsInvokedWithClassifierWithoutRepackageTheArchiveIsRepackaged
115115
});
116116
}
117117

118+
@TestTemplate
119+
void whenBuildImageIsInvokedWithClassifierWithoutRepackageTheOriginalArchiveIsFound(MavenBuild mavenBuild) {
120+
mavenBuild.project("dockerTest", "build-image-fork-classifier")
121+
.goals("spring-boot:build-image")
122+
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
123+
.prepare(this::writeLongNameResource)
124+
.execute((project) -> {
125+
File jar = new File(project, "target/build-image-fork-classifier-0.0.1.BUILD-SNAPSHOT.jar");
126+
assertThat(jar).isFile();
127+
File classifier = new File(project, "target/build-image-fork-classifier-0.0.1.BUILD-SNAPSHOT-test.jar");
128+
assertThat(classifier).doesNotExist();
129+
assertThat(buildLog(project)).contains("Building image")
130+
.contains("docker.io/library/build-image--fork-classifier:0.0.1.BUILD-SNAPSHOT")
131+
.contains("---> Test Info buildpack building")
132+
.contains("---> Test Info buildpack done")
133+
.contains("Successfully built image");
134+
removeImage("build-image-fork-classifier", "0.0.1.BUILD-SNAPSHOT");
135+
});
136+
}
137+
118138
@TestTemplate
119139
void whenBuildImageIsInvokedWithClassifierSourceWithoutRepackageTheArchiveIsRepackagedOnTheFly(
120140
MavenBuild mavenBuild) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.maven.it</groupId>
6+
<artifactId>build-image-fork-classifier</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>@java.version@</maven.compiler.source>
11+
<maven.compiler.target>@java.version@</maven.compiler.target>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>@project.groupId@</groupId>
17+
<artifactId>@project.artifactId@</artifactId>
18+
<version>@project.version@</version>
19+
<configuration>
20+
<classifier>exec</classifier>
21+
<image>
22+
<builder>ghcr.io/spring-io/spring-boot-cnb-test-builder:0.0.1</builder>
23+
</image>
24+
</configuration>
25+
</plugin>
26+
</plugins>
27+
</build>
28+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.test;
18+
19+
public class SampleApplication {
20+
21+
public static void main(String[] args) throws Exception {
22+
System.out.println("Launched");
23+
synchronized(args) {
24+
args.wait(); // Prevent exit"
25+
}
26+
}
27+
28+
}

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ private TarArchive getApplicationContent(Owner owner, Libraries libraries, Image
298298
}
299299

300300
private File getArchiveFile() {
301-
// We can use 'project.getArtifact().getFile()' because that was done in a
302-
// forked lifecycle and is now null
301+
// We can't use 'project.getArtifact().getFile()' because package can be done in a
302+
// forked lifecycle and will be null
303303
File archiveFile = getTargetFile(this.finalName, this.classifier, this.sourceDirectory);
304304
if (!archiveFile.exists()) {
305305
archiveFile = getSourceArtifact(this.classifier).getFile();
@@ -315,9 +315,17 @@ private File getArchiveFile() {
315315
* @return the file to use to back up the original source
316316
*/
317317
private File getBackupFile() {
318-
Artifact source = getSourceArtifact(null);
319-
if (this.classifier != null && !this.classifier.equals(source.getClassifier())) {
320-
return source.getFile();
318+
// We can't use 'project.getAttachedArtifacts()' because package can be done in a
319+
// forked lifecycle and will be null
320+
if (this.classifier != null) {
321+
File backupFile = getTargetFile(this.finalName, null, this.sourceDirectory);
322+
if (backupFile.exists()) {
323+
return backupFile;
324+
}
325+
Artifact source = getSourceArtifact(null);
326+
if (!this.classifier.equals(source.getClassifier())) {
327+
return source.getFile();
328+
}
321329
}
322330
return null;
323331
}

0 commit comments

Comments
 (0)