Search in sources :

Example 41 with DirectoryWalker

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

the class MainClassFinderTest method testFindMainClass_extension.

@Test
public void testFindMainClass_extension() throws URISyntaxException, IOException {
    Path rootDirectory = Paths.get(Resources.getResource("core/class-finder-tests/extension").toURI());
    MainClassFinder.Result mainClassFinderResult = MainClassFinder.find(new DirectoryWalker(rootDirectory).walk(), logEventConsumer);
    Assert.assertSame(Result.Type.MAIN_CLASS_FOUND, mainClassFinderResult.getType());
    MatcherAssert.assertThat(mainClassFinderResult.getFoundMainClass(), CoreMatchers.containsString("main.MainClass"));
}
Also used : Path(java.nio.file.Path) Result(com.google.cloud.tools.jib.api.MainClassFinder.Result) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) Test(org.junit.Test)

Example 42 with DirectoryWalker

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

the class MainClassFinderTest method testMainClass_synthetic.

@Test
public void testMainClass_synthetic() throws URISyntaxException, IOException {
    Path rootDirectory = Paths.get(Resources.getResource("core/class-finder-tests/synthetic").toURI());
    MainClassFinder.Result mainClassFinderResult = MainClassFinder.find(new DirectoryWalker(rootDirectory).walk(), logEventConsumer);
    Assert.assertSame(MainClassFinder.Result.Type.MAIN_CLASS_FOUND, mainClassFinderResult.getType());
    MatcherAssert.assertThat(mainClassFinderResult.getFoundMainClass(), CoreMatchers.containsString("HelloWorldKt"));
}
Also used : Path(java.nio.file.Path) Result(com.google.cloud.tools.jib.api.MainClassFinder.Result) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) Test(org.junit.Test)

Example 43 with DirectoryWalker

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

the class TarExtractor method preserveModificationTimes.

/**
 * Preserve modification timestamps of files and directories in a tar file. If a directory is not
 * an entry in the tar file and reproducible timestamps are enabled then its modification
 * timestamp is set to a constant value. Note that the modification timestamps of symbolic links
 * are not preserved even with reproducible timestamps enabled.
 *
 * @param destination target root for unzipping
 * @param entries list of entries in tar file
 * @param enableReproducibleTimestamps whether or not reproducible timestamps should be used
 * @throws IOException when I/O error occurs
 */
private static void preserveModificationTimes(Path destination, List<TarArchiveEntry> entries, boolean enableReproducibleTimestamps) throws IOException {
    if (enableReproducibleTimestamps) {
        FileTime epochPlusOne = FileTime.fromMillis(1000L);
        new DirectoryWalker(destination).filter(Files::isDirectory).walk(path -> Files.setLastModifiedTime(path, epochPlusOne));
    }
    for (TarArchiveEntry entry : entries) {
        // the target to change
        if (!entry.isSymbolicLink()) {
            Files.setLastModifiedTime(destination.resolve(entry.getName()), FileTime.from(entry.getModTime().toInstant()));
        }
    }
}
Also used : DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) FileTime(java.nio.file.attribute.FileTime) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 44 with DirectoryWalker

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

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 45 with DirectoryWalker

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

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