Skip to content

Commit 6df5e7a

Browse files
committed
Add integration tests for Maven plugin's build-image support
See gh-19830
1 parent 0a9fe65 commit 6df5e7a

File tree

6 files changed

+214
-1
lines changed

6 files changed

+214
-1
lines changed

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@ configurations {
1515

1616
dependencies {
1717
api(platform(project(":spring-boot-project:spring-boot-parent")))
18-
api(project(":spring-boot-project:spring-boot-tools:spring-boot-buildpack-platform"))
1918

2019
compileOnly("org.apache.maven.plugin-tools:maven-plugin-annotations")
2120
compileOnly("org.sonatype.plexus:plexus-build-api")
2221

22+
implementation(project(":spring-boot-project:spring-boot-tools:spring-boot-buildpack-platform"))
2323
implementation(project(":spring-boot-project:spring-boot-tools:spring-boot-loader-tools"))
2424
implementation("org.apache.maven.shared:maven-common-artifact-filters")
2525
implementation("org.apache.maven:maven-plugin-api")
2626

2727
intTestImplementation(platform(project(":spring-boot-project:spring-boot-parent")))
28+
intTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-buildpack-platform"))
29+
intTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
2830
intTestImplementation("org.apache.maven.shared:maven-invoker")
2931
intTestImplementation("org.assertj:assertj-core")
3032
intTestImplementation("org.junit.jupiter:junit-jupiter")
33+
intTestImplementation("org.testcontainers:testcontainers")
3134

3235
optional(platform(project(":spring-boot-project:spring-boot-parent")))
3336
optional("org.apache.maven.plugins:maven-shade-plugin")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-2020 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.springframework.boot.maven;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.junit.jupiter.api.TestTemplate;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.testcontainers.containers.GenericContainer;
25+
import org.testcontainers.containers.wait.strategy.Wait;
26+
27+
import org.springframework.boot.buildpack.platform.docker.DockerApi;
28+
import org.springframework.boot.buildpack.platform.docker.type.ImageName;
29+
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
30+
import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Integration tests for the Maven plugin's image support.
36+
*
37+
* @author Stephane Nicoll
38+
*/
39+
@ExtendWith(MavenBuildExtension.class)
40+
@DisabledIfDockerUnavailable
41+
public class BuildImageTests extends AbstractArchiveIntegrationTests {
42+
43+
@TestTemplate
44+
void whenBuildImageIsInvokedWithoutRepackageTheArchiveIsRepackagedOnTheFly(MavenBuild mavenBuild) {
45+
mavenBuild.project("build-image").goals("package").execute((project) -> {
46+
File jar = new File(project, "target/build-image-0.0.1.BUILD-SNAPSHOT.jar");
47+
assertThat(jar).isFile();
48+
File original = new File(project, "target/build-image-0.0.1.BUILD-SNAPSHOT.jar.original");
49+
assertThat(original).doesNotExist();
50+
assertThat(buildLog(project)).contains("Building image")
51+
.contains("docker.io/library/build-image:0.0.1.BUILD-SNAPSHOT")
52+
.contains("Successfully built image");
53+
ImageReference imageReference = ImageReference.of(ImageName.of("build-image"), "0.0.1.BUILD-SNAPSHOT");
54+
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
55+
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
56+
}
57+
finally {
58+
removeImage(imageReference);
59+
}
60+
});
61+
}
62+
63+
@TestTemplate
64+
void whenBuildImageIsInvokedWihCustomImageName(MavenBuild mavenBuild) {
65+
mavenBuild.project("build-image-custom-name").goals("package").execute((project) -> {
66+
File jar = new File(project, "target/build-image-custom-name-0.0.1.BUILD-SNAPSHOT.jar");
67+
assertThat(jar).isFile();
68+
File original = new File(project, "target/build-image-custom-name-0.0.1.BUILD-SNAPSHOT.jar.original");
69+
assertThat(original).doesNotExist();
70+
assertThat(buildLog(project)).contains("Building image")
71+
.contains("example.com/test/build-image:0.0.1.BUILD-SNAPSHOT").contains("Successfully built image");
72+
ImageReference imageReference = ImageReference.of("example.com/test/build-image:0.0.1.BUILD-SNAPSHOT");
73+
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
74+
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
75+
}
76+
finally {
77+
removeImage(imageReference);
78+
}
79+
});
80+
}
81+
82+
private void removeImage(ImageReference imageReference) {
83+
try {
84+
new DockerApi().image().remove(imageReference, false);
85+
}
86+
catch (IOException ex) {
87+
throw new IllegalStateException("Failed to remove docker image " + imageReference, ex);
88+
}
89+
}
90+
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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-custom-name</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+
<goals>
22+
<goal>build-image</goal>
23+
</goals>
24+
<configuration>
25+
<image>
26+
<name>example.com/test/build-image:${project.version}</name>
27+
</image>
28+
</configuration>
29+
</execution>
30+
</executions>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2020 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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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</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+
<goals>
22+
<goal>build-image</goal>
23+
</goals>
24+
</execution>
25+
</executions>
26+
</plugin>
27+
</plugins>
28+
</build>
29+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2020 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+
}

0 commit comments

Comments
 (0)