Skip to content

Commit

Permalink
Add inclusion/exclusion filtering configuration for ExtraDirectories …
Browse files Browse the repository at this point in the history
…in Maven and Gradle (#3171)
  • Loading branch information
chanseokoh committed Mar 31, 2021
1 parent c2cc436 commit 1ea37ee
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;

/** Object in {@link JibExtension} that configures the extra directories. */
public class ExtraDirectoriesParameters {

private final ObjectFactory objects;
private final Project project;

private ListProperty<ExtraDirectoryParameters> paths;
private ExtraDirectoryParametersSpec spec;
private Map<String, String> permissions = Collections.emptyMap();

@Inject
public ExtraDirectoriesParameters(Project project) {
public ExtraDirectoriesParameters(ObjectFactory objects, Project project) {
this.objects = objects;
this.project = project;
paths = project.getObjects().listProperty(ExtraDirectoryParameters.class).empty();
spec = project.getObjects().newInstance(ExtraDirectoryParametersSpec.class, project, paths);
paths = objects.listProperty(ExtraDirectoryParameters.class).empty();
spec = objects.newInstance(ExtraDirectoryParametersSpec.class, project, paths);
}

public void paths(Action<? super ExtraDirectoryParametersSpec> action) {
Expand All @@ -70,12 +73,13 @@ public List<ExtraDirectoryParameters> getPaths() {
List<String> pathStrings = ConfigurationPropertyValidator.parseListProperty(property);
return pathStrings
.stream()
.map(path -> new ExtraDirectoryParameters(project, Paths.get(path), "/"))
.map(path -> new ExtraDirectoryParameters(objects, project, Paths.get(path), "/"))
.collect(Collectors.toList());
}
if (paths.get().isEmpty()) {
return Collections.singletonList(
new ExtraDirectoryParameters(
objects,
project,
project.getProjectDir().toPath().resolve("src").resolve("main").resolve("jib"),
"/"));
Expand All @@ -95,7 +99,7 @@ public void setPaths(Object paths) {
.files(paths)
.getFiles()
.stream()
.map(file -> new ExtraDirectoryParameters(project, file.toPath(), "/"))
.map(file -> new ExtraDirectoryParameters(objects, project, file.toPath(), "/"))
.collect(Collectors.toList()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,35 @@

package com.google.cloud.tools.jib.gradle;

import com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtraDirectoriesConfiguration;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.inject.Inject;
import org.gradle.api.Project;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;

/** Configuration of an extra directory. */
public class ExtraDirectoryParameters {
public class ExtraDirectoryParameters implements ExtraDirectoriesConfiguration {

private Project project;
private Path from = Paths.get("");
private String into = "/";
private ListProperty<String> includes;
private ListProperty<String> excludes;

@Inject
public ExtraDirectoryParameters(Project project) {
public ExtraDirectoryParameters(ObjectFactory objects, Project project) {
this.project = project;
includes = objects.listProperty(String.class).empty();
excludes = objects.listProperty(String.class).empty();
}

public ExtraDirectoryParameters(Project project, Path from, String into) {
this.project = project;
ExtraDirectoryParameters(ObjectFactory objects, Project project, Path from, String into) {
this(objects, project);
this.from = from;
this.into = into;
}
Expand All @@ -48,6 +56,7 @@ public String getFromString() {
return from.toString();
}

@Override
@Internal
public Path getFrom() {
return from;
Expand All @@ -57,6 +66,7 @@ public void setFrom(Object from) {
this.from = project.file(from).toPath();
}

@Override
@Input
public String getInto() {
return into;
Expand All @@ -65,4 +75,26 @@ public String getInto() {
public void setInto(String into) {
this.into = into;
}

@Input
public ListProperty<String> getIncludes() {
return includes;
}

@Input
public ListProperty<String> getExcludes() {
return excludes;
}

@Override
@Internal
public List<String> getIncludesList() {
return includes.get();
}

@Override
@Internal
public List<String> getExcludesList() {
return excludes.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@

package com.google.cloud.tools.jib.gradle;

import com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath;
import com.google.cloud.tools.jib.api.buildplan.FilePermissions;
import com.google.cloud.tools.jib.api.buildplan.ImageFormat;
import com.google.cloud.tools.jib.plugins.common.AuthProperty;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -164,16 +162,14 @@ public String getCreationTime() {
}

@Override
public Map<Path, AbsoluteUnixPath> getExtraDirectories() {
Map<Path, AbsoluteUnixPath> directoryMap = new LinkedHashMap<>();
public List<? extends ExtraDirectoriesConfiguration> getExtraDirectories() {
for (ExtraDirectoryParameters path : jibExtension.getExtraDirectories().getPaths()) {
if (path.getFrom().equals(Paths.get(""))) {
throw new IllegalArgumentException(
"Incomplete extraDirectories.paths configuration; source directory must be set");
}
directoryMap.put(path.getFrom(), AbsoluteUnixPath.get(path.getInto()));
}
return directoryMap;
return jibExtension.getExtraDirectories().getPaths();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator;
import com.google.cloud.tools.jib.plugins.common.PropertyNames;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtensionConfiguration;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtraDirectoriesConfiguration;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -132,6 +133,7 @@ Optional<String> getMode() {

/** Configuration for {@code platform} parameter. */
public static class PlatformParameters implements PlatformConfiguration {

@Nullable @Parameter private String os;
@Nullable @Parameter private String architecture;

Expand All @@ -150,11 +152,8 @@ public Optional<String> getArchitectureName() {
public static class FromConfiguration {

@Nullable @Parameter private String image;

@Nullable @Parameter private String credHelper;

@Parameter private FromAuthConfiguration auth = new FromAuthConfiguration();

@Parameter private List<PlatformParameters> platforms;

/** Constructor for defaults. */
Expand All @@ -170,11 +169,8 @@ public FromConfiguration() {
public static class ToConfiguration {

@Nullable @Parameter private String image;

@Parameter private List<String> tags = Collections.emptyList();

@Nullable @Parameter private String credHelper;

@Parameter private ToAuthConfiguration auth = new ToAuthConfiguration();

public void set(String image) {
Expand All @@ -188,43 +184,27 @@ public static class ContainerParameters {
// Note: `entrypoint` and `args` are @Nullable to handle inheriting values from the base image

@Nullable @Parameter private List<String> entrypoint;

@Parameter private List<String> jvmFlags = Collections.emptyList();

@Parameter private Map<String, String> environment = Collections.emptyMap();

@Parameter private List<String> extraClasspath = Collections.emptyList();

private boolean expandClasspathDependencies;

@Nullable @Parameter private String mainClass;

@Nullable @Parameter private List<String> args;

@Parameter private String format = "Docker";

@Parameter private List<String> ports = Collections.emptyList();

@Parameter private List<String> volumes = Collections.emptyList();

@Parameter private Map<String, String> labels = Collections.emptyMap();

@Parameter private String appRoot = "";

@Nullable @Parameter private String user;

@Nullable @Parameter private String workingDirectory;

@Parameter private String filesModificationTime = "EPOCH_PLUS_SECOND";

@Parameter private String creationTime = "EPOCH";
}

/** Configuration for the {@code extraDirectories} parameter. */
public static class ExtraDirectoriesParameters {

@Parameter private List<ExtraDirectoryParameters> paths = Collections.emptyList();

@Parameter private List<PermissionConfiguration> permissions = Collections.emptyList();

public List<ExtraDirectoryParameters> getPaths() {
Expand All @@ -233,11 +213,12 @@ public List<ExtraDirectoryParameters> getPaths() {
}

/** A bean that configures the source and destination of an extra directory. */
public static class ExtraDirectoryParameters {
public static class ExtraDirectoryParameters implements ExtraDirectoriesConfiguration {

@Parameter private File from = new File("");

@Parameter private String into = "/";
@Parameter private List<String> includes = Collections.emptyList();
@Parameter private List<String> excludes = Collections.emptyList();

// Need default constructor for Maven
public ExtraDirectoryParameters() {}
Expand All @@ -254,6 +235,7 @@ public void set(File path) {
into = "/";
}

@Override
public Path getFrom() {
return from.toPath();
}
Expand All @@ -262,36 +244,41 @@ public void setFrom(File from) {
this.from = from;
}

String getInto() {
@Override
public String getInto() {
return into;
}

@Override
public List<String> getIncludesList() {
return includes;
}

@Override
public List<String> getExcludesList() {
return excludes;
}
}

/** Configuration for the {@code dockerClient} parameter. */
public static class DockerClientParameters {

@Nullable @Parameter private File executable;

@Parameter private Map<String, String> environment = Collections.emptyMap();
}

public static class OutputPathsParameters {

@Nullable @Parameter private File tar;

@Nullable @Parameter private File digest;

@Nullable @Parameter private File imageId;

@Nullable @Parameter private File imageJson;
}

public static class ExtensionParameters implements ExtensionConfiguration {

@Parameter private String implementation = "<extension implementation not configured>";

@Parameter private Map<String, String> properties = Collections.emptyMap();

@Nullable @Parameter private Object configuration;

@Override
Expand Down Expand Up @@ -322,17 +309,14 @@ public Optional<Object> getExtraConfiguration() {
@Parameter(defaultValue = "${plugin}", readonly = true)
protected PluginDescriptor descriptor;

@Parameter private FromConfiguration from = new FromConfiguration();
@Component protected SettingsDecrypter settingsDecrypter;

@Parameter private FromConfiguration from = new FromConfiguration();
@Parameter private ToConfiguration to = new ToConfiguration();

@Parameter private ContainerParameters container = new ContainerParameters();

// this parameter is cloned in FilesMojo
@Parameter private ExtraDirectoriesParameters extraDirectories = new ExtraDirectoriesParameters();

@Parameter private DockerClientParameters dockerClient = new DockerClientParameters();

@Parameter private OutputPathsParameters outputPaths = new OutputPathsParameters();

@Parameter(property = PropertyNames.ALLOW_INSECURE_REGISTRIES)
Expand All @@ -345,9 +329,6 @@ public Optional<Object> getExtraConfiguration() {
private boolean skip;

@Parameter private List<ExtensionParameters> pluginExtensions = Collections.emptyList();

@Component protected SettingsDecrypter settingsDecrypter;

@Inject private Set<JibMavenPluginExtension<?>> injectedPluginExtensions = Collections.emptySet();

protected Set<JibMavenPluginExtension<?>> getInjectedPluginExtensions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

package com.google.cloud.tools.jib.maven;

import com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath;
import com.google.cloud.tools.jib.api.buildplan.FilePermissions;
import com.google.cloud.tools.jib.api.buildplan.ImageFormat;
import com.google.cloud.tools.jib.maven.JibPluginConfiguration.ExtraDirectoryParameters;
import com.google.cloud.tools.jib.plugins.common.AuthProperty;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -169,13 +166,8 @@ public String getCreationTime() {
}

@Override
public Map<Path, AbsoluteUnixPath> getExtraDirectories() {
Map<Path, AbsoluteUnixPath> directoryMap = new LinkedHashMap<>();
for (ExtraDirectoryParameters extraDirectory :
MojoCommon.getExtraDirectories(jibPluginConfiguration)) {
directoryMap.put(extraDirectory.getFrom(), AbsoluteUnixPath.get(extraDirectory.getInto()));
}
return directoryMap;
public List<? extends ExtraDirectoriesConfiguration> getExtraDirectories() {
return MojoCommon.getExtraDirectories(jibPluginConfiguration);
}

@Override
Expand Down

0 comments on commit 1ea37ee

Please sign in to comment.