Search in sources :

Example 21 with FileEntriesLayer

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

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_includes.

@Test
public void testExtraDirectoryLayerConfiguration_includes() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    FileEntriesLayer layerConfiguration = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Arrays.asList("**/bar", "**/*a*"), Collections.emptyList(), Collections.emptyMap(), (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(layerConfiguration.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(extraFilesDirectory.resolve("a/b/bar"), extraFilesDirectory.resolve("c/cat"));
}
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) Test(org.junit.Test)

Example 22 with FileEntriesLayer

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

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_includesAndExcludesEverything.

@Test
public void testExtraDirectoryLayerConfiguration_includesAndExcludesEverything() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    FileEntriesLayer layerConfiguration = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Arrays.asList("**/*"), Arrays.asList("**/*"), Collections.emptyMap(), (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(layerConfiguration.getEntries()).isEmpty();
}
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) Test(org.junit.Test)

Example 23 with FileEntriesLayer

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

the class PluginConfigurationProcessor method getSkaffoldSyncMap.

/**
 * Generate a skaffold syncmap JSON string for an image build configuration.
 *
 * @param rawConfiguration the raw configuration from the plugin
 * @param projectProperties an plugin specific implementation of {@link ProjectProperties}
 * @param excludes a set of paths to exclude, directories include in this list will be expanded
 * @return new json string representation of the Sync Map
 * @throws InvalidImageReferenceException if the image reference is invalid
 * @throws MainClassInferenceException if a main class could not be found
 * @throws InvalidAppRootException if the specific path for application root is invalid
 * @throws IOException if an error occurs creating the container builder
 * @throws InvalidWorkingDirectoryException if the working directory specified for the build is
 *     invalid
 * @throws InvalidPlatformException if there exists a {@link PlatformConfiguration} in the
 *     specified platforms list that is missing required fields or has invalid values
 * @throws InvalidContainerVolumeException if a specific container volume is invalid
 * @throws IncompatibleBaseImageJavaVersionException if the base image java version cannot support
 *     this build
 * @throws NumberFormatException if a string to number conversion operation fails
 * @throws InvalidContainerizingModeException if an invalid {@link ContainerizingMode} was
 *     specified
 * @throws InvalidFilesModificationTimeException if configured modification time could not be
 *     parsed
 * @throws InvalidCreationTimeException if configured creation time could not be parsed
 * @throws ExtraDirectoryNotFoundException if the extra directory specified for the build is not
 *     found
 */
public static String getSkaffoldSyncMap(RawConfiguration rawConfiguration, ProjectProperties projectProperties, Set<Path> excludes) throws IOException, InvalidCreationTimeException, InvalidImageReferenceException, IncompatibleBaseImageJavaVersionException, InvalidPlatformException, InvalidContainerVolumeException, MainClassInferenceException, InvalidAppRootException, InvalidWorkingDirectoryException, InvalidFilesModificationTimeException, InvalidContainerizingModeException, ExtraDirectoryNotFoundException {
    JibContainerBuilder jibContainerBuilder = processCommonConfiguration(rawConfiguration, ignored -> Optional.empty(), projectProperties);
    SkaffoldSyncMapTemplate syncMap = new SkaffoldSyncMapTemplate();
    // since jib has already expanded out directories after processing everything, we just
    // ignore directories and provide only files to watch
    Set<Path> excludesExpanded = getAllFiles(excludes);
    for (LayerObject layerObject : jibContainerBuilder.toContainerBuildPlan().getLayers()) {
        Verify.verify(layerObject instanceof FileEntriesLayer, "layer types other than FileEntriesLayer not yet supported in build plan layers");
        FileEntriesLayer layer = (FileEntriesLayer) layerObject;
        if (CONST_LAYERS.contains(layer.getName())) {
            continue;
        }
        if (GENERATED_LAYERS.contains(layer.getName())) {
            layer.getEntries().stream().filter(layerEntry -> Files.isRegularFile(layerEntry.getSourceFile())).filter(layerEntry -> !excludesExpanded.contains(layerEntry.getSourceFile().toAbsolutePath())).forEach(syncMap::addGenerated);
        } else {
            // this is a direct layer
            layer.getEntries().stream().filter(layerEntry -> Files.isRegularFile(layerEntry.getSourceFile())).filter(layerEntry -> !excludesExpanded.contains(layerEntry.getSourceFile().toAbsolutePath())).forEach(syncMap::addDirect);
        }
    }
    return syncMap.getJsonString();
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) CredHelperConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.CredHelperConfiguration) Arrays(java.util.Arrays) ImageReference(com.google.cloud.tools.jib.api.ImageReference) PlatformConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) ExtraDirectoriesConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtraDirectoriesConfiguration) Credential(com.google.cloud.tools.jib.api.Credential) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) Map(java.util.Map) Splitter(com.google.common.base.Splitter) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Verify(com.google.common.base.Verify) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) Set(java.util.Set) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) Stream(java.util.stream.Stream) ImageFormat(com.google.cloud.tools.jib.api.buildplan.ImageFormat) JibSystemProperties(com.google.cloud.tools.jib.global.JibSystemProperties) Optional(java.util.Optional) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Ports(com.google.cloud.tools.jib.api.Ports) DockerDaemonImage(com.google.cloud.tools.jib.api.DockerDaemonImage) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Jib(com.google.cloud.tools.jib.api.Jib) ImmutableList(com.google.common.collect.ImmutableList) CredentialRetrieverFactory(com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) Files(java.nio.file.Files) TarImage(com.google.cloud.tools.jib.api.TarImage) IOException(java.io.IOException) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) DateTimeFormatter(java.time.format.DateTimeFormatter) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Example 24 with FileEntriesLayer

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

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_excludes.

@Test
public void testExtraDirectoryLayerConfiguration_excludes() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    FileEntriesLayer layerConfiguration = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Collections.emptyList(), Arrays.asList("**/bar", "**/*a*"), Collections.emptyMap(), (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(layerConfiguration.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(extraFilesDirectory.resolve("a"), extraFilesDirectory.resolve("a/b"), extraFilesDirectory.resolve("c"), extraFilesDirectory.resolve("foo"));
}
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) Test(org.junit.Test)

Example 25 with FileEntriesLayer

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

the class JibMojo method findNatives.

private FileEntriesLayer findNatives() {
    final Path home = findHome();
    getLog().info("Using natives from '" + home + "'");
    final Path jreLib = home.resolve("jre/lib");
    final boolean isWin = Files.exists(jreLib.resolve("java.lib"));
    Path nativeFolder = isWin ? jreLib : /* win/cygwin */
    jreLib.resolve(// older graalvm, for 20.x it is no more needed
    System.getProperty("os.arch", "amd64"));
    if (!Files.exists(nativeFolder)) {
        nativeFolder = nativeFolder.getParent();
        try {
            if (!Files.exists(nativeFolder) || !Files.list(nativeFolder).anyMatch(it -> it.getFileName().toString().endsWith(".so"))) {
                throw new IllegalArgumentException("No native folder '" + nativeFolder + "' found.");
            }
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }
    final boolean includeAll = singletonList("true").equals(includeNatives) || singletonList("*").equals(includeNatives);
    final Predicate<Path> include = includeAll ? p -> true : path -> {
        final String name = path.getFileName().toString();
        return includeNatives.stream().anyMatch(n -> name.contains(isWin ? (n + ".lib") : ("lib" + n + ".so")));
    };
    final FileEntriesLayer.Builder builder = FileEntriesLayer.builder();
    final Collection<String> collected = new ArrayList<>();
    // ref for lambda
    final Path nativeDir = nativeFolder;
    try {
        Files.walkFileTree(nativeFolder, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                if (include.test(file)) {
                    collected.add(file.getFileName().toString());
                    builder.addEntry(file, AbsoluteUnixPath.get(nativeRootDir).resolve(nativeDir.relativize(file).toString()), FilePermissions.DEFAULT_FILE_PERMISSIONS, getTimestamp(file));
                }
                return super.visitFile(file, attrs);
            }
        });
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    if (!includeAll && collected.size() != includeNatives.size()) {
        throw new IllegalArgumentException("Found " + collected + " but was configured to extract " + includeNatives);
    }
    return builder.setName("Natives").build();
}
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) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

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