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

Maven: Split project dependencies out into their own layer #1780

Merged
merged 2 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -19,6 +19,7 @@
import com.google.cloud.tools.jib.api.AbsoluteUnixPath;
import com.google.cloud.tools.jib.api.Containerizer;
import com.google.cloud.tools.jib.api.JavaContainerBuilder;
import com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType;
import com.google.cloud.tools.jib.api.JibContainerBuilder;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.api.RegistryImage;
Expand All @@ -36,11 +37,14 @@
import com.google.cloud.tools.jib.plugins.common.logging.ProgressDisplayGenerator;
import com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor;
import com.google.common.annotations.VisibleForTesting;
import java.io.File;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -190,24 +194,6 @@ public JibContainerBuilder createContainerBuilder(
return JavaContainerBuilderHelper.fromExplodedWar(javaContainerBuilder, explodedWarPath);
}

// Add dependencies
Set<Artifact> allDependencies = project.getArtifacts();
javaContainerBuilder
.addDependencies(
allDependencies
.stream()
.filter(artifact -> !artifact.isSnapshot())
.map(Artifact::getFile)
.map(File::toPath)
.collect(Collectors.toList()))
.addSnapshotDependencies(
allDependencies
.stream()
.filter(Artifact::isSnapshot)
.map(Artifact::getFile)
.map(File::toPath)
.collect(Collectors.toList()));

switch (containerizingMode) {
case EXPLODED:
// Add resources, and classes
Expand All @@ -228,7 +214,26 @@ public JibContainerBuilder createContainerBuilder(
throw new IllegalStateException("unknown containerizing mode: " + containerizingMode);
}

return javaContainerBuilder.toContainerBuilder();
// Classify and add dependencies
Map<LayerType, List<Path>> classifiedDependencies =
classifyDependencies(
project.getArtifacts(),
session
.getProjects()
.stream()
.map(MavenProject::getArtifact)
.collect(Collectors.toSet()));

return javaContainerBuilder
.addDependencies(
Preconditions.checkNotNull(classifiedDependencies.get(LayerType.DEPENDENCIES)))
.addSnapshotDependencies(
Preconditions.checkNotNull(
classifiedDependencies.get(LayerType.SNAPSHOT_DEPENDENCIES)))
.addProjectDependencies(
Preconditions.checkNotNull(
classifiedDependencies.get(LayerType.PROJECT_DEPENDENCIES)))
.toContainerBuilder();

} catch (IOException ex) {
throw new IOException(
Expand All @@ -243,6 +248,26 @@ public JibContainerBuilder createContainerBuilder(
}
}

@VisibleForTesting
Map<LayerType, List<Path>> classifyDependencies(
Set<Artifact> dependencies, Set<Artifact> projectArtifacts) {
Map<LayerType, List<Path>> classifiedDependencies = new HashMap<>();
classifiedDependencies.put(LayerType.DEPENDENCIES, new ArrayList<>());
classifiedDependencies.put(LayerType.SNAPSHOT_DEPENDENCIES, new ArrayList<>());
classifiedDependencies.put(LayerType.PROJECT_DEPENDENCIES, new ArrayList<>());

for (Artifact art : dependencies) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

artifact?

if (projectArtifacts.contains(art)) {
classifiedDependencies.get(LayerType.PROJECT_DEPENDENCIES).add(art.getFile().toPath());
} else if (art.isSnapshot()) {
classifiedDependencies.get(LayerType.SNAPSHOT_DEPENDENCIES).add(art.getFile().toPath());
} else {
classifiedDependencies.get(LayerType.DEPENDENCIES).add(art.getFile().toPath());
}
}
return classifiedDependencies;
}

@Override
public List<Path> getClassFiles() throws IOException {
return new DirectoryWalker(Paths.get(project.getBuild().getOutputDirectory())).walk().asList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
Expand All @@ -40,11 +41,13 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build;
Expand Down Expand Up @@ -619,4 +622,52 @@ private BuildConfiguration setupBuildConfiguration(
Containerizer.to(RegistryImage.named("to"))
.setExecutorService(MoreExecutors.newDirectExecutorService()));
}

@Test
public void testClassifyDependencies() {
Set<Artifact> artifacts =
ImmutableSet.of(
newArtifact("com.test", "dependencyA", "1.0"),
newArtifact("com.test", "dependencyB", "4.0-SNAPSHOT"),
newArtifact("com.test", "projectA", "1.0"),
newArtifact("com.test", "dependencyC", "1.0-SNAPSHOT"),
newArtifact("com.test", "dependencyD", "4.0"),
newArtifact("com.test", "projectB", "1.0-SNAPSHOT"),
newArtifact("com.test", "projectC", "3.0"));

Set<Artifact> projectArtifacts =
ImmutableSet.of(
newArtifact("com.test", "projectA", "1.0"),
newArtifact("com.test", "projectB", "1.0-SNAPSHOT"),
newArtifact("com.test", "projectC", "3.0"));

Map<LayerType, List<Path>> classifyDependencies =
new MavenProjectProperties(mockMavenProject, mockMavenSession, mockLog)
.classifyDependencies(artifacts, projectArtifacts);

Assert.assertEquals(
classifyDependencies.get(LayerType.DEPENDENCIES),
ImmutableList.of(
newArtifact("com.test", "dependencyA", "1.0").getFile().toPath(),
newArtifact("com.test", "dependencyD", "4.0").getFile().toPath()));

Assert.assertEquals(
classifyDependencies.get(LayerType.SNAPSHOT_DEPENDENCIES),
ImmutableList.of(
newArtifact("com.test", "dependencyB", "4.0-SNAPSHOT").getFile().toPath(),
newArtifact("com.test", "dependencyC", "1.0-SNAPSHOT").getFile().toPath()));

Assert.assertEquals(
classifyDependencies.get(LayerType.PROJECT_DEPENDENCIES),
ImmutableList.of(
newArtifact("com.test", "projectA", "1.0").getFile().toPath(),
newArtifact("com.test", "projectB", "1.0-SNAPSHOT").getFile().toPath(),
newArtifact("com.test", "projectC", "3.0").getFile().toPath()));
}

private Artifact newArtifact(String group, String artifactId, String version) {
Artifact artifact = new DefaultArtifact(group, artifactId, version, null, "jar", "", null);
artifact.setFile(new File("/tmp/" + group + artifactId + version));
return artifact;
}
}