Search in sources :

Example 1 with GradleData

use of com.google.cloud.tools.jib.gradle.extension.GradleData in project jib-extensions by GoogleContainerTools.

the class JibQuarkusExtension method extendContainerBuildPlan.

@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Void> config, GradleData gradleData, ExtensionLogger logger) throws JibPluginExtensionException {
    try {
        logger.log(LogLevel.LIFECYCLE, "Running Quarkus Jib extension");
        readJibConfigurations(gradleData.getProject());
        Project project = gradleData.getProject();
        Path buildDir = project.getBuildDir().toPath();
        Jar jarTask = (Jar) project.getTasks().findByName("jar");
        String jarName = jarTask.getArchiveFile().get().getAsFile().getName();
        Path jar = buildDir.resolve(jarName.replaceAll("\\.jar$", "-runner.jar"));
        if (!Files.isRegularFile(jar)) {
            throw new JibPluginExtensionException(getClass(), jar + " doesn't exist; did you run the Qaurkus Gradle plugin ('quarkusBuild' task)?");
        }
        ContainerBuildPlan.Builder planBuilder = buildPlan.toBuilder();
        planBuilder.setLayers(Collections.emptyList());
        // dependency layers
        addDependencyLayers(project, planBuilder, buildDir.resolve("lib"));
        // Quarkus runner JAR layer
        AbsoluteUnixPath appRootJar = appRoot.resolve("app.jar");
        FileEntriesLayer jarLayer = FileEntriesLayer.builder().setName("quarkus jar").addEntry(jar, appRootJar).build();
        planBuilder.addLayer(jarLayer);
        // Preserve extra directories layers.
        String extraFilesLayerName = JavaContainerBuilder.LayerType.EXTRA_FILES.getName();
        buildPlan.getLayers().stream().filter(layer -> layer.getName().startsWith(extraFilesLayerName)).forEach(planBuilder::addLayer);
        // set entrypoint
        List<String> entrypoint = new ArrayList<>();
        entrypoint.add("java");
        entrypoint.addAll(jvmFlags);
        entrypoint.add("-jar");
        entrypoint.add(appRootJar.toString());
        planBuilder.setEntrypoint(entrypoint);
        return planBuilder.build();
    } catch (IOException ex) {
        throw new JibPluginExtensionException(getClass(), Verify.verifyNotNull(ex.getMessage()), ex);
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) JavaPlugin(org.gradle.api.plugins.JavaPlugin) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Map(java.util.Map) Path(java.nio.file.Path) GradleData(com.google.cloud.tools.jib.gradle.extension.GradleData) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Verify(com.google.common.base.Verify) Files(java.nio.file.Files) Project(org.gradle.api.Project) Predicate(java.util.function.Predicate) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Jar(org.gradle.jvm.tasks.Jar) JibExtension(com.google.cloud.tools.jib.gradle.JibExtension) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) JibGradlePluginExtension(com.google.cloud.tools.jib.gradle.extension.JibGradlePluginExtension) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Collections(java.util.Collections) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Project(org.gradle.api.Project) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Jar(org.gradle.jvm.tasks.Jar) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Example 2 with GradleData

use of com.google.cloud.tools.jib.gradle.extension.GradleData in project jib-extensions by GoogleContainerTools.

the class JibLayerFilterExtension method extendContainerBuildPlan.

@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Configuration> config, GradleData gradleData, ExtensionLogger logger) throws JibPluginExtensionException {
    logger.log(LogLevel.LIFECYCLE, "Running Jib Layer Filter Extension");
    if (!config.isPresent()) {
        logger.log(LogLevel.WARN, "Nothing configured for Jib Layer Filter Extension");
        return buildPlan;
    }
    preparePathMatchersAndLayerBuilders(buildPlan, config.get());
    ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder();
    newPlanBuilder.setLayers(Collections.emptyList());
    @SuppressWarnings("unchecked") List<FileEntriesLayer> originalLayers = (List<FileEntriesLayer>) buildPlan.getLayers();
    // Start filtering original layers.
    for (FileEntriesLayer layer : originalLayers) {
        List<FileEntry> filesToKeep = new ArrayList<>();
        for (FileEntry entry : layer.getEntries()) {
            Optional<String> finalLayerName = determineFinalLayerName(entry, layer.getName());
            // Either keep, move, or delete this FileEntry.
            if (finalLayerName.isPresent()) {
                if (finalLayerName.get().equals(layer.getName())) {
                    filesToKeep.add(entry);
                } else {
                    FileEntriesLayer.Builder targetLayerBuilder = Verify.verifyNotNull(newToLayers.get(finalLayerName.get()));
                    targetLayerBuilder.addEntry(entry);
                }
            }
        }
        if (!filesToKeep.isEmpty()) {
            newPlanBuilder.addLayer(layer.toBuilder().setEntries(filesToKeep).build());
        }
    }
    // Add newly created non-empty to-layers (if any).
    newToLayers.values().stream().map(FileEntriesLayer.Builder::build).filter(layer -> !layer.getEntries().isEmpty()).forEach(newPlanBuilder::addLayer);
    return newPlanBuilder.build();
}
Also used : GradleData(com.google.cloud.tools.jib.gradle.extension.GradleData) Verify(com.google.common.base.Verify) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) JibGradlePluginExtension(com.google.cloud.tools.jib.gradle.extension.JibGradlePluginExtension) Paths(java.nio.file.Paths) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Map(java.util.Map) PathMatcher(java.nio.file.PathMatcher) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Path(java.nio.file.Path) Collections(java.util.Collections) FileSystems(java.nio.file.FileSystems) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Aggregations

ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)2 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)2 GradleData (com.google.cloud.tools.jib.gradle.extension.GradleData)2 JibGradlePluginExtension (com.google.cloud.tools.jib.gradle.extension.JibGradlePluginExtension)2 ExtensionLogger (com.google.cloud.tools.jib.plugins.extension.ExtensionLogger)2 LogLevel (com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel)2 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)2 Verify (com.google.common.base.Verify)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)1 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)1 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)1 LayerObject (com.google.cloud.tools.jib.api.buildplan.LayerObject)1 JibExtension (com.google.cloud.tools.jib.gradle.JibExtension)1