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

Add file size to dependency filenames to avoid filename collisions #1338

Merged
merged 5 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ All notable changes to this project will be documented in this file.

### Fixed

- Builds failing due to dependency JARs with the same name ([#810](https://github.com/GoogleContainerTools/jib/issues/810))

## 0.10.1

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.Logger;
Expand Down Expand Up @@ -130,14 +133,31 @@ private static JavaLayerConfigurations getForNonWarProject(
}

// Adds dependency files.
List<String> duplicates =
coollog marked this conversation as resolved.
Show resolved Hide resolved
dependencyFiles
.getFiles()
.stream()
.map(File::getName)
.collect(Collectors.groupingBy(filename -> filename, Collectors.counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1)
.map(Entry::getKey)
.collect(Collectors.toList());
for (File dependencyFile : dependencyFiles) {
if (dependencyFile.exists()) {
boolean isSnapshot = dependencyFile.getName().contains("SNAPSHOT");
LayerType layerType = isSnapshot ? LayerType.SNAPSHOT_DEPENDENCIES : LayerType.DEPENDENCIES;
layerBuilder.addFile(
layerType,
dependencyFile.toPath(),
dependenciesExtractionPath.resolve(dependencyFile.getName()));
dependenciesExtractionPath.resolve(
duplicates.contains(dependencyFile.getName())
? dependencyFile
.getName()
.replaceFirst("\\.jar$", "-" + Files.size(dependencyFile.toPath()))
+ ".jar"
: dependencyFile.getName()));
} else {
logger.info("\t'" + dependencyFile + "' (not found, skipped)");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,19 @@ public void setUp() throws URISyntaxException, IOException {

Set<Path> allFiles = new HashSet<>(classesFiles);
allFiles.add(resourcesOutputDir);
allFiles.add(
Paths.get(Resources.getResource("application/dependencies/library.jarC.jar").toURI()));
allFiles.add(Paths.get(Resources.getResource("application/dependencies/libraryB.jar").toURI()));
allFiles.add(Paths.get(Resources.getResource("application/dependencies/libraryA.jar").toURI()));
allFiles.add(
Paths.get(Resources.getResource("application/dependencies/dependency-1.0.0.jar").toURI()));
allFiles.add(
Paths.get(
Resources.getResource("application/dependencies/more/dependency-1.0.0.jar").toURI()));
allFiles.add(
Paths.get(
Resources.getResource("application/dependencies/another/one/dependency-1.0.0.jar")
.toURI()));
allFiles.add(
Paths.get(
Resources.getResource("application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar")
Expand Down Expand Up @@ -158,8 +167,11 @@ public void test_correctFiles() throws URISyntaxException, IOException {
ImmutableList<Path> expectedDependenciesFiles =
ImmutableList.of(
applicationDirectory.resolve("dependencies/dependency-1.0.0.jar"),
applicationDirectory.resolve("dependencies/more/dependency-1.0.0.jar"),
applicationDirectory.resolve("dependencies/another/one/dependency-1.0.0.jar"),
applicationDirectory.resolve("dependencies/libraryA.jar"),
applicationDirectory.resolve("dependencies/libraryB.jar"));
applicationDirectory.resolve("dependencies/libraryB.jar"),
applicationDirectory.resolve("dependencies/library.jarC.jar"));
ImmutableList<Path> expectedSnapshotDependenciesFiles =
ImmutableList.of(
applicationDirectory.resolve("dependencies/dependencyX-1.0.0-SNAPSHOT.jar"));
Expand Down Expand Up @@ -262,12 +274,15 @@ public void testGetForProject_nonDefaultAppRoot() throws IOException {

assertExtractionPathsUnordered(
Arrays.asList(
"/my/app/libs/dependency-1.0.0.jar",
"/my/app/libs/dependency-1.0.0-770.jar",
"/my/app/libs/dependency-1.0.0-200.jar",
"/my/app/libs/dependency-1.0.0-480.jar",
"/my/app/libs/libraryA.jar",
"/my/app/libs/libraryB.jar"),
"/my/app/libs/libraryB.jar",
"/my/app/libs/library.jarC.jar"),
configuration.getDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList("/my/app/libs/dependencyX-1.0.0-SNAPSHOT.jar"),
Collections.singletonList("/my/app/libs/dependencyX-1.0.0-SNAPSHOT.jar"),
configuration.getSnapshotDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList(
Expand Down Expand Up @@ -295,10 +310,15 @@ public void testGetForProject_defaultAppRoot() throws IOException {

assertExtractionPathsUnordered(
Arrays.asList(
"/app/libs/dependency-1.0.0.jar", "/app/libs/libraryA.jar", "/app/libs/libraryB.jar"),
"/app/libs/dependency-1.0.0-770.jar",
"/app/libs/dependency-1.0.0-200.jar",
"/app/libs/dependency-1.0.0-480.jar",
"/app/libs/libraryA.jar",
"/app/libs/libraryB.jar",
"/app/libs/library.jarC.jar"),
configuration.getDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList("/app/libs/dependencyX-1.0.0-SNAPSHOT.jar"),
Collections.singletonList("/app/libs/dependencyX-1.0.0-SNAPSHOT.jar"),
configuration.getSnapshotDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList(
Expand Down Expand Up @@ -366,10 +386,10 @@ public void testWebApp() throws URISyntaxException, IOException {
assertSourcePathsUnordered(expectedExtraFiles, configuration.getExtraFilesLayerEntries());

assertExtractionPathsUnordered(
Arrays.asList("/my/app/WEB-INF/lib/dependency-1.0.0.jar"),
Collections.singletonList("/my/app/WEB-INF/lib/dependency-1.0.0.jar"),
configuration.getDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList("/my/app/WEB-INF/lib/dependencyX-1.0.0-SNAPSHOT.jar"),
Collections.singletonList("/my/app/WEB-INF/lib/dependencyX-1.0.0-SNAPSHOT.jar"),
configuration.getSnapshotDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList(
Expand Down Expand Up @@ -409,10 +429,10 @@ public void testWebApp_defaultWebAppRoot() throws URISyntaxException, IOExceptio
AbsoluteUnixPath.get(JavaLayerConfigurations.DEFAULT_WEB_APP_ROOT));

assertExtractionPathsUnordered(
Arrays.asList("/jetty/webapps/ROOT/WEB-INF/lib/dependency-1.0.0.jar"),
Collections.singletonList("/jetty/webapps/ROOT/WEB-INF/lib/dependency-1.0.0.jar"),
configuration.getDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList("/jetty/webapps/ROOT/WEB-INF/lib/dependencyX-1.0.0-SNAPSHOT.jar"),
Collections.singletonList("/jetty/webapps/ROOT/WEB-INF/lib/dependencyX-1.0.0-SNAPSHOT.jar"),
configuration.getSnapshotDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList(
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
]e$Ềx���,�
.3��I݅����3�8��V�KA����M��)=5�~�'qю�$[��- :��&�%�� ����E�o����7N���s`�i�Z0MT.����9J[�}?\E ��}U�v���J�d��o(��i��"M��ԛ��_+/�c���I<��Zje��44%���d2?�l>.��-�O�=H�i�
2 changes: 2 additions & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ All notable changes to this project will be documented in this file.

### Fixed

- Builds failing due to dependency JARs with the same name ([#810](https://github.com/GoogleContainerTools/jib/issues/810))

## 0.10.1

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import com.google.cloud.tools.jib.frontend.JavaLayerConfigurations.Builder;
import com.google.cloud.tools.jib.frontend.JavaLayerConfigurations.LayerType;
import com.google.cloud.tools.jib.plugins.common.JavaLayerConfigurationsHelper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;

Expand Down Expand Up @@ -87,12 +91,30 @@ private static JavaLayerConfigurations getForNonWarProject(
Builder layerBuilder = JavaLayerConfigurations.builder();

// Gets all the dependencies.
List<String> duplicates =
project
.getArtifacts()
.stream()
.map(Artifact::getFile)
.map(File::getName)
.collect(Collectors.groupingBy(filename -> filename, Collectors.counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1)
.map(Entry::getKey)
.collect(Collectors.toList());
for (Artifact artifact : project.getArtifacts()) {
Path artifactPath = artifact.getFile().toPath();
LayerType layerType =
artifact.isSnapshot() ? LayerType.SNAPSHOT_DEPENDENCIES : LayerType.DEPENDENCIES;
String filename = artifactPath.getFileName().toString();
layerBuilder.addFile(
layerType, artifactPath, dependenciesExtractionPath.resolve(artifactPath.getFileName()));
layerType,
artifactPath,
dependenciesExtractionPath.resolve(
duplicates.contains(filename)
? filename.replaceFirst("\\.jar$", "-" + Files.size(artifactPath)) + ".jar"
: filename));
}

Path classesOutputDirectory = Paths.get(project.getBuild().getOutputDirectory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ private static void assertExtractionPathsUnordered(
private static void assertNonDefaultAppRoot(JavaLayerConfigurations configuration) {
assertExtractionPathsUnordered(
Arrays.asList(
"/my/app/libs/dependency-1.0.0.jar",
"/my/app/libs/dependency-1.0.0-770.jar",
"/my/app/libs/dependency-1.0.0-200.jar",
"/my/app/libs/dependency-1.0.0-480.jar",
"/my/app/libs/libraryA.jar",
"/my/app/libs/libraryB.jar"),
"/my/app/libs/libraryB.jar",
"/my/app/libs/library.jarC.jar"),
configuration.getDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList("/my/app/libs/dependencyX-1.0.0-SNAPSHOT.jar"),
Collections.singletonList("/my/app/libs/dependencyX-1.0.0-SNAPSHOT.jar"),
configuration.getSnapshotDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList(
Expand Down Expand Up @@ -111,14 +114,19 @@ private static void assertNonDefaultAppRoot(JavaLayerConfigurations configuratio
@Before
public void setUp() throws URISyntaxException, IOException {
Path outputPath = Paths.get(Resources.getResource("application/output").toURI());
Path dependenciesPath = Paths.get(Resources.getResource("application/dependencies").toURI());

Mockito.when(mockMavenProject.getBuild()).thenReturn(mockBuild);
Mockito.when(mockBuild.getOutputDirectory()).thenReturn(outputPath.toString());

Set<Artifact> artifacts =
ImmutableSet.of(
makeArtifact(Paths.get("application", "dependencies", "libraryB.jar")),
makeArtifact(Paths.get("application", "dependencies", "libraryA.jar")),
makeArtifact(dependenciesPath.resolve("library.jarC.jar")),
makeArtifact(dependenciesPath.resolve("libraryB.jar")),
makeArtifact(dependenciesPath.resolve("libraryA.jar")),
makeArtifact(dependenciesPath.resolve("more").resolve("dependency-1.0.0.jar")),
makeArtifact(
dependenciesPath.resolve("another").resolve("one").resolve("dependency-1.0.0.jar")),
// Maven reads and populates "Artifacts" with its own processing, so read some from a
// repository
testRepository.findArtifact("com.test", "dependency", "1.0.0"),
Expand All @@ -135,11 +143,15 @@ public void setUp() throws URISyntaxException, IOException {

@Test
public void test_correctFiles() throws URISyntaxException, IOException {
Path dependenciesPath = Paths.get(Resources.getResource("application/dependencies").toURI());
ImmutableList<Path> expectedDependenciesFiles =
ImmutableList.of(
testRepository.artifactPathOnDisk("com.test", "dependency", "1.0.0"),
Paths.get("application", "dependencies", "libraryA.jar"),
Paths.get("application", "dependencies", "libraryB.jar"));
dependenciesPath.resolve("more").resolve("dependency-1.0.0.jar"),
dependenciesPath.resolve("another").resolve("one").resolve("dependency-1.0.0.jar"),
dependenciesPath.resolve("libraryA.jar"),
dependenciesPath.resolve("libraryB.jar"),
dependenciesPath.resolve("library.jarC.jar"));
ImmutableList<Path> expectedSnapshotDependenciesFiles =
ImmutableList.of(
testRepository.artifactPathOnDisk("com.test", "dependencyX", "1.0.0-SNAPSHOT"));
Expand Down Expand Up @@ -266,10 +278,10 @@ public void testGetForWarProject_nonDefaultAppRoot() throws URISyntaxException,
assertSourcePathsUnordered(expectedExtraFiles, configuration.getExtraFilesLayerEntries());

assertExtractionPathsUnordered(
Arrays.asList("/my/app/WEB-INF/lib/dependency-1.0.0.jar"),
Collections.singletonList("/my/app/WEB-INF/lib/dependency-1.0.0.jar"),
configuration.getDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList("/my/app/WEB-INF/lib/dependencyX-1.0.0-SNAPSHOT.jar"),
Collections.singletonList("/my/app/WEB-INF/lib/dependencyX-1.0.0-SNAPSHOT.jar"),
configuration.getSnapshotDependencyLayerEntries());
assertExtractionPathsUnordered(
Arrays.asList(
Expand Down Expand Up @@ -349,28 +361,28 @@ public void testGetForWarProject_noErrorIfWebInfClassesDoesNotExist() throws IOE
}

@Test
public void testIsWarProject_WarPackagingIsWar() throws IOException {
public void testIsWarProject_WarPackagingIsWar() {
Mockito.when(mockMavenProject.getPackaging()).thenReturn("war");

Assert.assertTrue(MojoCommon.isWarProject(mockMavenProject));
}

@Test
public void testIsWarProject_GwtAppPackagingIsWar() throws IOException {
public void testIsWarProject_GwtAppPackagingIsWar() {
Mockito.when(mockMavenProject.getPackaging()).thenReturn("gwt-app");

Assert.assertTrue(MojoCommon.isWarProject(mockMavenProject));
}

@Test
public void testIsWarProject_JarPackagingIsNotWar() throws IOException {
public void testIsWarProject_JarPackagingIsNotWar() {
Mockito.when(mockMavenProject.getPackaging()).thenReturn("jar");

Assert.assertFalse(MojoCommon.isWarProject(mockMavenProject));
}

@Test
public void testIsWarProject_GwtLibPackagingIsNotWar() throws IOException {
public void testIsWarProject_GwtLibPackagingIsNotWar() {
Mockito.when(mockMavenProject.getPackaging()).thenReturn("gwt-lib");

Assert.assertFalse(MojoCommon.isWarProject(mockMavenProject));
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
]e$Ềx���,�
.3��I݅����3�8��V�KA����M��)=5�~�'qю�$[��- :��&�%�� ����E�o����7N���s`�i�Z0MT.����9J[�}?\E ��}U�v���J�d��o(��i��"M��ԛ��_+/�c���I<��Zje��44%���d2?�l>.��-�O�=H�i�