diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java index 692449db03..da33a401e4 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java @@ -36,12 +36,11 @@ import com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig; import com.google.cloud.tools.jib.plugins.common.globalconfig.InvalidGlobalConfigException; import com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException; -import com.google.common.base.Preconditions; import java.io.IOException; import java.nio.file.Path; import java.util.Optional; import java.util.concurrent.Future; -import javax.annotation.Nullable; +import javax.inject.Inject; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.tasks.Nested; @@ -53,7 +52,7 @@ public class BuildDockerTask extends DefaultTask implements JibTask { private static final String HELPFUL_SUGGESTIONS_PREFIX = "Build to Docker daemon failed"; - @Nullable private JibExtension jibExtension; + private final JibExtension jibExtension; /** * This will call the property {@code "jib"} so that it is the same name as the extension. This @@ -62,7 +61,6 @@ public class BuildDockerTask extends DefaultTask implements JibTask { * @return the {@link JibExtension}. */ @Nested - @Nullable public JibExtension getJib() { return jibExtension; } @@ -74,7 +72,12 @@ public JibExtension getJib() { */ @Option(option = "image", description = "The image reference for the target image") public void setTargetImage(String targetImage) { - Preconditions.checkNotNull(jibExtension).getTo().setImage(targetImage); + jibExtension.getTo().setImage(targetImage); + } + + @Inject + public BuildDockerTask(JibExtension jibExtension) { + this.jibExtension = jibExtension; } /** @@ -90,8 +93,6 @@ public void setTargetImage(String targetImage) { public void buildDocker() throws IOException, BuildStepsExecutionException, CacheDirectoryCreationException, MainClassInferenceException, InvalidGlobalConfigException { - Preconditions.checkNotNull(jibExtension); - // Check deprecated parameters Path dockerExecutable = jibExtension.getDockerClient().getExecutablePath(); boolean isDockerInstalled = @@ -192,10 +193,4 @@ public void buildDocker() projectProperties.waitForLoggingThread(); } } - - @Override - public BuildDockerTask setJibExtension(JibExtension jibExtension) { - this.jibExtension = jibExtension; - return this; - } } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index 49265559a6..7dea81ed97 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -35,12 +35,11 @@ import com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig; import com.google.cloud.tools.jib.plugins.common.globalconfig.InvalidGlobalConfigException; import com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException; -import com.google.common.base.Preconditions; import com.google.common.base.Strings; import java.io.IOException; import java.util.Optional; import java.util.concurrent.Future; -import javax.annotation.Nullable; +import javax.inject.Inject; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.tasks.Nested; @@ -52,7 +51,7 @@ public class BuildImageTask extends DefaultTask implements JibTask { private static final String HELPFUL_SUGGESTIONS_PREFIX = "Build image failed"; - @Nullable private JibExtension jibExtension; + private final JibExtension jibExtension; /** * This will call the property {@code "jib"} so that it is the same name as the extension. This @@ -61,7 +60,6 @@ public class BuildImageTask extends DefaultTask implements JibTask { * @return the {@link JibExtension}. */ @Nested - @Nullable public JibExtension getJib() { return jibExtension; } @@ -73,7 +71,12 @@ public JibExtension getJib() { */ @Option(option = "image", description = "The image reference for the target image") public void setTargetImage(String targetImage) { - Preconditions.checkNotNull(jibExtension).getTo().setImage(targetImage); + jibExtension.getTo().setImage(targetImage); + } + + @Inject + public BuildImageTask(JibExtension jibExtension) { + this.jibExtension = jibExtension; } /** @@ -89,8 +92,6 @@ public void setTargetImage(String targetImage) { public void buildImage() throws IOException, BuildStepsExecutionException, CacheDirectoryCreationException, MainClassInferenceException, InvalidGlobalConfigException { - // Asserts required @Input parameters are not null. - Preconditions.checkNotNull(jibExtension); TaskCommon.disableHttpLogging(); TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider(); @@ -187,10 +188,4 @@ public void buildImage() projectProperties.waitForLoggingThread(); } } - - @Override - public BuildImageTask setJibExtension(JibExtension jibExtension) { - this.jibExtension = jibExtension; - return this; - } } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java index 1a77856b6b..e1f6f0355d 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java @@ -35,14 +35,13 @@ import com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig; import com.google.cloud.tools.jib.plugins.common.globalconfig.InvalidGlobalConfigException; import com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException; -import com.google.common.base.Preconditions; import java.io.IOException; import java.nio.file.Path; import java.util.List; import java.util.Optional; import java.util.concurrent.Future; import java.util.stream.Collectors; -import javax.annotation.Nullable; +import javax.inject.Inject; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.file.FileCollection; @@ -57,7 +56,7 @@ public class BuildTarTask extends DefaultTask implements JibTask { private static final String HELPFUL_SUGGESTIONS_PREFIX = "Building image tarball failed"; - @Nullable private JibExtension jibExtension; + private final JibExtension jibExtension; /** * This will call the property {@code "jib"} so that it is the same name as the extension. This @@ -66,7 +65,6 @@ public class BuildTarTask extends DefaultTask implements JibTask { * @return the {@link JibExtension}. */ @Nested - @Nullable public JibExtension getJib() { return jibExtension; } @@ -78,7 +76,7 @@ public JibExtension getJib() { */ @Option(option = "image", description = "The image reference for the target image") public void setTargetImage(String targetImage) { - Preconditions.checkNotNull(jibExtension).getTo().setImage(targetImage); + jibExtension.getTo().setImage(targetImage); } /** @@ -90,7 +88,7 @@ public void setTargetImage(String targetImage) { @InputFiles public FileCollection getInputFiles() { List extraDirectories = - Preconditions.checkNotNull(jibExtension).getExtraDirectories().getPaths().stream() + jibExtension.getExtraDirectories().getPaths().stream() .map(ExtraDirectoryParameters::getFrom) .collect(Collectors.toList()); return GradleProjectProperties.getInputFiles( @@ -104,7 +102,12 @@ public FileCollection getInputFiles() { */ @OutputFile public String getOutputFile() { - return Preconditions.checkNotNull(jibExtension).getOutputPaths().getTarPath().toString(); + return jibExtension.getOutputPaths().getTarPath().toString(); + } + + @Inject + public BuildTarTask(JibExtension jibExtension) { + this.jibExtension = jibExtension; } /** @@ -120,8 +123,6 @@ public String getOutputFile() { public void buildTar() throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException, MainClassInferenceException, InvalidGlobalConfigException { - // Asserts required @Input parameters are not null. - Preconditions.checkNotNull(jibExtension); TaskCommon.disableHttpLogging(); TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider(); @@ -209,10 +210,4 @@ public void buildTar() projectProperties.waitForLoggingThread(); } } - - @Override - public BuildTarTask setJibExtension(JibExtension jibExtension) { - this.jibExtension = jibExtension; - return this; - } } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibPlugin.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibPlugin.java index 6f2d9d16d1..762f55030a 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibPlugin.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibPlugin.java @@ -96,34 +96,28 @@ public void apply(Project project) { TaskContainer tasks = project.getTasks(); TaskProvider buildImageTask = - tasks.register( - BUILD_IMAGE_TASK_NAME, - BuildImageTask.class, - task -> { - task.setGroup("Jib"); - task.setDescription("Builds a container image to a registry."); - task.setJibExtension(jibExtension); - }); + tasks.register(BUILD_IMAGE_TASK_NAME, BuildImageTask.class, jibExtension); + buildImageTask.configure( + task -> { + task.setGroup("Jib"); + task.setDescription("Builds a container image to a registry."); + }); TaskProvider buildDockerTask = - tasks.register( - BUILD_DOCKER_TASK_NAME, - BuildDockerTask.class, - task -> { - task.setGroup("Jib"); - task.setDescription("Builds a container image to a Docker daemon."); - task.setJibExtension(jibExtension); - }); + tasks.register(BUILD_DOCKER_TASK_NAME, BuildDockerTask.class, jibExtension); + buildDockerTask.configure( + task -> { + task.setGroup("Jib"); + task.setDescription("Builds a container image to a Docker daemon."); + }); TaskProvider buildTarTask = - tasks.register( - BUILD_TAR_TASK_NAME, - BuildTarTask.class, - task -> { - task.setGroup("Jib"); - task.setDescription("Builds a container image to a tarball."); - task.setJibExtension(jibExtension); - }); + tasks.register(BUILD_TAR_TASK_NAME, BuildTarTask.class, jibExtension); + buildTarTask.configure( + task -> { + task.setGroup("Jib"); + task.setDescription("Builds a container image to a tarball."); + }); tasks .register(SKAFFOLD_FILES_TASK_V2_NAME, FilesTaskV2.class) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibTask.java index e742988c7f..bc1432625b 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibTask.java @@ -19,13 +19,4 @@ import org.gradle.api.Task; /** A task with a {@link JibExtension}. */ -public interface JibTask extends Task { - - /** - * Sets the task's {@link JibExtension}. - * - * @param jibExtension the {@link JibExtension} - * @return this - */ - Task setJibExtension(JibExtension jibExtension); -} +public interface JibTask extends Task {}