Skip to content

Commit

Permalink
Use dependency injection for Jib gradle tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sineaggi authored and Clayton Walker committed Jan 23, 2024
1 parent 440bd48 commit ae60d85
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -62,7 +61,6 @@ public class BuildDockerTask extends DefaultTask implements JibTask {
* @return the {@link JibExtension}.
*/
@Nested
@Nullable
public JibExtension getJib() {
return jibExtension;
}
Expand All @@ -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;
}

/**
Expand All @@ -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 =
Expand Down Expand Up @@ -192,10 +193,4 @@ public void buildDocker()
projectProperties.waitForLoggingThread();
}
}

@Override
public BuildDockerTask setJibExtension(JibExtension jibExtension) {
this.jibExtension = jibExtension;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -61,7 +60,6 @@ public class BuildImageTask extends DefaultTask implements JibTask {
* @return the {@link JibExtension}.
*/
@Nested
@Nullable
public JibExtension getJib() {
return jibExtension;
}
Expand All @@ -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;
}

/**
Expand All @@ -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();

Expand Down Expand Up @@ -187,10 +188,4 @@ public void buildImage()
projectProperties.waitForLoggingThread();
}
}

@Override
public BuildImageTask setJibExtension(JibExtension jibExtension) {
this.jibExtension = jibExtension;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -66,7 +65,6 @@ public class BuildTarTask extends DefaultTask implements JibTask {
* @return the {@link JibExtension}.
*/
@Nested
@Nullable
public JibExtension getJib() {
return jibExtension;
}
Expand All @@ -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);
}

/**
Expand All @@ -90,7 +88,7 @@ public void setTargetImage(String targetImage) {
@InputFiles
public FileCollection getInputFiles() {
List<Path> extraDirectories =
Preconditions.checkNotNull(jibExtension).getExtraDirectories().getPaths().stream()
jibExtension.getExtraDirectories().getPaths().stream()
.map(ExtraDirectoryParameters::getFrom)
.collect(Collectors.toList());
return GradleProjectProperties.getInputFiles(
Expand All @@ -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;
}

/**
Expand All @@ -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();

Expand Down Expand Up @@ -209,10 +210,4 @@ public void buildTar()
projectProperties.waitForLoggingThread();
}
}

@Override
public BuildTarTask setJibExtension(JibExtension jibExtension) {
this.jibExtension = jibExtension;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,34 +96,28 @@ public void apply(Project project) {

TaskContainer tasks = project.getTasks();
TaskProvider<BuildImageTask> 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> 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> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

0 comments on commit ae60d85

Please sign in to comment.