Search in sources :

Example 26 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project geronimo-arthur by apache.

the class JibMojo method createOthersLayer.

private FileEntriesLayer createOthersLayer() {
    final FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName("Others");
    otherFiles.stream().map(File::toPath).forEach(f -> {
        final AbsoluteUnixPath containerPath = AbsoluteUnixPath.get(project.getBasedir().toPath().relativize(f).toString());
        if (containerPath.toString().contains("..")) {
            throw new IllegalArgumentException("You can only include files included in basedir");
        }
        try {
            if (Files.isDirectory(f)) {
                builder.addEntryRecursive(f, containerPath, (l, c) -> FilePermissions.DEFAULT_FILE_PERMISSIONS, (l, c) -> {
                    try {
                        return getTimestamp(l);
                    } catch (final IOException e) {
                        throw new IllegalStateException(e);
                    }
                });
            } else {
                builder.addEntry(f, containerPath, FilePermissions.DEFAULT_FILE_PERMISSIONS, getTimestamp(f));
            }
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) IOException(java.io.IOException)

Example 27 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jkube by eclipse.

the class JibServiceUtil method layers.

@Nonnull
public static List<FileEntriesLayer> layers(BuildDirs buildDirs, Map<Assembly, List<AssemblyFileEntry>> layers) {
    final List<FileEntriesLayer> fileEntriesLayers = new ArrayList<>();
    for (Map.Entry<Assembly, List<AssemblyFileEntry>> layer : layers.entrySet()) {
        final FileEntriesLayer.Builder fel = FileEntriesLayer.builder();
        final String layerId = layer.getKey().getId();
        final Path outputPath;
        if (StringUtils.isBlank(layerId)) {
            outputPath = buildDirs.getOutputDirectory().toPath();
        } else {
            fel.setName(layerId);
            outputPath = new File(buildDirs.getOutputDirectory(), layerId).toPath();
        }
        for (AssemblyFileEntry afe : layer.getValue()) {
            final Path source = afe.getSource().toPath();
            final AbsoluteUnixPath target = AbsoluteUnixPath.get(StringUtils.prependIfMissing(FilenameUtils.separatorsToUnix(outputPath.relativize(afe.getDest().toPath()).normalize().toString()), "/"));
            final FilePermissions permissions = StringUtils.isNotBlank(afe.getFileMode()) ? FilePermissions.fromOctalString(StringUtils.right(afe.getFileMode(), 3)) : DEFAULT_FILE_PERMISSIONS_PROVIDER.get(source, target);
            fel.addEntry(source, target, permissions);
        }
        fileEntriesLayers.add(fel.build());
    }
    return fileEntriesLayers;
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) Nonnull(javax.annotation.Nonnull)

Example 28 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jkube by eclipse.

the class JibServiceUtilTest method layers_withMultipleLayers_shouldReturnTransformedLayers.

@Test
public void layers_withMultipleLayers_shouldReturnTransformedLayers() throws IOException {
    // Given
    final BuildDirs buildDirs = new BuildDirs("layers-test", JKubeConfiguration.builder().outputDirectory("target/docker").project(JavaProject.builder().baseDirectory(temporaryFolder.getRoot()).build()).build());
    final Map<Assembly, List<AssemblyFileEntry>> originalLayers = new LinkedHashMap<>();
    originalLayers.put(Assembly.builder().id("layer-1").build(), Arrays.asList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("layer-1").resolve("l1.1.txt").toFile()).build(), AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("layer-1").resolve("l1.2.txt").toFile()).build()));
    originalLayers.put(Assembly.builder().build(), Arrays.asList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(new File(buildDirs.getOutputDirectory(), "l2.1.txt")).build(), AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(new File(buildDirs.getOutputDirectory(), "l2.2.txt")).build()));
    // Creates a denormalized path in JDK 8
    originalLayers.put(Assembly.builder().id("jkube-generated-layer-final-artifact").build(), Collections.singletonList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("jkube-generated-layer-final-artifact").resolve("deployments").resolve(".").resolve("edge.case").toFile()).build()));
    // When
    final List<FileEntriesLayer> result = JibServiceUtil.layers(buildDirs, originalLayers);
    // Then
    assertThat(result).hasSize(3).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "layer-1").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/l1.1.txt", "/l1.2.txt")).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/l2.1.txt", "/l2.2.txt")).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "jkube-generated-layer-final-artifact").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/deployments/edge.case")).extracting(FileEntriesLayer::getName).containsExactly("layer-1", "", "jkube-generated-layer-final-artifact");
}
Also used : Arrays(java.util.Arrays) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Port(com.google.cloud.tools.jib.api.buildplan.Port) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) Map(java.util.Map) Arguments(org.eclipse.jkube.kit.config.image.build.Arguments) JavaProject(org.eclipse.jkube.kit.common.JavaProject) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) File(java.io.File) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) List(java.util.List) Rule(org.junit.Rule) Assembly(org.eclipse.jkube.kit.common.Assembly) ImageFormat(com.google.cloud.tools.jib.api.buildplan.ImageFormat) JibServiceUtil.containerFromImageConfiguration(org.eclipse.jkube.kit.service.jib.JibServiceUtil.containerFromImageConfiguration) Collections(java.util.Collections) Mocked(mockit.Mocked) Verifications(mockit.Verifications) TemporaryFolder(org.junit.rules.TemporaryFolder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) List(java.util.List) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 29 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer 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 30 with FileEntriesLayer

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

the class JibSpringBootExtensionTest method testExtendContainerBuildPlan_noFiltering.

@Test
public void testExtendContainerBuildPlan_noFiltering() throws JibPluginExtensionException {
    when(bootJar.getClasspath()).thenReturn(new MockFileCollection(Paths.get("spring-boot-devtools-1.2.3.jar")));
    FileEntriesLayer layer1 = buildLayer("dependencies", Paths.get("spring-boot-devtools-1.2.3.jar"), Paths.get("archive").resolve("bar.zip"));
    FileEntriesLayer layer2 = buildLayer("NOT dependencies", Paths.get("spring-boot-devtools-1.2.3.jar"));
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer1).addLayer(layer2).build();
    ContainerBuildPlan newPlan = new JibSpringBootExtension().extendContainerBuildPlan(buildPlan, properties, Optional.empty(), gradleData, logger);
    assertSame(buildPlan, newPlan);
    verify(logger).log(LogLevel.INFO, "Keeping spring-boot-devtools (if any)");
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Aggregations

FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)154 Test (org.junit.Test)111 Path (java.nio.file.Path)92 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)85 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)55 List (java.util.List)29 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)24 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)23 Map (java.util.Map)23 Files (java.nio.file.Files)21 Predicate (java.util.function.Predicate)20 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)16 Collectors (java.util.stream.Collectors)16 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)13 Paths (java.nio.file.Paths)13 Optional (java.util.Optional)13 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)12 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)12