Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dependency injection in tasks #3897

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
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 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
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 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
Original file line number Diff line number Diff line change
Expand Up @@ -96,46 +96,33 @@ 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(SKAFFOLD_FILES_TASK_V2_NAME, FilesTaskV2.class)
.configure(task -> task.setJibExtension(jibExtension));
tasks
.register(SKAFFOLD_INIT_TASK_NAME, InitTask.class)
.configure(task -> 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, jibExtension);
tasks.register(SKAFFOLD_INIT_TASK_NAME, InitTask.class, jibExtension);
TaskProvider<SyncMapTask> syncMapTask =
tasks.register(
SKAFFOLD_SYNC_MAP_TASK_NAME,
SyncMapTask.class,
task -> task.setJibExtension(jibExtension));
tasks.register(SKAFFOLD_SYNC_MAP_TASK_NAME, SyncMapTask.class, jibExtension);

// A check to catch older versions of Jib. This can be removed once we are certain people
// are using Jib 1.3.1 or later.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.cloud.tools.jib.gradle.ExtraDirectoryParameters;
import com.google.cloud.tools.jib.gradle.JibExtension;
import com.google.cloud.tools.jib.plugins.common.SkaffoldFilesOutput;
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -30,7 +29,7 @@
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
Expand All @@ -52,7 +51,12 @@ public class FilesTaskV2 extends DefaultTask {

private final SkaffoldFilesOutput skaffoldFilesOutput = new SkaffoldFilesOutput();

@Nullable private JibExtension jibExtension;
private JibExtension jibExtension;

@Inject
public FilesTaskV2(JibExtension jibExtension) {
this.jibExtension = jibExtension;
}

public FilesTaskV2 setJibExtension(JibExtension jibExtension) {
this.jibExtension = jibExtension;
Expand All @@ -66,7 +70,6 @@ public FilesTaskV2 setJibExtension(JibExtension jibExtension) {
*/
@TaskAction
public void listFiles() throws IOException {
Preconditions.checkNotNull(jibExtension);
Project project = getProject();

// If this is not the root project, add the root project's build.gradle and settings.gradle
Expand Down Expand Up @@ -187,8 +190,6 @@ private void addProjectFiles(Project project) {
* @return the set of project dependencies
*/
private Set<ProjectDependency> findProjectDependencies(Project project) {
Preconditions.checkNotNull(jibExtension);

Set<ProjectDependency> projectDependencies = new HashSet<>();
Deque<Project> projects = new ArrayDeque<>();
projects.push(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

import com.google.cloud.tools.jib.gradle.JibExtension;
import com.google.cloud.tools.jib.plugins.common.SkaffoldInitOutput;
import com.google.common.base.Preconditions;
import java.io.IOException;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -32,7 +31,12 @@
*/
public class InitTask extends DefaultTask {

@Nullable private JibExtension jibExtension;
private JibExtension jibExtension;

@Inject
public InitTask(JibExtension jibExtension) {
this.jibExtension = jibExtension;
}

public InitTask setJibExtension(JibExtension jibExtension) {
this.jibExtension = jibExtension;
Expand All @@ -52,7 +56,7 @@ public void listModulesAndTargets() throws IOException {
return;
}
SkaffoldInitOutput skaffoldInitOutput = new SkaffoldInitOutput();
skaffoldInitOutput.setImage(Preconditions.checkNotNull(jibExtension).getTo().getImage());
skaffoldInitOutput.setImage(jibExtension.getTo().getImage());
if (!project.equals(project.getRootProject())) {
skaffoldInitOutput.setProject(project.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import com.google.cloud.tools.jib.plugins.common.ContainerizingMode;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
import com.google.common.base.Preconditions;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -37,7 +36,12 @@
*/
public class SyncMapTask extends DefaultTask {

@Nullable private JibExtension jibExtension;
private JibExtension jibExtension;

@Inject
public SyncMapTask(JibExtension jibExtension) {
this.jibExtension = jibExtension;
}

public SyncMapTask setJibExtension(JibExtension jibExtension) {
this.jibExtension = jibExtension;
Expand All @@ -47,8 +51,6 @@ public SyncMapTask setJibExtension(JibExtension jibExtension) {
/** Task Action, lists files and container targets. */
@TaskAction
public void listFilesAndTargets() {
Preconditions.checkNotNull(jibExtension);

try (TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider()) {
GradleProjectProperties projectProperties =
GradleProjectProperties.getForProject(
Expand Down
Loading