Allow Gradle task property to be set with String or enum value

See gh-32769
This commit is contained in:
Scott Frederick 2022-10-20 13:59:54 -05:00
parent c53c8c84b8
commit b78b22b6f6
3 changed files with 19 additions and 3 deletions

View File

@ -68,6 +68,8 @@ public abstract class BootBuildImage extends DefaultTask {
private static final String BUILDPACK_JVM_VERSION_KEY = "BP_JVM_VERSION";
private final Property<PullPolicy> pullPolicy;
private final String projectName;
private final CacheSpec buildCache;
@ -94,6 +96,7 @@ public abstract class BootBuildImage extends DefaultTask {
this.buildCache = getProject().getObjects().newInstance(CacheSpec.class);
this.launchCache = getProject().getObjects().newInstance(CacheSpec.class);
this.docker = getProject().getObjects().newInstance(DockerSpec.class);
this.pullPolicy = getProject().getObjects().property(PullPolicy.class);
}
/**
@ -175,7 +178,17 @@ public abstract class BootBuildImage extends DefaultTask {
@Input
@Optional
@Option(option = "pullPolicy", description = "The image pull policy")
public abstract Property<String> getPullPolicy();
public Property<PullPolicy> getPullPolicy() {
return this.pullPolicy;
}
/**
* Sets image pull policy that will be used when building the image.
* @param pullPolicy the pull policy to use
*/
public void setPullPolicy(String pullPolicy) {
getPullPolicy().set(PullPolicy.valueOf(pullPolicy));
}
/**
* Whether the built image should be pushed to a registry.
@ -342,7 +355,7 @@ public abstract class BootBuildImage extends DefaultTask {
}
private BuildRequest customizePullPolicy(BuildRequest request) {
PullPolicy pullPolicy = getPullPolicy().map(PullPolicy::valueOf).getOrNull();
PullPolicy pullPolicy = getPullPolicy().getOrNull();
if (pullPolicy != null) {
request = request.withPullPolicy(pullPolicy);
}

View File

@ -216,7 +216,7 @@ class BootBuildImageTests {
@Test
void whenPullPolicyIsConfiguredThenRequestHasPullPolicy() {
this.buildImage.getPullPolicy().set(PullPolicy.NEVER.toString());
this.buildImage.getPullPolicy().set(PullPolicy.NEVER);
assertThat(this.buildImage.createRequest().getPullPolicy()).isEqualTo(PullPolicy.NEVER);
}

View File

@ -1,3 +1,5 @@
import org.springframework.boot.buildpack.platform.build.PullPolicy;
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
@ -12,4 +14,5 @@ targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
pullPolicy = PullPolicy.ALWAYS
}