Search in sources :

Example 46 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.

the class GradleProjectPropertiesTest method zipUpDirectory.

private static Path zipUpDirectory(Path sourceRoot, Path targetZip) throws IOException {
    try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(targetZip))) {
        for (Path source : new DirectoryWalker(sourceRoot).filterRoot().walk()) {
            StringJoiner pathJoiner = new StringJoiner("/", "", "");
            sourceRoot.relativize(source).forEach(element -> pathJoiner.add(element.toString()));
            String zipEntryPath = Files.isDirectory(source) ? pathJoiner.toString() + '/' : pathJoiner.toString();
            ZipEntry entry = new ZipEntry(zipEntryPath);
            zipOut.putNextEntry(entry);
            if (!Files.isDirectory(source)) {
                try (InputStream in = Files.newInputStream(source)) {
                    ByteStreams.copy(in, zipOut);
                }
            }
            zipOut.closeEntry();
        }
    }
    return targetZip;
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) StringJoiner(java.util.StringJoiner)

Example 47 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.

the class JavaContainerBuilderHelper method extraDirectoryLayerConfiguration.

/**
 * Returns a {@link FileEntriesLayer} for adding the extra directory to the container.
 *
 * @param sourceDirectory the source extra directory path
 * @param targetDirectory the root directory on the container to place the files in
 * @param includes the list of glob patterns to include from the source directory
 * @param excludes the list of glob patterns to exclude from the source directory
 * @param extraDirectoryPermissions map from path on container to file permissions
 * @param modificationTimeProvider file modification time provider
 * @return a {@link FileEntriesLayer} for adding the extra directory to the container
 * @throws IOException if walking the extra directory fails
 */
public static FileEntriesLayer extraDirectoryLayerConfiguration(Path sourceDirectory, AbsoluteUnixPath targetDirectory, List<String> includes, List<String> excludes, Map<String, FilePermissions> extraDirectoryPermissions, ModificationTimeProvider modificationTimeProvider) throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(LayerType.EXTRA_FILES.getName());
    Map<PathMatcher, FilePermissions> permissionsPathMatchers = new LinkedHashMap<>();
    for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
        permissionsPathMatchers.put(FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + entry.getKey()), entry.getValue());
    }
    DirectoryWalker walker = new DirectoryWalker(sourceDirectory).filterRoot();
    // add exclusion filters
    excludes.stream().map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern)).forEach(pathMatcher -> walker.filter(path -> !pathMatcher.matches(sourceDirectory.relativize(path))));
    // add an inclusion filter
    includes.stream().map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern)).map(pathMatcher -> (Predicate<Path>) path -> pathMatcher.matches(sourceDirectory.relativize(path))).reduce((matches1, matches2) -> matches1.or(matches2)).ifPresent(walker::filter);
    // walk the source tree and add layer entries
    walker.walk(localPath -> {
        AbsoluteUnixPath pathOnContainer = targetDirectory.resolve(sourceDirectory.relativize(localPath));
        Instant modificationTime = modificationTimeProvider.get(localPath, pathOnContainer);
        Optional<FilePermissions> permissions = determinePermissions(pathOnContainer, extraDirectoryPermissions, permissionsPathMatchers);
        if (permissions.isPresent()) {
            builder.addEntry(localPath, pathOnContainer, permissions.get(), modificationTime);
        } else {
            builder.addEntry(localPath, pathOnContainer, modificationTime);
        }
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) Set(java.util.Set) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) Instant(java.time.Instant) LinkedHashMap(java.util.LinkedHashMap) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) PathMatcher(java.nio.file.PathMatcher) Optional(java.util.Optional) Path(java.nio.file.Path) FileSystems(java.nio.file.FileSystems) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) Path(java.nio.file.Path) Instant(java.time.Instant) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) LinkedHashMap(java.util.LinkedHashMap) PathMatcher(java.nio.file.PathMatcher) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 48 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.

the class JavaContainerBuilderHelper method fromExplodedWar.

/**
 * Constructs a new {@link JibContainerBuilder} for a WAR project.
 *
 * @param javaContainerBuilder Java container builder to start with
 * @param explodedWar the exploded WAR directory
 * @param projectArtifactFilename the file names of project artifacts for project dependencies
 * @return {@link JibContainerBuilder} containing the layers for the exploded WAR
 * @throws IOException if adding layer contents fails
 */
public static JibContainerBuilder fromExplodedWar(JavaContainerBuilder javaContainerBuilder, Path explodedWar, Set<String> projectArtifactFilename) throws IOException {
    Path webInfLib = explodedWar.resolve("WEB-INF/lib");
    Path webInfClasses = explodedWar.resolve("WEB-INF/classes");
    Predicate<Path> isDependency = path -> path.startsWith(webInfLib);
    Predicate<Path> isClassFile = // Don't use Path.endsWith(), since Path works on path elements.
    path -> path.startsWith(webInfClasses) && path.getFileName().toString().endsWith(".class");
    Predicate<Path> isResource = isDependency.or(isClassFile).negate();
    Predicate<Path> isSnapshot = path -> path.getFileName().toString().contains("SNAPSHOT");
    Predicate<Path> isProjectDependency = path -> projectArtifactFilename.contains(path.getFileName().toString());
    javaContainerBuilder.setResourcesDestination(RelativeUnixPath.get("")).setClassesDestination(RelativeUnixPath.get("WEB-INF/classes")).setDependenciesDestination(RelativeUnixPath.get("WEB-INF/lib"));
    if (Files.exists(explodedWar)) {
        javaContainerBuilder.addResources(explodedWar, isResource);
    }
    if (Files.exists(webInfClasses)) {
        javaContainerBuilder.addClasses(webInfClasses, isClassFile);
    }
    if (Files.exists(webInfLib)) {
        javaContainerBuilder.addDependencies(new DirectoryWalker(webInfLib).filterRoot().filter(isSnapshot.negate()).filter(isProjectDependency.negate()).walk());
        javaContainerBuilder.addSnapshotDependencies(new DirectoryWalker(webInfLib).filterRoot().filter(isSnapshot).walk());
        javaContainerBuilder.addProjectDependencies(new DirectoryWalker(webInfLib).filterRoot().filter(isProjectDependency).walk());
    }
    return javaContainerBuilder.toContainerBuilder();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) Set(java.util.Set) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) Instant(java.time.Instant) LinkedHashMap(java.util.LinkedHashMap) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) PathMatcher(java.nio.file.PathMatcher) Optional(java.util.Optional) Path(java.nio.file.Path) FileSystems(java.nio.file.FileSystems) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Example 49 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.

the class TestProject method copyProject.

/**
 * Copies test project {@code projectName} to {@code destination} folder.
 */
private static void copyProject(String projectName, Path destination) throws IOException, URISyntaxException {
    Path projectPathInResources = Paths.get(Resources.getResource(projectName).toURI());
    new DirectoryWalker(projectPathInResources).filterRoot().walk(path -> {
        // Creates the same path in the destDir.
        Path destPath = destination.resolve(projectPathInResources.relativize(path));
        if (Files.isDirectory(path)) {
            Files.createDirectory(destPath);
        } else {
            Files.copy(path, destPath);
        }
    });
}
Also used : Path(java.nio.file.Path) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Example 50 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.

the class ArtifactLayers method getDirectoryContentsAsLayer.

/**
 * Creates a layer containing contents of a directory. Only paths that match the given predicate
 * will be added.
 *
 * @param layerName name of the layer
 * @param sourceRoot path to source directory
 * @param pathFilter predicate to determine whether to add the path or not
 * @param basePathInContainer path to destination on container
 * @return {@link FileEntriesLayer} representing the layer
 * @throws IOException if io exception occurs when reading from the source directory
 */
public static FileEntriesLayer getDirectoryContentsAsLayer(String layerName, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(layerName);
    new DirectoryWalker(sourceRoot).filterRoot().filter(path -> pathFilter.test(path)).walk(path -> {
        AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
        builder.addEntry(path, pathOnContainer);
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Predicate(java.util.function.Predicate) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Aggregations

DirectoryWalker (com.google.cloud.tools.jib.filesystem.DirectoryWalker)52 Path (java.nio.file.Path)48 Test (org.junit.Test)23 Result (com.google.cloud.tools.jib.api.MainClassFinder.Result)20 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)15 IOException (java.io.IOException)11 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)9 Files (java.nio.file.Files)9 FileTime (java.nio.file.attribute.FileTime)8 List (java.util.List)8 Predicate (java.util.function.Predicate)8 Instant (java.time.Instant)7 Map (java.util.Map)7 ModificationTimeProvider (com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider)6 RelativeUnixPath (com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath)6 InputStream (java.io.InputStream)6 Paths (java.nio.file.Paths)6 ArrayList (java.util.ArrayList)6 StringJoiner (java.util.StringJoiner)6 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)5