Search in sources :

Example 6 with Assembly

use of org.eclipse.jkube.kit.common.Assembly 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 7 with Assembly

use of org.eclipse.jkube.kit.common.Assembly 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 8 with Assembly

use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.

the class AssemblyFileSetUtils method processAssemblyFileSet.

/**
 * Will copy files from the provided <code>baseDirectory</code> into
 * <code>outputDirectory/assemblyConfiguration.targetDir[/layer.directory]</code>
 * considering the inclusion and exclusion rules defined in the provided {@link AssemblyFileSet}.
 *
 * @param baseDirectory directory from where to resolve source files.
 * @param outputDirectory directory where files should be output.
 * @param assemblyFileSet fileSet to process.
 * @param layer the layer to which fileSet belongs to.
 * @param assemblyConfiguration configuration for assembly.
 * @return List containing the copied {@link AssemblyFileEntry} for the processed {@link AssemblyFileSet}
 * @throws IOException in case something goes wrong when performing File operations.
 */
@SuppressWarnings("squid:S3864")
@Nonnull
public static List<AssemblyFileEntry> processAssemblyFileSet(File baseDirectory, File outputDirectory, AssemblyFileSet assemblyFileSet, Assembly layer, AssemblyConfiguration assemblyConfiguration) throws IOException {
    final File sourceDirectory = resolveSourceDirectory(baseDirectory, assemblyFileSet);
    Objects.requireNonNull(assemblyConfiguration.getTargetDir(), "Assembly Configuration target dir is required");
    if (!sourceDirectory.exists()) {
        return Collections.emptyList();
    }
    final File targetDirectory;
    if (StringUtils.isNotBlank(layer.getId())) {
        targetDirectory = new File(new File(outputDirectory, layer.getId()), assemblyConfiguration.getTargetDir());
    } else {
        targetDirectory = new File(outputDirectory, assemblyConfiguration.getTargetDir());
    }
    final File destinationDirectory;
    if (assemblyFileSet.getOutputDirectory() == null) {
        destinationDirectory = new File(targetDirectory, sourceDirectory.getName());
    } else if (assemblyFileSet.getOutputDirectory().isAbsolute()) {
        destinationDirectory = assemblyFileSet.getOutputDirectory();
    } else if (assemblyFileSet.getOutputDirectory().getPath().equals(PATH_TO_SELF)) {
        destinationDirectory = targetDirectory;
    } else {
        destinationDirectory = targetDirectory.toPath().resolve(assemblyFileSet.getOutputDirectory().getPath()).toFile();
    }
    final List<String> includes = Optional.ofNullable(assemblyFileSet.getIncludes()).filter(i -> !i.isEmpty()).orElse(Collections.singletonList(PATH_TO_SELF));
    final List<AssemblyFileEntry> allEntries = new ArrayList<>();
    for (String include : includes) {
        final String effectiveInclude = isSelfPath(include) ? "**" : include;
        allEntries.addAll(processInclude(sourceDirectory.toPath(), effectiveInclude, destinationDirectory.toPath(), assemblyFileSet));
    }
    return allEntries;
}
Also used : AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) ArrayList(java.util.ArrayList) AssemblyFileSet(org.eclipse.jkube.kit.common.AssemblyFileSet) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Assembly(org.eclipse.jkube.kit.common.Assembly) PathMatcher(java.nio.file.PathMatcher) FileUtil(org.eclipse.jkube.kit.common.util.FileUtil) Optional(java.util.Optional) Path(java.nio.file.Path) Collections(java.util.Collections) Nonnull(javax.annotation.Nonnull) FileSystems(java.nio.file.FileSystems) LinkedHashSet(java.util.LinkedHashSet) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) ArrayList(java.util.ArrayList) File(java.io.File) Nonnull(javax.annotation.Nonnull)

Example 9 with Assembly

use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.

the class JavaExecGeneratorTest method createAssemblyWithNoFatJarShouldAddDefaultFileSets.

@Test
public void createAssemblyWithNoFatJarShouldAddDefaultFileSets() {
    // When
    final AssemblyConfiguration result = new JavaExecGenerator(generatorContext).createAssembly();
    // Then
    assertThat(result).hasFieldOrPropertyWithValue("excludeFinalOutputArtifact", false).extracting(AssemblyConfiguration::getLayers).asList().hasSize(1).first().asInstanceOf(InstanceOfAssertFactories.type(Assembly.class)).extracting(Assembly::getFileSets).asList().hasSize(2);
}
Also used : AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) Assembly(org.eclipse.jkube.kit.common.Assembly) Test(org.junit.Test)

Example 10 with Assembly

use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.

the class JavaExecGeneratorTest method createAssemblyWithFatJarShouldAddDefaultFileSetsAndFatJar.

@Test
public void createAssemblyWithFatJarShouldAddDefaultFileSetsAndFatJar(@Mocked FatJarDetector fatJarDetector, @Mocked FatJarDetector.Result fjResult) {
    // Given
    // @formatter:off
    new Expectations() {

        {
            generatorContext.getProject().getBuildPackageDirectory();
            result = new File("");
            generatorContext.getProject().getBaseDirectory();
            result = new File("");
            fjResult.getArchiveFile();
            result = new File("fat.jar");
            fatJarDetector.scan();
            result = fjResult;
        }
    };
    // @formatter:on
    // When
    final AssemblyConfiguration result = new JavaExecGenerator(generatorContext).createAssembly();
    // Then
    assertThat(result).hasFieldOrPropertyWithValue("excludeFinalOutputArtifact", true).extracting(AssemblyConfiguration::getLayers).asList().hasSize(1).first().asInstanceOf(InstanceOfAssertFactories.type(Assembly.class)).extracting(Assembly::getFileSets).asList().hasSize(3).extracting("directory", "includes").containsExactlyInAnyOrder(tuple(new File("src/main/jkube-includes"), Collections.emptyList()), tuple(new File("src/main/jkube-includes/bin"), Collections.emptyList()), tuple(new File(""), Collections.singletonList("fat.jar")));
}
Also used : Expectations(mockit.Expectations) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) Test(org.junit.Test)

Aggregations

Assembly (org.eclipse.jkube.kit.common.Assembly)36 AssemblyConfiguration (org.eclipse.jkube.kit.common.AssemblyConfiguration)31 File (java.io.File)29 Test (org.junit.Test)29 AssemblyFileSet (org.eclipse.jkube.kit.common.AssemblyFileSet)22 AssemblyFileEntry (org.eclipse.jkube.kit.common.AssemblyFileEntry)21 AssemblyFileSetUtils.processAssemblyFileSet (org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.processAssemblyFileSet)18 AssemblyFile (org.eclipse.jkube.kit.common.AssemblyFile)12 List (java.util.List)9 Map (java.util.Map)7 IOException (java.io.IOException)6 BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)6 ArrayList (java.util.ArrayList)5 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 Nonnull (javax.annotation.Nonnull)5 AssemblyConfigurationUtils.createDockerFileBuilder (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.createDockerFileBuilder)4 AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault)4 AssemblyConfigurationUtils.getJKubeAssemblyFileSets (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFileSets)4 AssemblyConfigurationUtils.getJKubeAssemblyFiles (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFiles)4