Allow image application directory to be configurable

An `applicationDirectory` option on the Maven
`spring-boot:build-image` goal and the Gradle `bootBuildImage` task
can be configured to set the location that will be used to upload
application contents to the builder image, and will contain the
application contents in the generated image.

Closes gh-34786
This commit is contained in:
Scott Frederick
2023-04-07 14:00:00 -05:00
parent df542849be
commit 56bc6d2fa0
16 changed files with 292 additions and 22 deletions

View File

@@ -199,6 +199,12 @@ The values provided to the `tags` option should be full image references in the
The value must be a string in the ISO 8601 instant format, or `now` to use the current date and time.
| A fixed date that enables https://buildpacks.io/docs/features/reproducibility/[build reproducibility].
| `applicationDirectory`
| `--applicationDirectory`
| The path to a directory that application contents will be uploaded to in the builder image.
Application contents will also be in this location in the generated image.
| `/workspace`
|===
NOTE: The plugin detects the target Java compatibility of the project using the JavaPlugin's `targetCompatibility` property.

View File

@@ -270,6 +270,16 @@ public abstract class BootBuildImage extends DefaultTask {
@Option(option = "createdDate", description = "The date to use as the created date of the image")
public abstract Property<String> getCreatedDate();
/**
* Returns the directory that contains application content in the image. When
* {@code null}, a default location will be used.
* @return the application directory
*/
@Input
@Optional
@Option(option = "applicationDirectory", description = "The directory containing application content in the image")
public abstract Property<String> getApplicationDirectory();
/**
* Returns the Docker configuration the builder will use.
* @return docker configuration.
@@ -316,6 +326,7 @@ public abstract class BootBuildImage extends DefaultTask {
request = customizeCaches(request);
request = request.withNetwork(getNetwork().getOrNull());
request = customizeCreatedDate(request);
request = customizeApplicationDirectory(request);
return request;
}
@@ -406,4 +417,12 @@ public abstract class BootBuildImage extends DefaultTask {
return request;
}
private BuildRequest customizeApplicationDirectory(BuildRequest request) {
String applicationDirectory = getApplicationDirectory().getOrNull();
if (applicationDirectory != null) {
return request.withApplicationDirectory(applicationDirectory);
}
return request;
}
}

View File

@@ -143,8 +143,8 @@ class BootBuildImageIntegrationTests {
BuildResult result = this.gradleBuild.build("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT",
"--imageName=example/test-image-cmd",
"--builder=projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2",
"--runImage=projects.registry.vmware.com/springboot/run:tiny-cnb",
"--createdDate=2020-07-01T12:34:56Z");
"--runImage=projects.registry.vmware.com/springboot/run:tiny-cnb", "--createdDate=2020-07-01T12:34:56Z",
"--applicationDirectory=/application");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example/test-image-cmd");
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
@@ -329,6 +329,19 @@ class BootBuildImageIntegrationTests {
removeImages(projectName);
}
@TestTemplate
void buildsImageWithApplicationDirectory() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage");
String projectName = this.gradleBuild.getProjectDir().getName();
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
assertThat(result.getOutput()).contains("---> Test Info buildpack done");
removeImages(projectName);
}
@TestTemplate
void failsWithInvalidCreatedDate() throws IOException {
writeMainClass();

View File

@@ -0,0 +1,14 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
if (project.hasProperty('applyWarPlugin')) {
apply plugin: 'war'
}
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
applicationDirectory = "/application"
}