Add option to customize cache volume names when building an image

This commit adds configuration to the Maven and Gradle plugins to
allow specifying the names of build and launch cache volumes provided
to the CNB builder.

See gh-28292
This commit is contained in:
Julian Liebig
2021-10-11 17:59:45 +02:00
committed by Scott Frederick
parent 9d6a0cfd24
commit dc36346285
8 changed files with 171 additions and 17 deletions

View File

@@ -181,6 +181,11 @@ The value supplied will be passed unvalidated to Docker when creating the builde
| A list of one or more additional tags to apply to the generated image.
|
| `cacheVolumeNames`
|
| Cache volume names that should be used by the builder instead of generating random names.
|
|===
NOTE: The plugin detects the target Java compatibility of the project using the JavaPlugin's `targetCompatibility` property.

View File

@@ -0,0 +1,23 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{gradle-project-version}'
}
bootJar {
mainClass = 'com.example.ExampleApplication'
}
// tag::cacheVolumeNames[]
bootBuildImage {
cacheVolumeNames = [
"build": "example-build-cachevol",
"launch": "example-launch-cachevol"
]
}
// end::cacheVolumeNames[]
task bootBuildImageCacheVolumeNames {
doFirst {
bootBuildImage.cacheVolumeNames.each { type, name -> println "$type=$name" }
}
}

View File

@@ -0,0 +1,21 @@
import org.springframework.boot.gradle.tasks.bundling.BootBuildImage
plugins {
java
id("org.springframework.boot") version "{gradle-project-version}"
}
// tag::cacheVolumeNames[]
tasks.getByName<BootBuildImage>("bootBuildImage") {
cacheVolumeNames = mapOf("build" to "example-build-cachevol",
"launch" to "example-launch-cachevol")
}
// end::cacheVolumeNames[]
tasks.register("bootBuildImageEnvironment") {
doFirst {
for((type, name) in tasks.getByName<BootBuildImage>("bootBuildImage").cacheVolumeNames) {
print(type + "=" + name)
}
}
}

View File

@@ -60,6 +60,7 @@ import org.springframework.util.StringUtils;
* @author Scott Frederick
* @author Rafael Ceccone
* @author Jeroen Meijer
* @author Julian Liebig
* @since 2.3.0
*/
public class BootBuildImage extends DefaultTask {
@@ -98,6 +99,8 @@ public class BootBuildImage extends DefaultTask {
private final ListProperty<String> tags;
private Map<String, String> cacheVolumeNames = new HashMap<>();
private final DockerSpec docker = new DockerSpec();
public BootBuildImage() {
@@ -417,6 +420,41 @@ public class BootBuildImage extends DefaultTask {
this.tags.addAll(tags);
}
/**
* Returns the cache volume names that will be used when building the image.
* @return the cache volume names
*/
@Input
@Optional
public Map<String, String> getCacheVolumeNames() {
return this.cacheVolumeNames;
}
/**
* Sets the cache volume names that will be used when building the image.
* @param cacheVolumeNames the cache volume names
*/
public void setCacheVolumeNames(Map<String, String> cacheVolumeNames) {
this.cacheVolumeNames = cacheVolumeNames;
}
/**
* Add an entry to cache volume names that will be used when building the image.
* @param type the type of the entry
* @param name the name of the entry
*/
public void cacheVolumeName(String type, String name) {
this.cacheVolumeNames.put(type, name);
}
/**
* Adds entries to cache volume names that will be used when building the image.
* @param entries the entries to add to cache volume names
*/
public void cacheVolumeNames(Map<String, String> entries) {
this.cacheVolumeNames.putAll(entries);
}
/**
* Returns the network the build container will connect to.
* @return the network
@@ -499,6 +537,7 @@ public class BootBuildImage extends DefaultTask {
request = customizeBuildpacks(request);
request = customizeBindings(request);
request = customizeTags(request);
request = customizeCacheVolumeNames(request);
request = request.withNetwork(this.network);
return request;
}
@@ -576,6 +615,13 @@ public class BootBuildImage extends DefaultTask {
return request;
}
private BuildRequest customizeCacheVolumeNames(BuildRequest request) {
if (this.cacheVolumeNames != null && !this.cacheVolumeNames.isEmpty()) {
request = request.withCacheVolumeNames(this.cacheVolumeNames);
}
return request;
}
private String translateTargetJavaVersion() {
return this.targetJavaVersion.get().getMajorVersion() + ".*";
}