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

See gh-32769
pull/32840/head
Scott Frederick 2 years ago
parent c53c8c84b8
commit b78b22b6f6

@ -68,6 +68,8 @@ public abstract class BootBuildImage extends DefaultTask {
private static final String BUILDPACK_JVM_VERSION_KEY = "BP_JVM_VERSION"; private static final String BUILDPACK_JVM_VERSION_KEY = "BP_JVM_VERSION";
private final Property<PullPolicy> pullPolicy;
private final String projectName; private final String projectName;
private final CacheSpec buildCache; private final CacheSpec buildCache;
@ -94,6 +96,7 @@ public abstract class BootBuildImage extends DefaultTask {
this.buildCache = getProject().getObjects().newInstance(CacheSpec.class); this.buildCache = getProject().getObjects().newInstance(CacheSpec.class);
this.launchCache = getProject().getObjects().newInstance(CacheSpec.class); this.launchCache = getProject().getObjects().newInstance(CacheSpec.class);
this.docker = getProject().getObjects().newInstance(DockerSpec.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 @Input
@Optional @Optional
@Option(option = "pullPolicy", description = "The image pull policy") @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. * 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) { private BuildRequest customizePullPolicy(BuildRequest request) {
PullPolicy pullPolicy = getPullPolicy().map(PullPolicy::valueOf).getOrNull(); PullPolicy pullPolicy = getPullPolicy().getOrNull();
if (pullPolicy != null) { if (pullPolicy != null) {
request = request.withPullPolicy(pullPolicy); request = request.withPullPolicy(pullPolicy);
} }

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

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

Loading…
Cancel
Save