Skip to content

Commit

Permalink
Merge pull request #31735 from iocanel/gradle-image-build-jib-31698
Browse files Browse the repository at this point in the history
Fix handling of required builders for gradle tasks
  • Loading branch information
aloubyansky authored Mar 16, 2023
2 parents 012085e + 22d250f commit ad68fdb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@

package io.quarkus.gradle.tasks;

import java.util.List;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Optional;

import javax.inject.Inject;

import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

public abstract class ImageBuild extends ImageTask {

public enum Builder {
docker,
jib,
buildpack,
openshift
}

Builder builder = Builder.docker;
Optional<Builder> builder = Optional.empty();

@Option(option = "builder", description = "The container image extension to use for building the image (e.g. docker, jib, buildpack, openshift).")
public void setBuilder(Builder builder) {
this.builder = builder;
this.builder = Optional.of(builder);
}

@Inject
Expand All @@ -33,24 +25,13 @@ public ImageBuild(QuarkusBuildConfiguration buildConfiguration) {

@Override
public Map<String, String> forcedProperties() {
return Map.of("quarkus.container-image.build", "true",
"quarkus.container-image.builder", builder.name());
return builder().map(b -> Map.of(QUARKUS_CONTAINER_IMAGE_BUILD, "true", QUARKUS_CONTAINER_IMAGE_BUILDER, b.name()))
.orElse(Collections.emptyMap());
}

@TaskAction
public void checkRequiredExtensions() {
// Currently forcedDependencies() is not implemented for gradle.
// So, let's give users a meaningful warning message.
String requiredExtension = "quarkus-container-image-" + builder.name();
String requiredDependency = requiredExtension + "-deployment";
List<String> projectDependencies = getProject().getConfigurations().stream().flatMap(c -> c.getDependencies().stream())
.map(d -> d.getName())
.collect(Collectors.toList());

if (!projectDependencies.contains(requiredDependency)) {
getProject().getLogger().warn("Task: {} requires extensions: {}", getName(), requiredDependency);
getProject().getLogger().warn("To add the extensions to the project you can run the following command:");
getProject().getLogger().warn("\tgradle addExtension --extensions={}", requiredExtension);
}
public Optional<Builder> builder() {
return builder
.or(() -> builderFromSystemProperties())
.or(() -> availableBuilders().stream().findFirst());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public ImagePush(QuarkusBuildConfiguration buildConfiguration) {

@Override
public Map<String, String> forcedProperties() {
return Map.of("quarkus.container-image.push", "true");
return Map.of(QUARKUS_CONTAINER_IMAGE_PUSH, "true");
}

@TaskAction
public void checkRequiredExtensions() {
List<String> containerImageExtensions = getProject().getConfigurations().stream()
.flatMap(c -> c.getDependencies().stream())
.map(d -> d.getName())
.filter(n -> n.startsWith("quarkus-container-image"))
.filter(n -> n.startsWith(QUARKUS_CONTAINER_IMAGE_PREFIX))
.map(n -> n.replaceAll("-deployment$", ""))
.collect(Collectors.toList());

List<String> extensions = Arrays.stream(ImageBuild.Builder.values()).map(b -> "quarkus-container-image-" + b.name())
List<String> extensions = Arrays.stream(ImageBuild.Builder.values()).map(b -> QUARKUS_CONTAINER_IMAGE_PREFIX + b.name())
.collect(Collectors.toList());

if (containerImageExtensions.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,94 @@

package io.quarkus.gradle.tasks;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.gradle.api.tasks.StopExecutionException;
import org.gradle.api.tasks.TaskAction;

public abstract class ImageTask extends QuarkusBuildProviderTask {

static final String QUARKUS_CONTAINER_IMAGE_PREFIX = "quarkus-container-image-";
static final String QUARKUS_CONTAINER_IMAGE_BUILD = "quarkus.container-image.build";
static final String QUARKUS_CONTAINER_IMAGE_PUSH = "quarkus.container-image.push";
static final String QUARKUS_CONTAINER_IMAGE_BUILDER = "quarkus.container-image.builder";

static final Map<String, Builder> BUILDERS = new HashMap<>();
static {
for (Builder builder : Builder.values()) {
BUILDERS.put(builder.name(), builder);
}
}

enum Builder {
docker,
jib,
buildpack,
openshift
}

public ImageTask(QuarkusBuildConfiguration buildConfiguration, String description) {
super(buildConfiguration, description);
}

@Override
public Map<String, String> forcedProperties() {
return Map.of("quarkus.container-image.build", "true");
return Map.of(QUARKUS_CONTAINER_IMAGE_BUILD, "true");
}

Optional<Builder> builder() {
return builderFromSystemProperties();
}

Optional<Builder> builderFromSystemProperties() {
return Optional.ofNullable(System.getProperty(QUARKUS_CONTAINER_IMAGE_BUILDER))
.filter(BUILDERS::containsKey)
.map(BUILDERS::get);
}

List<Builder> availableBuilders() {
return getProject().getConfigurations().stream().flatMap(c -> c.getDependencies().stream())
.map(d -> d.getName())
.filter(n -> n.startsWith(QUARKUS_CONTAINER_IMAGE_PREFIX))
.map(n -> n.replace(QUARKUS_CONTAINER_IMAGE_PREFIX, ""))
.filter(BUILDERS::containsKey)
.map(BUILDERS::get)
.collect(Collectors.toList());
}

@TaskAction
public void checkRequiredExtensions() {
// Currently forcedDependencies() is not implemented for gradle.
// So, let's give users a meaningful warning message.
List<Builder> availableBuidlers = availableBuilders();
Optional<Builder> missingBuilder = builder().filter(Predicate.not(availableBuidlers::contains));
missingBuilder.map(builder -> QUARKUS_CONTAINER_IMAGE_PREFIX + builder.name()).ifPresent(missingDependency -> {
getProject().getLogger().warn("Task: {} requires extensions: {}", getName(), missingDependency);
getProject().getLogger().warn("To add the extensions to the project you can run the following command:");
getProject().getLogger().warn("\tgradle addExtension --extensions={}", missingDependency);
abort("Aborting.");
});

if (!missingBuilder.isPresent() && availableBuidlers.isEmpty()) {
getProject().getLogger().warn("Task: {} requires on of extensions: {}", getName(),
Arrays.stream(Builder.values()).map(Builder::name).collect(Collectors.joining(", ", "[", "]")));
getProject().getLogger().warn("To add the extensions to the project you can run the following command:");
getProject().getLogger().warn("\tgradle addExtension --extensions=<extension name>");
abort("Aborting.");
}
}

void abort(String message, Object... args) {
getProject().getLogger().warn(message, args);
getProject().getTasks().stream().filter(t -> t != this).forEach(t -> {
t.setEnabled(false);
});
throw new StopExecutionException();
}
}

0 comments on commit ad68fdb

Please sign in to comment.