Skip to content

Commit

Permalink
Improve error message with Maven build-image and war packaging
Browse files Browse the repository at this point in the history
Fixes gh-24522
  • Loading branch information
scottfrederick committed Dec 16, 2020
1 parent 2cd1459 commit e4d124d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
@@ -1,11 +1,13 @@
[[build-image]]
== Packaging OCI Images
The plugin can create an https://github.com/opencontainers/image-spec[OCI image] using https://buildpacks.io/[Cloud Native Buildpacks] (CNB).
The plugin can create an https://github.com/opencontainers/image-spec[OCI image] from an executable jar file using https://buildpacks.io/[Cloud Native Buildpacks] (CNB).
Images can be built using the `build-image` goal.

NOTE: For security reasons, images build and run as non-root users.
See the {buildpacks-reference}/reference/spec/platform-api/#users[CNB specification] for more details.

NOTE: The `build-image` goal is not supported with projects using <<repackage, war packaging>>.

The easiest way to get started is to invoke `mvn spring-boot:build-image` on a project.
It is possible to automate the creation of an image whenever the `package` phase is invoked, as shown in the following example:

Expand Down
Expand Up @@ -152,6 +152,12 @@ void failsWhenBuilderFails(MavenBuild mavenBuild) {
.containsPattern("Builder lifecycle '.*' failed with status code"));
}

@TestTemplate
void failsWithWarPackaging(MavenBuild mavenBuild) {
mavenBuild.project("build-image-war-packaging").goals("package").executeAndFail(
(project) -> assertThat(buildLog(project)).contains("Executable jar file required for building image"));
}

private void writeLongNameResource(File project) {
StringBuilder name = new StringBuilder();
new Random().ints('a', 'z' + 1).limit(128).forEach((i) -> name.append((char) i));
Expand Down
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>build-image-war</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>@maven-war-plugin.version@</version>
<configuration>
<archive>
<manifestEntries>
<Not-Used>Foo</Not-Used>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring-framework.version@</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>@jakarta-servlet.version@</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,24 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.test;

public class SampleApplication {

public static void main(String[] args) throws Exception {
}

}
Expand Up @@ -180,7 +180,11 @@ private File getJarFile() {
name.append("-").append(this.classifier);
}
name.append(".jar");
return new File(this.sourceDirectory, name.toString());
File jarFile = new File(this.sourceDirectory, name.toString());
if (!jarFile.exists()) {
throw new IllegalStateException("Executable jar file required for building image");
}
return jarFile;
}

private BuildRequest customize(BuildRequest request) {
Expand Down

0 comments on commit e4d124d

Please sign in to comment.