Skip to content

Commit e49a2da

Browse files
committed
Merge branch '3.3.x'
Closes gh-41662
2 parents 8287cc7 + 8ea6d3c commit e49a2da

File tree

4 files changed

+98
-6
lines changed

4 files changed

+98
-6
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void whenBuildImageIsInvokedOnTheCommandLineWithoutRepackageTheArchiveIsRepackag
9999
}
100100

101101
@TestTemplate
102-
void whenBuildImageIsInvokedWithClassifierWithoutRepackageTheArchiveIsRepackagedOnTheFly(MavenBuild mavenBuild) {
102+
void whenPackageIsInvokedWithClassifierTheOriginalArchiveIsFound(MavenBuild mavenBuild) {
103103
mavenBuild.project("dockerTest", "build-image-classifier")
104104
.goals("package")
105105
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
@@ -118,6 +118,26 @@ void whenBuildImageIsInvokedWithClassifierWithoutRepackageTheArchiveIsRepackaged
118118
});
119119
}
120120

121+
@TestTemplate
122+
void whenBuildImageIsInvokedWithClassifierAndRepackageTheOriginalArchiveIsFound(MavenBuild mavenBuild) {
123+
mavenBuild.project("dockerTest", "build-image-fork-classifier")
124+
.goals("spring-boot:build-image")
125+
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
126+
.prepare(this::writeLongNameResource)
127+
.execute((project) -> {
128+
File jar = new File(project, "target/build-image-fork-classifier-0.0.1.BUILD-SNAPSHOT.jar");
129+
assertThat(jar).isFile();
130+
File classifier = new File(project, "target/build-image-fork-classifier-0.0.1.BUILD-SNAPSHOT-exec.jar");
131+
assertThat(classifier).exists();
132+
assertThat(buildLog(project)).contains("Building image")
133+
.contains("docker.io/library/build-image-fork-classifier:0.0.1.BUILD-SNAPSHOT")
134+
.contains("---> Test Info buildpack building")
135+
.contains("---> Test Info buildpack done")
136+
.contains("Successfully built image");
137+
removeImage("build-image-fork-classifier", "0.0.1.BUILD-SNAPSHOT");
138+
});
139+
}
140+
121141
@TestTemplate
122142
void whenBuildImageIsInvokedWithClassifierSourceWithoutRepackageTheArchiveIsRepackagedOnTheFly(
123143
MavenBuild mavenBuild) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
<executions>
20+
<execution>
21+
<id>repackage</id>
22+
<goals>
23+
<goal>repackage</goal>
24+
</goals>
25+
</execution>
26+
</executions>
27+
<configuration>
28+
<classifier>exec</classifier>
29+
<image>
30+
<builder>ghcr.io/spring-io/spring-boot-cnb-test-builder:0.0.1</builder>
31+
</image>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ private TarArchive getApplicationContent(Owner owner, Libraries libraries, Image
319319
}
320320

321321
private File getArchiveFile() {
322-
// We can use 'project.getArtifact().getFile()' because that was done in a
323-
// forked lifecycle and is now null
322+
// We can't use 'project.getArtifact().getFile()' because package can be done in a
323+
// forked lifecycle and will be null
324324
File archiveFile = getTargetFile(this.finalName, this.classifier, this.sourceDirectory);
325325
if (!archiveFile.exists()) {
326326
archiveFile = getSourceArtifact(this.classifier).getFile();
@@ -336,9 +336,17 @@ private File getArchiveFile() {
336336
* @return the file to use to back up the original source
337337
*/
338338
private File getBackupFile() {
339-
Artifact source = getSourceArtifact(null);
340-
if (this.classifier != null && !this.classifier.equals(source.getClassifier())) {
341-
return source.getFile();
339+
// We can't use 'project.getAttachedArtifacts()' because package can be done in a
340+
// forked lifecycle and will be null
341+
if (this.classifier != null) {
342+
File backupFile = getTargetFile(this.finalName, null, this.sourceDirectory);
343+
if (backupFile.exists()) {
344+
return backupFile;
345+
}
346+
Artifact source = getSourceArtifact(null);
347+
if (!this.classifier.equals(source.getClassifier())) {
348+
return source.getFile();
349+
}
342350
}
343351
return null;
344352
}

0 commit comments

Comments
 (0)