-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42875 from cdsap/image_tasks_compatibility_conf_c…
…ache Compatibility with configuration cache for image tasks
- Loading branch information
Showing
13 changed files
with
370 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 19 additions & 13 deletions
32
...ls/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/ImageBuild.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
|
||
package io.quarkus.gradle.tasks; | ||
|
||
import java.util.Optional; | ||
import static io.quarkus.gradle.tasks.ImageCheckRequirementsTask.QUARKUS_CONTAINER_IMAGE_BUILD; | ||
import static io.quarkus.gradle.tasks.ImageCheckRequirementsTask.QUARKUS_CONTAINER_IMAGE_BUILDER; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.gradle.api.provider.MapProperty; | ||
import org.gradle.api.tasks.options.Option; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
public abstract class ImageBuild extends ImageTask { | ||
|
||
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 = Optional.of(builder); | ||
} | ||
|
||
@Inject | ||
public ImageBuild() { | ||
super("Perform an image build"); | ||
MapProperty<String, String> forcedProperties = extension().forcedPropertiesProperty(); | ||
super("Perform an image build", true); | ||
} | ||
|
||
@TaskAction | ||
public void imageBuild() throws IOException { | ||
Map<String, String> forcedProperties = new HashMap<String, String>(); | ||
File imageBuilder = getBuilderName().get().getAsFile(); | ||
String inputString = new String(Files.readAllBytes(imageBuilder.toPath())); | ||
forcedProperties.put(QUARKUS_CONTAINER_IMAGE_BUILD, "true"); | ||
forcedProperties.put(QUARKUS_CONTAINER_IMAGE_BUILDER, getProject().provider(() -> builder().name())); | ||
forcedProperties.put(QUARKUS_CONTAINER_IMAGE_BUILDER, inputString); | ||
getAdditionalForcedProperties().get().getProperties().putAll(forcedProperties); | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
...-application-plugin/src/main/java/io/quarkus/gradle/tasks/ImageCheckRequirementsTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package io.quarkus.gradle.tasks; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
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.DefaultTask; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.tasks.InputFile; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.PathSensitive; | ||
import org.gradle.api.tasks.PathSensitivity; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import io.quarkus.bootstrap.model.ApplicationModel; | ||
import io.quarkus.gradle.tooling.ToolingUtils; | ||
import io.quarkus.maven.dependency.ArtifactCoords; | ||
|
||
public abstract class ImageCheckRequirementsTask extends DefaultTask { | ||
|
||
@OutputFile | ||
public abstract RegularFileProperty getOutputFile(); | ||
|
||
private static final String DEPLOYMENT_SUFFIX = "-deployment"; | ||
static final String QUARKUS_PREFIX = "quarkus-"; | ||
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"; | ||
|
||
@InputFile | ||
@PathSensitive(PathSensitivity.RELATIVE) | ||
public abstract RegularFileProperty getApplicationModel(); | ||
|
||
static final Map<String, ImageCheckRequirementsTask.Builder> BUILDERS = new HashMap<>(); | ||
static { | ||
for (ImageCheckRequirementsTask.Builder builder : ImageCheckRequirementsTask.Builder.values()) { | ||
BUILDERS.put(builder.name(), builder); | ||
} | ||
} | ||
|
||
enum Builder { | ||
docker, | ||
jib, | ||
buildpack, | ||
openshift | ||
} | ||
|
||
Optional<ImageCheckRequirementsTask.Builder> builderFromSystemProperties() { | ||
return Optional.ofNullable(System.getProperty(QUARKUS_CONTAINER_IMAGE_BUILDER)) | ||
.filter(BUILDERS::containsKey) | ||
.map(BUILDERS::get); | ||
} | ||
|
||
List<ImageCheckRequirementsTask.Builder> availableBuilders() throws IOException { | ||
// This will pick up all dependencies set in the serialized ApplicationModel. | ||
// This means that extensions like quarkus-container-image-openshift via quarkus-openshift are not picked up | ||
// So, let's relax our filters a bit so that we can pickup quarkus-openshift directly (relax the prefix requirement). | ||
ApplicationModel appModel = ToolingUtils.deserializeAppModel(getApplicationModel().get().getAsFile().toPath()); | ||
return appModel.getDependencies() | ||
.stream() | ||
.map(ArtifactCoords::getArtifactId) | ||
.filter(n -> n.startsWith(QUARKUS_CONTAINER_IMAGE_PREFIX) || n.startsWith(QUARKUS_PREFIX)) | ||
.map(n -> n.replace(QUARKUS_CONTAINER_IMAGE_PREFIX, "").replace(QUARKUS_PREFIX, "").replace(DEPLOYMENT_SUFFIX, | ||
"")) | ||
.filter(BUILDERS::containsKey) | ||
.map(BUILDERS::get) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Builder builder() { | ||
return builderFromSystemProperties() | ||
.or(() -> { | ||
try { | ||
return availableBuilders().stream().findFirst(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}) | ||
.orElse(ImageCheckRequirementsTask.Builder.docker); | ||
} | ||
|
||
@TaskAction | ||
public void checkRequiredExtensions() throws IOException { | ||
|
||
// Currently forcedDependencies() is not implemented for gradle. | ||
// So, let's give users a meaningful warning message. | ||
List<Builder> availableBuidlers = availableBuilders(); | ||
Optional<Builder> missingBuilder = Optional.of(builder()).filter(Predicate.not(availableBuidlers::contains)); | ||
missingBuilder.map(builder -> QUARKUS_CONTAINER_IMAGE_PREFIX + builder.name()).ifPresent(missingDependency -> { | ||
throw new GradleException(String.format( | ||
"Task: %s requires extensions: %s. " + | ||
"To add the extensions to the project, you can run the following command:\n" + | ||
"\tgradle addExtension --extensions=%s", | ||
getName(), missingDependency, missingDependency)); | ||
}); | ||
|
||
if (!missingBuilder.isPresent() && availableBuidlers.isEmpty()) { | ||
String availableExtensions = Arrays.stream(Builder.values()) | ||
.map(Builder::name) | ||
.collect(Collectors.joining(", ", "[", "]")); | ||
|
||
throw new GradleException(String.format( | ||
"Task: %s requires one of the extensions: %s. " + | ||
"To add the extensions to the project, you can run the following command:\n" + | ||
"\tgradle addExtension --extensions=<extension name>", | ||
getName(), availableExtensions)); | ||
} | ||
|
||
File outputFile = getOutputFile().get().getAsFile(); | ||
Files.write(outputFile.toPath(), builder().name().getBytes()); | ||
} | ||
} |
39 changes: 10 additions & 29 deletions
39
...ols/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/ImagePush.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,26 @@ | ||
|
||
package io.quarkus.gradle.tasks; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import static io.quarkus.gradle.tasks.ImageCheckRequirementsTask.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.gradle.api.provider.MapProperty; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
public abstract class ImagePush extends ImageTask { | ||
|
||
@Inject | ||
public ImagePush() { | ||
super("Perform an image push"); | ||
MapProperty<String, String> forcedProperties = extension().forcedPropertiesProperty(); | ||
forcedProperties.put(QUARKUS_CONTAINER_IMAGE_BUILD, "true"); | ||
forcedProperties.put(QUARKUS_CONTAINER_IMAGE_PUSH, "true"); | ||
super("Perform an 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_PREFIX)) | ||
.map(n -> n.replaceAll("-deployment$", "")) | ||
.collect(Collectors.toList()); | ||
|
||
List<String> extensions = Arrays.stream(ImageBuild.Builder.values()).map(b -> QUARKUS_CONTAINER_IMAGE_PREFIX + b.name()) | ||
.collect(Collectors.toList()); | ||
|
||
if (containerImageExtensions.isEmpty()) { | ||
getLogger().warn("Task: {} requires a container image extension.", getName()); | ||
getLogger().warn("Available container image exntesions: [{}]", | ||
extensions.stream().collect(Collectors.joining(", "))); | ||
getLogger().warn("To add an extension to the project, you can run one of the commands below:"); | ||
extensions.forEach(e -> { | ||
getLogger().warn("\tgradle addExtension --extensions={}", e); | ||
}); | ||
} | ||
public void imagePush() { | ||
Map<String, String> forcedProperties = new HashMap<String, String>(); | ||
forcedProperties.put(QUARKUS_CONTAINER_IMAGE_BUILD, "true"); | ||
forcedProperties.put(QUARKUS_CONTAINER_IMAGE_PUSH, "true"); | ||
getAdditionalForcedProperties().get().getProperties().putAll(forcedProperties); | ||
} | ||
} |
92 changes: 10 additions & 82 deletions
92
...ols/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/ImageTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,23 @@ | ||
|
||
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.file.RegularFileProperty; | ||
import org.gradle.api.tasks.InputFile; | ||
import org.gradle.api.tasks.PathSensitive; | ||
import org.gradle.api.tasks.PathSensitivity; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import io.quarkus.gradle.dependency.ApplicationDeploymentClasspathBuilder; | ||
import io.quarkus.gradle.tooling.ToolingUtils; | ||
import io.quarkus.runtime.LaunchMode; | ||
|
||
public abstract class ImageTask extends QuarkusBuildTask { | ||
|
||
private static final String DEPLOYMENT_SUFFIX = "-deployment"; | ||
static final String QUARKUS_PREFIX = "quarkus-"; | ||
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); | ||
} | ||
ImageTask(String description, boolean compatible) { | ||
super(description, compatible); | ||
} | ||
|
||
enum Builder { | ||
docker, | ||
jib, | ||
buildpack, | ||
openshift | ||
} | ||
|
||
public ImageTask(String description) { | ||
super(description, false); | ||
} | ||
|
||
public Builder builder() { | ||
return builderFromSystemProperties() | ||
.or(() -> availableBuilders().stream().findFirst()) | ||
.orElse(Builder.docker); | ||
} | ||
|
||
Optional<Builder> builderFromSystemProperties() { | ||
return Optional.ofNullable(System.getProperty(QUARKUS_CONTAINER_IMAGE_BUILDER)) | ||
.filter(BUILDERS::containsKey) | ||
.map(BUILDERS::get); | ||
} | ||
|
||
List<Builder> availableBuilders() { | ||
// This will only pickup direct dependencies and not transitives | ||
// This means that extensions like quarkus-container-image-openshift via quarkus-openshift are not picked up | ||
// So, let's relax our filters a bit so that we can pickup quarkus-openshift directly (relax the prefix requirement). | ||
return getProject().getConfigurations() | ||
.getByName(ToolingUtils.toDeploymentConfigurationName( | ||
ApplicationDeploymentClasspathBuilder.getFinalRuntimeConfigName(LaunchMode.NORMAL))) | ||
.getDependencies().stream() | ||
.map(d -> d.getName()) | ||
.filter(n -> n.startsWith(QUARKUS_CONTAINER_IMAGE_PREFIX) || n.startsWith(QUARKUS_PREFIX)) | ||
.map(n -> n.replace(QUARKUS_CONTAINER_IMAGE_PREFIX, "").replace(QUARKUS_PREFIX, "").replace(DEPLOYMENT_SUFFIX, | ||
"")) | ||
.filter(BUILDERS::containsKey) | ||
.map(BUILDERS::get) | ||
.collect(Collectors.toList()); | ||
} | ||
@InputFile | ||
@PathSensitive(PathSensitivity.RELATIVE) | ||
public abstract RegularFileProperty getBuilderName(); | ||
|
||
@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 = Optional.of(builder()).filter(Predicate.not(availableBuidlers::contains)); | ||
missingBuilder.map(builder -> QUARKUS_CONTAINER_IMAGE_PREFIX + builder.name()).ifPresent(missingDependency -> { | ||
getLogger().warn("Task: {} requires extensions: {}", getName(), missingDependency); | ||
getLogger().warn("To add the extensions to the project you can run the following command:"); | ||
getLogger().warn("\tgradle addExtension --extensions={}", missingDependency); | ||
abort("Aborting."); | ||
}); | ||
|
||
if (!missingBuilder.isPresent() && availableBuidlers.isEmpty()) { | ||
getLogger().warn("Task: {} requires on of extensions: {}", getName(), | ||
Arrays.stream(Builder.values()).map(Builder::name).collect(Collectors.joining(", ", "[", "]"))); | ||
getLogger().warn("To add the extensions to the project you can run the following command:"); | ||
getLogger().warn("\tgradle addExtension --extensions=<extension name>"); | ||
abort("Aborting."); | ||
} | ||
public void imageTask() { | ||
} | ||
} |
Oops, something went wrong.