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(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);
}
});
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
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;
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
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();
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class ZipUtil method preserveModificationTimes.
/**
* Preserve modification time of files and directories in a zip file. If a directory is not an
* entry in the zip file and reproducible timestamps are enabled then its modification timestamp
* is set to a constant value.
*
* @param destination target root for unzipping
* @param entries list of entries in zip 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<ZipEntry> 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 (ZipEntry entry : entries) {
Files.setLastModifiedTime(destination.resolve(entry.getName()), entry.getLastModifiedTime());
}
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class MainClassFinderTest method testFindMainClass_subdirectories.
@Test
public void testFindMainClass_subdirectories() throws URISyntaxException, IOException {
Path rootDirectory = Paths.get(Resources.getResource("core/class-finder-tests/subdirectories").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("multi.layered.HelloWorld"));
}
Aggregations