Search in sources :

Example 11 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by GoogleContainerTools.

the class MavenProjectPropertiesTest method testCreateContainerBuilder_correctFiles.

@Test
public void testCreateContainerBuilder_correctFiles() throws URISyntaxException, IOException, InvalidImageReferenceException, CacheDirectoryCreationException {
    ContainerBuilderLayers layers = new ContainerBuilderLayers(setUpBuildContext());
    Path dependenciesPath = getResource("maven/application/dependencies");
    Path applicationDirectory = getResource("maven/application");
    assertThat(layers.dependenciesLayer.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(testRepository.artifactPathOnDisk("com.test", "dependency", "1.0.0"), dependenciesPath.resolve("more/dependency-1.0.0.jar"), dependenciesPath.resolve("another/one/dependency-1.0.0.jar"), dependenciesPath.resolve("libraryA.jar"), dependenciesPath.resolve("libraryB.jar"), dependenciesPath.resolve("library.jarC.jar"));
    assertThat(layers.snapshotsLayer.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(testRepository.artifactPathOnDisk("com.test", "dependencyX", "1.0.0-SNAPSHOT"));
    assertThat(layers.resourcesLayer.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(applicationDirectory.resolve("output/directory"), applicationDirectory.resolve("output/directory/somefile"), applicationDirectory.resolve("output/package"), applicationDirectory.resolve("output/resourceA"), applicationDirectory.resolve("output/resourceB"), applicationDirectory.resolve("output/world"));
    assertThat(layers.classesLayer.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(applicationDirectory.resolve("output/HelloWorld.class"), applicationDirectory.resolve("output/directory"), applicationDirectory.resolve("output/package"), applicationDirectory.resolve("output/package/some.class"), applicationDirectory.resolve("output/some.class"));
    List<FileEntry> allFileEntries = new ArrayList<>();
    allFileEntries.addAll(layers.dependenciesLayer.getEntries());
    allFileEntries.addAll(layers.snapshotsLayer.getEntries());
    allFileEntries.addAll(layers.resourcesLayer.getEntries());
    allFileEntries.addAll(layers.classesLayer.getEntries());
    Set<Instant> modificationTimes = allFileEntries.stream().map(FileEntry::getModificationTime).collect(Collectors.toSet());
    assertThat(modificationTimes).containsExactly(EPOCH_PLUS_32);
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) ArrayList(java.util.ArrayList) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 12 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.

the class JibSpringBootExtension method filterOutDevtools.

@VisibleForTesting
static LayerObject filterOutDevtools(LayerObject layerObject) {
    String dependencyLayerName = JavaContainerBuilder.LayerType.DEPENDENCIES.getName();
    if (!dependencyLayerName.equals(layerObject.getName())) {
        return layerObject;
    }
    FileEntriesLayer layer = (FileEntriesLayer) layerObject;
    Predicate<FileEntry> notDevtoolsJar = fileEntry -> !isDevtoolsJar(fileEntry.getSourceFile().toFile());
    List<FileEntry> newEntries = layer.getEntries().stream().filter(notDevtoolsJar).collect(Collectors.toList());
    return layer.toBuilder().setEntries(newEntries).build();
}
Also used : GradleData(com.google.cloud.tools.jib.gradle.extension.GradleData) Project(org.gradle.api.Project) Predicate(java.util.function.Predicate) 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) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) File(java.io.File) List(java.util.List) Task(org.gradle.api.Task) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) UnknownTaskException(org.gradle.api.UnknownTaskException) JibGradlePluginExtension(com.google.cloud.tools.jib.gradle.extension.JibGradlePluginExtension) TaskProvider(org.gradle.api.tasks.TaskProvider) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Map(java.util.Map) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BootJar(org.springframework.boot.gradle.tasks.bundling.BootJar) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 13 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.

the class JibSpringBootExtension method filterOutDevtools.

@VisibleForTesting
static LayerObject filterOutDevtools(LayerObject layerObject) {
    String dependencyLayerName = JavaContainerBuilder.LayerType.DEPENDENCIES.getName();
    if (!dependencyLayerName.equals(layerObject.getName())) {
        return layerObject;
    }
    FileEntriesLayer layer = (FileEntriesLayer) layerObject;
    Predicate<FileEntry> notDevtoolsJar = fileEntry -> !isDevtoolsJar(fileEntry.getSourceFile().toFile());
    List<FileEntry> newEntries = layer.getEntries().stream().filter(notDevtoolsJar).collect(Collectors.toList());
    return layer.toBuilder().setEntries(newEntries).build();
}
Also used : JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) Predicate(java.util.function.Predicate) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) 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) File(java.io.File) List(java.util.List) Plugin(org.apache.maven.model.Plugin) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) MavenProject(org.apache.maven.project.MavenProject) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Map(java.util.Map) MavenData(com.google.cloud.tools.jib.maven.extension.MavenData) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 14 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.

the class JibLayerFilterExtension method extendContainerBuildPlan.

@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Configuration> config, MavenData mavenData, 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);
    ContainerBuildPlan newPlan = newPlanBuilder.build();
    return config.get().isCreateParentDependencyLayers() ? moveParentDepsToNewLayers(newPlan, mavenData, logger) : newPlan;
}
Also used : JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) Dependency(org.eclipse.aether.graph.Dependency) HashMap(java.util.HashMap) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) Singleton(javax.inject.Singleton) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) Map(java.util.Map) PathMatcher(java.nio.file.PathMatcher) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) Named(javax.inject.Named) Path(java.nio.file.Path) Verify(com.google.common.base.Verify) Artifact(org.eclipse.aether.artifact.Artifact) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) ScopeDependencyFilter(org.eclipse.aether.util.filter.ScopeDependencyFilter) List(java.util.List) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) Paths(java.nio.file.Paths) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) MavenData(com.google.cloud.tools.jib.maven.extension.MavenData) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) 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)

Example 15 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.

the class JibOwnershipExtension method applyRulesToFileEntry.

private FileEntry applyRulesToFileEntry(FileEntry entry) {
    String newOwnership = null;
    for (Entry<PathMatcher, String> mapEntry : pathMatchers.entrySet()) {
        PathMatcher matcher = mapEntry.getKey();
        Path pathInContainer = Paths.get(entry.getExtractionPath().toString());
        if (matcher.matches(pathInContainer)) {
            newOwnership = mapEntry.getValue();
        }
    }
    return newOwnership == null ? entry : new FileEntry(entry.getSourceFile(), entry.getExtractionPath(), entry.getPermissions(), entry.getModificationTime(), newOwnership);
}
Also used : Path(java.nio.file.Path) PathMatcher(java.nio.file.PathMatcher) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry)

Aggregations

FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)64 Test (org.junit.Test)45 Path (java.nio.file.Path)39 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)24 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)16 ArrayList (java.util.ArrayList)13 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)10 Blob (com.google.cloud.tools.jib.blob.Blob)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 List (java.util.List)9 BufferedOutputStream (java.io.BufferedOutputStream)8 OutputStream (java.io.OutputStream)8 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)8 PathMatcher (java.nio.file.PathMatcher)7 Instant (java.time.Instant)7 Optional (java.util.Optional)7 Collectors (java.util.stream.Collectors)7 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6