Search in sources :

Example 11 with DirectoryWalker

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

the class JavaContainerBuilder method addDirectoryContentsToLayer.

private void addDirectoryContentsToLayer(Map<LayerType, FileEntriesLayer.Builder> layerBuilders, LayerType layerType, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
    if (!layerBuilders.containsKey(layerType)) {
        layerBuilders.put(layerType, FileEntriesLayer.builder());
    }
    FileEntriesLayer.Builder builder = layerBuilders.get(layerType);
    new DirectoryWalker(sourceRoot).filterRoot().filter(path -> Files.isDirectory(path) || pathFilter.test(path)).walk(path -> {
        AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
        Instant modificationTime = modificationTimeProvider.get(path, pathOnContainer);
        builder.addEntry(path, pathOnContainer, modificationTime);
    });
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) Streams(com.google.common.collect.Streams) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ProjectInfo(com.google.cloud.tools.jib.ProjectInfo) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) Preconditions(com.google.common.base.Preconditions) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Example 12 with DirectoryWalker

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

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)

Example 13 with DirectoryWalker

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

the class MavenProjectPropertiesTest 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(org.codehaus.plexus.archiver.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(org.codehaus.plexus.archiver.zip.ZipEntry) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) StringJoiner(java.util.StringJoiner)

Example 14 with DirectoryWalker

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

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(PROJECTS_PATH_IN_RESOURCES + 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 15 with DirectoryWalker

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

the class TestProject method copyProject.

private void copyProject() throws IOException {
    projectRoot = ResourceExtractor.extractResourcePath(TestProject.class, PROJECTS_PATH_IN_RESOURCES + projectDir, newFolder(), true).toPath();
    // Puts the correct plugin version into the test project pom.xml.
    Path gradleProperties = Paths.get("gradle.properties");
    Properties properties = new Properties();
    properties.load(Files.newInputStream(gradleProperties));
    String pluginVersion = properties.getProperty("version");
    new DirectoryWalker(projectRoot).filter(TestProject::isPomXml).walk(pomXml -> Files.write(pomXml, new String(Files.readAllBytes(pomXml), StandardCharsets.UTF_8).replace("@@PluginVersion@@", pluginVersion).getBytes(StandardCharsets.UTF_8)));
}
Also used : Path(java.nio.file.Path) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) Properties(java.util.Properties)

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