Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JavaContainerBuilder in plugins #1572

Merged
merged 22 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.cloud.tools.jib.ProjectInfo;
import com.google.cloud.tools.jib.filesystem.AbsoluteUnixPath;
import com.google.cloud.tools.jib.filesystem.DirectoryWalker;
import com.google.cloud.tools.jib.filesystem.RelativeUnixPath;
import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor;
import com.google.cloud.tools.jib.frontend.JavaLayerConfigurations;
Expand Down Expand Up @@ -127,7 +128,7 @@ public static JavaContainerBuilder from(RegistryImage registryImage) {
// Keeps track of files to add to the image, by system path
private final List<PathPredicatePair> addedResources = new ArrayList<>();
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
private final List<PathPredicatePair> addedClasses = new ArrayList<>();
private final List<Path> addedDependencies = new ArrayList<>();
private final List<PathPredicatePair> addedDependencies = new ArrayList<>();
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
private final List<Path> addedOthers = new ArrayList<>();

private AbsoluteUnixPath appRoot = AbsoluteUnixPath.get("/app");
Expand Down Expand Up @@ -227,8 +228,8 @@ public JavaContainerBuilder addDependencies(List<Path> dependencyFiles) throws I
if (!Files.exists(file)) {
throw new NoSuchFileException(file.toString());
}
addedDependencies.add(new PathPredicatePair(file, path -> true));
}
addedDependencies.addAll(dependencyFiles);
classpathOrder.add(LayerType.DEPENDENCIES);
return this;
}
Expand All @@ -245,6 +246,26 @@ public JavaContainerBuilder addDependencies(Path... dependencyFiles) throws IOEx
return addDependencies(Arrays.asList(dependencyFiles));
}

/**
* Adds dependency JARs to the image. Duplicate JAR filenames are renamed with the filesize in
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
* order to avoid collisions.
*
* @param dependenciesDirectory the directory containing dependency JARs to add to the image
* @param pathFilter filter that determines which files (not directories) should be added
* @return this
* @throws IOException if adding the layer fails
*/
public JavaContainerBuilder addDependencies(
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
Path dependenciesDirectory, Predicate<Path> pathFilter) throws IOException {
return addDependencies(
new DirectoryWalker(dependenciesDirectory)
.filterRoot()
.filter(pathFilter)
.filter(path -> !Files.isDirectory(path))
.walk()
.asList());
}

/**
* Adds the contents of a resources directory to the image.
*
Expand Down Expand Up @@ -425,6 +446,7 @@ public JibContainerBuilder toContainerBuilder() throws IOException {
List<String> duplicates =
addedDependencies
.stream()
.map(entry -> entry.path)
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.groupingBy(filename -> filename, Collectors.counting()))
Expand All @@ -433,22 +455,24 @@ public JibContainerBuilder toContainerBuilder() throws IOException {
.filter(entry -> entry.getValue() > 1)
.map(Entry::getKey)
.collect(Collectors.toList());
for (Path file : addedDependencies) {
for (PathPredicatePair pathPredicatePair : addedDependencies) {
// Add dependencies to layer configuration
layerConfigurationsBuilder.addFile(
file.getFileName().toString().contains("SNAPSHOT")
pathPredicatePair.path.getFileName().toString().contains("SNAPSHOT")
? LayerType.SNAPSHOT_DEPENDENCIES
: LayerType.DEPENDENCIES,
file,
pathPredicatePair.path,
appRoot
.resolve(dependenciesDestination)
.resolve(
duplicates.contains(file.getFileName().toString())
? file.getFileName()
duplicates.contains(pathPredicatePair.path.getFileName().toString())
? pathPredicatePair
.path
.getFileName()
.toString()
.replaceFirst("\\.jar$", "-" + Files.size(file))
.replaceFirst("\\.jar$", "-" + Files.size(pathPredicatePair.path))
+ ".jar"
: file.getFileName().toString()));
: pathPredicatePair.path.getFileName().toString()));
}

// Add others to layer configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ JibContainer containerize(
* @throws IOException if an I/O exception occurs
*/
@VisibleForTesting
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
BuildConfiguration toBuildConfiguration(
public BuildConfiguration toBuildConfiguration(
Containerizer containerizer, ExecutorService executorService)
throws CacheDirectoryCreationException, IOException {
buildConfigurationBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public enum LayerType {
}

@VisibleForTesting
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
String getName() {
public String getName() {
return name;
}
}
Expand Down

This file was deleted.

Loading