From cd0f41e9e3513b83a1661f930f185d77ecb15528 Mon Sep 17 00:00:00 2001 From: Appu Goundan Date: Thu, 12 Jul 2018 10:58:00 -0400 Subject: [PATCH 1/4] Add support for SNAPSHOT dependencies layer --- .../tools/jib/builder/EntrypointBuilder.java | 1 + .../jib/builder/SourceFilesConfiguration.java | 13 ++++ .../BuildAndCacheApplicationLayerStep.java | 11 +++ .../jib/docker/DockerContextGenerator.java | 6 ++ .../jib/builder/EntrypointBuilderTest.java | 5 +- .../builder/TestSourceFilesConfiguration.java | 12 +++ ...BuildAndCacheApplicationLayerStepTest.java | 25 +++++-- .../docker/DockerContextGeneratorTest.java | 10 +++ .../dependency-1.0.0-SNAPSHOT.jar | Bin 0 -> 770 bytes jib-core/src/test/resources/sampleDockerfile | 3 +- .../GradleSourceFilesConfiguration.java | 19 ++++- .../GradleSourceFilesConfigurationTest.java | 16 ++++ .../dependencyX-1.0.0-SNAPSHOT.jar | Bin 0 -> 770 bytes jib-maven-plugin/pom.xml | 14 ++++ .../maven/MavenSourceFilesConfiguration.java | 19 ++++- .../MavenSourceFilesConfigurationTest.java | 33 ++++++--- .../cloud/tools/jib/maven/TestRepository.java | 69 ++++++++++++++++++ .../dependencyX-1.0.0-SNAPSHOT.jar | Bin 0 -> 770 bytes .../dependency/1.0.0/_remote.repositories | 4 + .../dependency/1.0.0/dependency-1.0.0.jar | Bin 0 -> 770 bytes .../dependency/1.0.0/dependency-1.0.0.pom | 9 +++ .../test/dependency/maven-metadata-local.xml | 12 +++ .../1.0.0-SNAPSHOT/_remote.repositories | 4 + .../dependencyX-1.0.0-SNAPSHOT.jar | Bin 0 -> 770 bytes .../dependencyX-1.0.0-SNAPSHOT.pom | 9 +++ .../1.0.0-SNAPSHOT/maven-metadata-local.xml | 24 ++++++ .../test/dependencyX/maven-metadata-local.xml | 11 +++ 27 files changed, 310 insertions(+), 19 deletions(-) create mode 100644 jib-core/src/test/resources/application/snapshot-dependencies/dependency-1.0.0-SNAPSHOT.jar create mode 100644 jib-gradle-plugin/src/test/resources/application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar create mode 100644 jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/TestRepository.java create mode 100644 jib-maven-plugin/src/test/resources/application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependency/1.0.0/_remote.repositories create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependency/1.0.0/dependency-1.0.0.jar create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependency/1.0.0/dependency-1.0.0.pom create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependency/maven-metadata-local.xml create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/_remote.repositories create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/dependencyX-1.0.0-SNAPSHOT.jar create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/dependencyX-1.0.0-SNAPSHOT.pom create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/maven-metadata-local.xml create mode 100644 jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/maven-metadata-local.xml diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java index 2c1c06ffea..19360ed864 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java @@ -37,6 +37,7 @@ public static ImmutableList makeEntrypoint( ImmutableList classPaths = ImmutableList.of( sourceFilesConfiguration.getDependenciesPathOnImage() + "*", + sourceFilesConfiguration.getSnapshotDependenciesPathOnImage() + "*", sourceFilesConfiguration.getResourcesPathOnImage(), sourceFilesConfiguration.getClassesPathOnImage()); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java index 15de722be6..0de4968f6b 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java @@ -26,6 +26,7 @@ public interface SourceFilesConfiguration { String DEFAULT_DEPENDENCIES_PATH_ON_IMAGE = "/app/libs/"; + String DEFAULT_SNAPSHOT_DEPENDENCIES_PATH_ON_IMAGE = "/app/snapshot-libs/"; String DEFAULT_RESOURCES_PATH_ON_IMAGE = "/app/resources/"; String DEFAULT_CLASSES_PATH_ON_IMAGE = "/app/classes/"; @@ -35,6 +36,12 @@ public interface SourceFilesConfiguration { */ ImmutableList getDependenciesFiles(); + /** + * @return the source files for snapshot dependencies. These files should be in a deterministic + * order + */ + ImmutableList getSnapshotDependenciesFiles(); + /** * @return the source files for the resources layer. These files should be in a deterministic * order. @@ -52,6 +59,12 @@ public interface SourceFilesConfiguration { */ String getDependenciesPathOnImage(); + /** + * @return the Unix-style path where the snapshot dependencies sources files are place in the + * container filesystem. Must end with slash. + */ + String getSnapshotDependenciesPathOnImage(); + /** * @return the Unix-style path where the resources source files are placed in the container * filesystem. Must end with slash. diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java index bff71a426b..c63a932403 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java @@ -63,6 +63,17 @@ static ImmutableList makeList( sourceFilesConfiguration.getDependenciesPathOnImage()) .build(), cache)) + .add( + new BuildAndCacheApplicationLayerStep( + "snapshot-dependencies", + listeningExecutorService, + buildConfiguration, + LayerConfiguration.builder() + .addEntry( + sourceFilesConfiguration.getSnapshotDependenciesFiles(), + sourceFilesConfiguration.getSnapshotDependenciesPathOnImage()) + .build(), + cache)) .add( new BuildAndCacheApplicationLayerStep( "resources", diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java index 91bbafd809..d8042a3205 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java @@ -136,14 +136,18 @@ public void generate(Path targetDirectory) throws IOException { // Creates the directories. Path dependenciesDir = targetDirectory.resolve("libs"); + Path snapshotDependenciesDir = targetDirectory.resolve("snapshot-libs"); Path resourcesDIr = targetDirectory.resolve("resources"); Path classesDir = targetDirectory.resolve("classes"); Files.createDirectory(dependenciesDir); + Files.createDirectories(snapshotDependenciesDir); Files.createDirectory(resourcesDIr); Files.createDirectory(classesDir); // Copies dependencies. FileOperations.copy(sourceFilesConfiguration.getDependenciesFiles(), dependenciesDir); + FileOperations.copy( + sourceFilesConfiguration.getSnapshotDependenciesFiles(), snapshotDependenciesDir); FileOperations.copy(sourceFilesConfiguration.getResourcesFiles(), resourcesDIr); FileOperations.copy(sourceFilesConfiguration.getClassesFiles(), classesDir); @@ -179,6 +183,8 @@ String makeDockerfile() throws JsonProcessingException { .append(Preconditions.checkNotNull(baseImage)) .append("\n\nCOPY libs ") .append(sourceFilesConfiguration.getDependenciesPathOnImage()) + .append("\nCOPY snapshot-libs ") + .append(sourceFilesConfiguration.getSnapshotDependenciesPathOnImage()) .append("\nCOPY resources ") .append(sourceFilesConfiguration.getResourcesPathOnImage()) .append("\nCOPY classes ") diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java index f6da756da0..7298365890 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java @@ -28,6 +28,7 @@ public class EntrypointBuilderTest { @Test public void testMakeEntrypoint() { String expectedDependenciesPath = "/app/libs/"; + String expectedSnapshotDependenciesPath = "/app/snapshot-libs/"; String expectedResourcesPath = "/app/resources/"; String expectedClassesPath = "/app/classes/"; List expectedJvmFlags = Arrays.asList("-flag", "anotherFlag"); @@ -38,6 +39,8 @@ public void testMakeEntrypoint() { Mockito.when(mockSourceFilesConfiguration.getDependenciesPathOnImage()) .thenReturn(expectedDependenciesPath); + Mockito.when(mockSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()) + .thenReturn(expectedSnapshotDependenciesPath); Mockito.when(mockSourceFilesConfiguration.getResourcesPathOnImage()) .thenReturn(expectedResourcesPath); Mockito.when(mockSourceFilesConfiguration.getClassesPathOnImage()) @@ -49,7 +52,7 @@ public void testMakeEntrypoint() { "-flag", "anotherFlag", "-cp", - "/app/libs/*:/app/resources/:/app/classes/", + "/app/libs/*:/app/snapshot-libs/*:/app/resources/:/app/classes/", "SomeMainClass"), EntrypointBuilder.makeEntrypoint( mockSourceFilesConfiguration, expectedJvmFlags, expectedMainClass)); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java index 49d27ce489..92354be594 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java @@ -31,11 +31,13 @@ public class TestSourceFilesConfiguration implements SourceFilesConfiguration { private static final String EXTRACTION_PATH = "/some/extraction/path/"; private final ImmutableList dependenciesSourceFiles; + private final ImmutableList snapshotDependenciesSourceFiles; private final ImmutableList resourcesSourceFiles; private final ImmutableList classesSourceFiles; public TestSourceFilesConfiguration() throws URISyntaxException, IOException { dependenciesSourceFiles = getFilesList("application/dependencies"); + snapshotDependenciesSourceFiles = getFilesList("application/snapshot-dependencies"); resourcesSourceFiles = getFilesList("application/resources"); classesSourceFiles = getFilesList("application/classes"); } @@ -45,6 +47,11 @@ public ImmutableList getDependenciesFiles() { return dependenciesSourceFiles; } + @Override + public ImmutableList getSnapshotDependenciesFiles() { + return snapshotDependenciesSourceFiles; + } + @Override public ImmutableList getResourcesFiles() { return resourcesSourceFiles; @@ -60,6 +67,11 @@ public String getDependenciesPathOnImage() { return EXTRACTION_PATH + "libs/"; } + @Override + public String getSnapshotDependenciesPathOnImage() { + return EXTRACTION_PATH + "snapshot-libs/"; + } + @Override public String getResourcesPathOnImage() { return EXTRACTION_PATH + "resources/"; diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java index 0c2049f855..297deff9c4 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java @@ -91,7 +91,7 @@ public void testRun() applicationLayers = applicationLayersBuilder.build(); cache.addCachedLayersWithMetadataToMetadata(applicationLayers.getLayers()); - Assert.assertEquals(4, applicationLayers.size()); + Assert.assertEquals(5, applicationLayers.size()); } // Re-initialize cache with the updated metadata. @@ -102,6 +102,11 @@ public void testRun() new LayerEntry( testSourceFilesConfiguration.getDependenciesFiles(), testSourceFilesConfiguration.getDependenciesPathOnImage())); + ImmutableList snapshotDependenciesLayerEntry = + ImmutableList.of( + new LayerEntry( + testSourceFilesConfiguration.getSnapshotDependenciesFiles(), + testSourceFilesConfiguration.getSnapshotDependenciesPathOnImage())); ImmutableList resourcesLayerEntry = ImmutableList.of( new LayerEntry( @@ -122,12 +127,17 @@ public void testRun() cacheReader.getUpToDateLayerByLayerEntries(dependenciesLayerEntry).getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(1).getBlobDescriptor(), - cacheReader.getUpToDateLayerByLayerEntries(resourcesLayerEntry).getBlobDescriptor()); + cacheReader + .getUpToDateLayerByLayerEntries(snapshotDependenciesLayerEntry) + .getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(2).getBlobDescriptor(), - cacheReader.getUpToDateLayerByLayerEntries(classesLayerEntry).getBlobDescriptor()); + cacheReader.getUpToDateLayerByLayerEntries(resourcesLayerEntry).getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(3).getBlobDescriptor(), + cacheReader.getUpToDateLayerByLayerEntries(classesLayerEntry).getBlobDescriptor()); + Assert.assertEquals( + applicationLayers.get(4).getBlobDescriptor(), cacheReader.getUpToDateLayerByLayerEntries(extraFilesLayerEntry).getBlobDescriptor()); // Verifies that the cache reader gets the same layers as the newest application layers. @@ -135,10 +145,13 @@ public void testRun() applicationLayers.get(0).getContentFile(), cacheReader.getLayerFile(dependenciesLayerEntry)); Assert.assertEquals( - applicationLayers.get(1).getContentFile(), cacheReader.getLayerFile(resourcesLayerEntry)); + applicationLayers.get(1).getContentFile(), + cacheReader.getLayerFile(snapshotDependenciesLayerEntry)); + Assert.assertEquals( + applicationLayers.get(2).getContentFile(), cacheReader.getLayerFile(resourcesLayerEntry)); Assert.assertEquals( - applicationLayers.get(2).getContentFile(), cacheReader.getLayerFile(classesLayerEntry)); + applicationLayers.get(3).getContentFile(), cacheReader.getLayerFile(classesLayerEntry)); Assert.assertEquals( - applicationLayers.get(3).getContentFile(), cacheReader.getLayerFile(extraFilesLayerEntry)); + applicationLayers.get(4).getContentFile(), cacheReader.getLayerFile(extraFilesLayerEntry)); } } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java index 89ac1936eb..838a74f4b3 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java @@ -65,11 +65,14 @@ private static void assertSameFiles(Path directory1, Path directory2) throws IOE @Before public void setUpMocks() { String expectedDependenciesPath = "/app/libs/"; + String expectedSnapshotDependenciesPath = "/app/snapshot-libs/"; String expectedResourcesPath = "/app/resources/"; String expectedClassesPath = "/app/classes/"; Mockito.when(mockSourceFilesConfiguration.getDependenciesPathOnImage()) .thenReturn(expectedDependenciesPath); + Mockito.when(mockSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()) + .thenReturn(expectedSnapshotDependenciesPath); Mockito.when(mockSourceFilesConfiguration.getResourcesPathOnImage()) .thenReturn(expectedResourcesPath); Mockito.when(mockSourceFilesConfiguration.getClassesPathOnImage()) @@ -79,16 +82,22 @@ public void setUpMocks() { @Test public void testGenerate() throws IOException, URISyntaxException { Path testDependencies = Paths.get(Resources.getResource("application/dependencies").toURI()); + Path testSnapshotDependencies = + Paths.get(Resources.getResource("application/snapshot-dependencies").toURI()); Path testResources = Paths.get(Resources.getResource("application/resources").toURI()); Path testClasses = Paths.get(Resources.getResource("application/classes").toURI()); ImmutableList expectedDependenciesFiles = new DirectoryWalker(testDependencies).filterRoot().walk(); + ImmutableList expectedSnapshotDependenciesFiles = + new DirectoryWalker(testSnapshotDependencies).filterRoot().walk(); ImmutableList expectedResourcesFiles = new DirectoryWalker(testResources).filterRoot().walk(); ImmutableList expectedClassesFiles = new DirectoryWalker(testClasses).filterRoot().walk(); Mockito.when(mockSourceFilesConfiguration.getDependenciesFiles()) .thenReturn(expectedDependenciesFiles); + Mockito.when(mockSourceFilesConfiguration.getSnapshotDependenciesFiles()) + .thenReturn(expectedSnapshotDependenciesFiles); Mockito.when(mockSourceFilesConfiguration.getResourcesFiles()) .thenReturn(expectedResourcesFiles); Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(expectedClassesFiles); @@ -107,6 +116,7 @@ public void testGenerate() throws IOException, URISyntaxException { Assert.assertTrue(Files.exists(targetDirectory.resolve("Dockerfile"))); assertSameFiles(targetDirectory.resolve("libs"), testDependencies); + assertSameFiles(targetDirectory.resolve("snapshot-libs"), testSnapshotDependencies); assertSameFiles(targetDirectory.resolve("resources"), testResources); assertSameFiles(targetDirectory.resolve("classes"), testClasses); } diff --git a/jib-core/src/test/resources/application/snapshot-dependencies/dependency-1.0.0-SNAPSHOT.jar b/jib-core/src/test/resources/application/snapshot-dependencies/dependency-1.0.0-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..dd68926802ed2f42df108b18fa84681be324ae42 GIT binary patch literal 770 zcmWIWW@h1HVBp|j*cqkm!vF+KAOZ+Df!NnI#8KDN&rP41ApoxMWTdvwrh2A#(m(~0 zKrDi+(AUw=)6F$FM9k6RUH3HY=W$WPI3F<;f$>@(8#yf#wY7k_r*l!oSd4nQ}Uj${8L}2M(zxj&f2_7ErhM{W<=_n zn4f~eo6K)zRC^cf+H0w3%Q5Xy*zsE*RH`BiEP}b`Ki+g8_+Lz`)irD5-1%SIF39Qs zoO*Ur@Ar3oPHDUEhDL3?>+Q7qw)M9Kk4!q8`rnttMBF^S{!xl#r3lAa<+m+oY=74| ztdO;m7tj8tqc>raKO;x_2?gyr!c&`++aGxyTx7A%fHPfhzr%dbB`Py~&sd#mdYa21 zI)_6%z%|#rX5JkJzr#O0%4FTFo4zht_mMxqn~_O`8F!=ugB1t_7~VR9Xm}Dp)`l$! zfE0tkl15D+3D<^{AdpSK7Vlu~V2jIuOr)qq*NC19AR0kdFGSUdnkE9gS=m5JS%B~- Lkp2uzL<|f7lVIYI literal 0 HcmV?d00001 diff --git a/jib-core/src/test/resources/sampleDockerfile b/jib-core/src/test/resources/sampleDockerfile index 73d372b6d0..d6617b1cde 100644 --- a/jib-core/src/test/resources/sampleDockerfile +++ b/jib-core/src/test/resources/sampleDockerfile @@ -1,10 +1,11 @@ FROM somebaseimage COPY libs /app/libs/ +COPY snapshot-libs /app/snapshot-libs/ COPY resources /app/resources/ COPY classes /app/classes/ EXPOSE 1000/tcp EXPOSE 2000-2010/udp -ENTRYPOINT ["java","-flag","another\"Flag","-cp","/app/libs/*:/app/resources/:/app/classes/","SomeMainClass"] +ENTRYPOINT ["java","-flag","another\"Flag","-cp","/app/libs/*:/app/snapshot-libs/*:/app/resources/:/app/classes/","SomeMainClass"] CMD ["arg1","arg2"] diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java index 1f22997656..c48795bd18 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java @@ -43,6 +43,7 @@ static GradleSourceFilesConfiguration getForProject( } private final ImmutableList dependenciesFiles; + private final ImmutableList snapshotDependenciesFiles; private final ImmutableList resourcesFiles; private final ImmutableList classesFiles; @@ -55,6 +56,7 @@ private GradleSourceFilesConfiguration(Project project, GradleBuildLogger gradle SourceSet mainSourceSet = javaPluginConvention.getSourceSets().getByName(MAIN_SOURCE_SET_NAME); List dependenciesFiles = new ArrayList<>(); + List snapshotDependenciesFiles = new ArrayList<>(); List resourcesFiles = new ArrayList<>(); List classesFiles = new ArrayList<>(); @@ -92,11 +94,16 @@ private GradleSourceFilesConfiguration(Project project, GradleBuildLogger gradle if (resourcesOutputDirectory.equals(dependencyFile.toPath())) { continue; } - dependenciesFiles.add(dependencyFile.toPath()); + if (dependencyFile.getName().contains("SNAPSHOT")) { + snapshotDependenciesFiles.add(dependencyFile.toPath()); + } else { + dependenciesFiles.add(dependencyFile.toPath()); + } } // Sorts all files by path for consistent ordering. this.dependenciesFiles = ImmutableList.sortedCopyOf(dependenciesFiles); + this.snapshotDependenciesFiles = ImmutableList.sortedCopyOf(snapshotDependenciesFiles); this.resourcesFiles = ImmutableList.sortedCopyOf(resourcesFiles); this.classesFiles = ImmutableList.sortedCopyOf(classesFiles); } @@ -106,6 +113,11 @@ public ImmutableList getDependenciesFiles() { return dependenciesFiles; } + @Override + public ImmutableList getSnapshotDependenciesFiles() { + return snapshotDependenciesFiles; + } + @Override public ImmutableList getResourcesFiles() { return resourcesFiles; @@ -121,6 +133,11 @@ public String getDependenciesPathOnImage() { return DEFAULT_DEPENDENCIES_PATH_ON_IMAGE; } + @Override + public String getSnapshotDependenciesPathOnImage() { + return DEFAULT_SNAPSHOT_DEPENDENCIES_PATH_ON_IMAGE; + } + @Override public String getResourcesPathOnImage() { return DEFAULT_RESOURCES_PATH_ON_IMAGE; diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java index 54376a14c5..5109f056a6 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java @@ -93,6 +93,11 @@ public void setUp() throws URISyntaxException, IOException { allFiles.add( Paths.get(Resources.getResource("application/dependencies/dependency-1.0.0.jar").toURI()) .toFile()); + allFiles.add( + Paths.get( + Resources.getResource("application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar") + .toURI()) + .toFile()); FileCollection runtimeFileCollection = new TestFileCollection(allFiles); Mockito.when(mockProject.getConvention()).thenReturn(mockConvention); @@ -117,6 +122,11 @@ public void test_correctFiles() throws URISyntaxException { Resources.getResource("application/dependencies/dependency-1.0.0.jar").toURI()), Paths.get(Resources.getResource("application/dependencies/libraryA.jar").toURI()), Paths.get(Resources.getResource("application/dependencies/libraryB.jar").toURI())); + ImmutableList expectedSnapshotDependenciesFiles = + ImmutableList.of( + Paths.get( + Resources.getResource("application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar") + .toURI())); ImmutableList expectedResourcesFiles = ImmutableList.of( Paths.get(Resources.getResource("application/resources").toURI()).resolve("resourceA"), @@ -130,6 +140,9 @@ public void test_correctFiles() throws URISyntaxException { Assert.assertEquals( expectedDependenciesFiles, testGradleSourceFilesConfiguration.getDependenciesFiles()); + Assert.assertEquals( + expectedSnapshotDependenciesFiles, + testGradleSourceFilesConfiguration.getSnapshotDependenciesFiles()); Assert.assertEquals( expectedResourcesFiles, testGradleSourceFilesConfiguration.getResourcesFiles()); Assert.assertEquals(expectedClassesFiles, testGradleSourceFilesConfiguration.getClassesFiles()); @@ -154,6 +167,9 @@ public void test_noClassesFiles() throws IOException { public void test_correctPathsOnImage() { Assert.assertEquals( "/app/libs/", testGradleSourceFilesConfiguration.getDependenciesPathOnImage()); + Assert.assertEquals( + "/app/snapshot-libs/", + testGradleSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()); Assert.assertEquals( "/app/resources/", testGradleSourceFilesConfiguration.getResourcesPathOnImage()); Assert.assertEquals( diff --git a/jib-gradle-plugin/src/test/resources/application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar b/jib-gradle-plugin/src/test/resources/application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..dd68926802ed2f42df108b18fa84681be324ae42 GIT binary patch literal 770 zcmWIWW@h1HVBp|j*cqkm!vF+KAOZ+Df!NnI#8KDN&rP41ApoxMWTdvwrh2A#(m(~0 zKrDi+(AUw=)6F$FM9k6RUH3HY=W$WPI3F<;f$>@(8#yf#wY7k_r*l!oSd4nQ}Uj${8L}2M(zxj&f2_7ErhM{W<=_n zn4f~eo6K)zRC^cf+H0w3%Q5Xy*zsE*RH`BiEP}b`Ki+g8_+Lz`)irD5-1%SIF39Qs zoO*Ur@Ar3oPHDUEhDL3?>+Q7qw)M9Kk4!q8`rnttMBF^S{!xl#r3lAa<+m+oY=74| ztdO;m7tj8tqc>raKO;x_2?gyr!c&`++aGxyTx7A%fHPfhzr%dbB`Py~&sd#mdYa21 zI)_6%z%|#rX5JkJzr#O0%4FTFo4zht_mMxqn~_O`8F!=ugB1t_7~VR9Xm}Dp)`l$! zfE0tkl15D+3D<^{AdpSK7Vlu~V2jIuOr)qq*NC19AR0kdFGSUdnkE9gS=m5JS%B~- Lkp2uzL<|f7lVIYI literal 0 HcmV?d00001 diff --git a/jib-maven-plugin/pom.xml b/jib-maven-plugin/pom.xml index 5c4d3214ff..ee93d11df0 100644 --- a/jib-maven-plugin/pom.xml +++ b/jib-maven-plugin/pom.xml @@ -123,6 +123,20 @@ 2.12.0 test + + + org.apache.maven.plugin-testing + maven-plugin-testing-harness + 3.3.0 + test + + + org.apache.maven + maven-compat + 3.5.4 + test + + diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java index 506b19da04..02279900ad 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java @@ -44,6 +44,7 @@ static MavenSourceFilesConfiguration getForProject(MavenProject project) throws } private final ImmutableList dependenciesFiles; + private final ImmutableList snapshotDependenciesFiles; private final ImmutableList resourcesFiles; private final ImmutableList classesFiles; @@ -53,12 +54,17 @@ private MavenSourceFilesConfiguration(MavenProject project) throws IOException { Path classesOutputDirectory = Paths.get(project.getBuild().getOutputDirectory()); List dependenciesFiles = new ArrayList<>(); + List snapshotDependenciesFiles = new ArrayList<>(); List resourcesFiles = new ArrayList<>(); List classesFiles = new ArrayList<>(); // Gets all the dependencies. for (Artifact artifact : project.getArtifacts()) { - dependenciesFiles.add(artifact.getFile().toPath()); + if (artifact.isSnapshot()) { + snapshotDependenciesFiles.add(artifact.getFile().toPath()); + } else { + dependenciesFiles.add(artifact.getFile().toPath()); + } } // Gets the classes files in the 'classes' output directory. It finds the files that are classes @@ -88,6 +94,7 @@ private MavenSourceFilesConfiguration(MavenProject project) throws IOException { // Sort all files by path for consistent ordering. this.dependenciesFiles = ImmutableList.sortedCopyOf(dependenciesFiles); + this.snapshotDependenciesFiles = ImmutableList.sortedCopyOf(snapshotDependenciesFiles); this.resourcesFiles = ImmutableList.sortedCopyOf(resourcesFiles); this.classesFiles = ImmutableList.sortedCopyOf(classesFiles); } @@ -97,6 +104,11 @@ public ImmutableList getDependenciesFiles() { return dependenciesFiles; } + @Override + public ImmutableList getSnapshotDependenciesFiles() { + return snapshotDependenciesFiles; + } + @Override public ImmutableList getResourcesFiles() { return resourcesFiles; @@ -112,6 +124,11 @@ public String getDependenciesPathOnImage() { return DEFAULT_DEPENDENCIES_PATH_ON_IMAGE; } + @Override + public String getSnapshotDependenciesPathOnImage() { + return DEFAULT_SNAPSHOT_DEPENDENCIES_PATH_ON_IMAGE; + } + @Override public String getResourcesPathOnImage() { return DEFAULT_RESOURCES_PATH_ON_IMAGE; diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java index b91fb61401..e2e8b6801c 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java @@ -17,19 +17,20 @@ package com.google.cloud.tools.jib.maven; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.common.io.Resources; import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.HashSet; import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Build; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -40,25 +41,30 @@ @RunWith(MockitoJUnitRunner.class) public class MavenSourceFilesConfigurationTest { + @Rule public TestRepository testRepository = new TestRepository(); + @Mock private MavenProject mockMavenProject; @Mock private Build mockBuild; private MavenSourceFilesConfiguration testMavenSourceFilesConfiguration; @Before - public void setUp() throws IOException, URISyntaxException { + public void setUp() throws IOException, URISyntaxException, ComponentLookupException { Path sourcePath = Paths.get(Resources.getResource("application/source").toURI()); Path outputPath = Paths.get(Resources.getResource("application/output").toURI()); Mockito.when(mockMavenProject.getBuild()).thenReturn(mockBuild); Mockito.when(mockBuild.getSourceDirectory()).thenReturn(sourcePath.toString()); Mockito.when(mockBuild.getOutputDirectory()).thenReturn(outputPath.toString()); + Set artifacts = - new HashSet<>( - Arrays.asList( - makeArtifact(Paths.get("application", "dependencies", "libraryB.jar")), - makeArtifact(Paths.get("application", "dependencies", "libraryA.jar")), - makeArtifact(Paths.get("application", "dependencies", "dependency-1.0.0.jar")))); + ImmutableSet.of( + makeArtifact(Paths.get("application", "dependencies", "libraryB.jar")), + makeArtifact(Paths.get("application", "dependencies", "libraryA.jar")), + // maven reads and populates "Artifacts" with it's own processing, so read some from + // a repository + testRepository.findArtifact("com.test", "dependency", "1.0.0"), + testRepository.findArtifact("com.test", "dependencyX", "1.0.0-SNAPSHOT")); Mockito.when(mockMavenProject.getArtifacts()).thenReturn(artifacts); testMavenSourceFilesConfiguration = @@ -69,9 +75,12 @@ public void setUp() throws IOException, URISyntaxException { public void test_correctFiles() throws URISyntaxException { ImmutableList expectedDependenciesFiles = ImmutableList.of( - Paths.get("application", "dependencies", "dependency-1.0.0.jar"), + testRepository.artifactPathOnDisk("com.test", "dependency", "1.0.0"), Paths.get("application", "dependencies", "libraryA.jar"), Paths.get("application", "dependencies", "libraryB.jar")); + ImmutableList expectedSnapshotDependenciesFiles = + ImmutableList.of( + testRepository.artifactPathOnDisk("com.test", "dependencyX", "1.0.0-SNAPSHOT")); ImmutableList expectedResourcesFiles = ImmutableList.of( Paths.get(Resources.getResource("application/output/directory").toURI()), @@ -86,6 +95,9 @@ public void test_correctFiles() throws URISyntaxException { Assert.assertEquals( expectedDependenciesFiles, testMavenSourceFilesConfiguration.getDependenciesFiles()); + Assert.assertEquals( + expectedSnapshotDependenciesFiles, + testMavenSourceFilesConfiguration.getSnapshotDependenciesFiles()); Assert.assertEquals( expectedResourcesFiles, testMavenSourceFilesConfiguration.getResourcesFiles()); Assert.assertEquals(expectedClassesFiles, testMavenSourceFilesConfiguration.getClassesFiles()); @@ -95,6 +107,9 @@ public void test_correctFiles() throws URISyntaxException { public void test_correctPathsOnImage() { Assert.assertEquals( "/app/libs/", testMavenSourceFilesConfiguration.getDependenciesPathOnImage()); + Assert.assertEquals( + "/app/snapshot-libs/", + testMavenSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()); Assert.assertEquals( "/app/resources/", testMavenSourceFilesConfiguration.getResourcesPathOnImage()); Assert.assertEquals("/app/classes/", testMavenSourceFilesConfiguration.getClassesPathOnImage()); diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/TestRepository.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/TestRepository.java new file mode 100644 index 0000000000..701fa470ed --- /dev/null +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/TestRepository.java @@ -0,0 +1,69 @@ +package com.google.cloud.tools.jib.maven; + +import com.google.common.io.Resources; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.nio.file.Path; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; +import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; +import org.apache.maven.artifact.resolver.ArtifactResolutionResult; +import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.plugin.testing.MojoRule; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.junit.Assert; +import org.junit.rules.ExternalResource; + +/** A test helper to resolve artifacts from a local repository in test/resources */ +public class TestRepository extends ExternalResource { + + private static final String TEST_M2 = "testM2"; + + private MojoRule testHarness; + private ArtifactRepositoryFactory artifactRepositoryFactory; + private ArtifactHandlerManager artifactHandlerManager; + private ArtifactRepository testLocalRepo; + private ArtifactResolver artifactResolver; + private ArtifactHandler jarHandler; + + @Override + protected void before() + throws ComponentLookupException, URISyntaxException, MalformedURLException { + testHarness = new MojoRule(); + artifactRepositoryFactory = testHarness.lookup(ArtifactRepositoryFactory.class); + artifactHandlerManager = testHarness.lookup(ArtifactHandlerManager.class); + artifactResolver = testHarness.lookup(ArtifactResolver.class); + jarHandler = artifactHandlerManager.getArtifactHandler("jar"); + + testLocalRepo = + artifactRepositoryFactory.createArtifactRepository( + "test", + Resources.getResource(TEST_M2).toURI().toURL().toString(), + new DefaultRepositoryLayout(), + null, + null); + } + + public Artifact findArtifact(String group, String artifact, String version) { + ArtifactResolutionRequest artifactResolutionRequest = new ArtifactResolutionRequest(); + artifactResolutionRequest.setLocalRepository(testLocalRepo); + Artifact artifactToFind = + new DefaultArtifact(group, artifact, version, null, "jar", null, jarHandler); + + artifactResolutionRequest.setArtifact(artifactToFind); + + ArtifactResolutionResult ars = artifactResolver.resolve(artifactResolutionRequest); + + Assert.assertEquals(1, ars.getArtifacts().size()); + return ars.getArtifacts().iterator().next(); + } + + public Path artifactPathOnDisk(String group, String artifact, String version) { + return findArtifact(group, artifact, version).getFile().toPath(); + } +} diff --git a/jib-maven-plugin/src/test/resources/application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar b/jib-maven-plugin/src/test/resources/application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..dd68926802ed2f42df108b18fa84681be324ae42 GIT binary patch literal 770 zcmWIWW@h1HVBp|j*cqkm!vF+KAOZ+Df!NnI#8KDN&rP41ApoxMWTdvwrh2A#(m(~0 zKrDi+(AUw=)6F$FM9k6RUH3HY=W$WPI3F<;f$>@(8#yf#wY7k_r*l!oSd4nQ}Uj${8L}2M(zxj&f2_7ErhM{W<=_n zn4f~eo6K)zRC^cf+H0w3%Q5Xy*zsE*RH`BiEP}b`Ki+g8_+Lz`)irD5-1%SIF39Qs zoO*Ur@Ar3oPHDUEhDL3?>+Q7qw)M9Kk4!q8`rnttMBF^S{!xl#r3lAa<+m+oY=74| ztdO;m7tj8tqc>raKO;x_2?gyr!c&`++aGxyTx7A%fHPfhzr%dbB`Py~&sd#mdYa21 zI)_6%z%|#rX5JkJzr#O0%4FTFo4zht_mMxqn~_O`8F!=ugB1t_7~VR9Xm}Dp)`l$! zfE0tkl15D+3D<^{AdpSK7Vlu~V2jIuOr)qq*NC19AR0kdFGSUdnkE9gS=m5JS%B~- Lkp2uzL<|f7lVIYI literal 0 HcmV?d00001 diff --git a/jib-maven-plugin/src/test/resources/testM2/com/test/dependency/1.0.0/_remote.repositories b/jib-maven-plugin/src/test/resources/testM2/com/test/dependency/1.0.0/_remote.repositories new file mode 100644 index 0000000000..d4aa0ba7cb --- /dev/null +++ b/jib-maven-plugin/src/test/resources/testM2/com/test/dependency/1.0.0/_remote.repositories @@ -0,0 +1,4 @@ +#NOTE: This is an Aether internal implementation file, its format can be changed without prior notice. +#Wed Jul 11 18:33:40 EDT 2018 +dependency-1.0.0.pom>= +dependency-1.0.0.jar>= diff --git a/jib-maven-plugin/src/test/resources/testM2/com/test/dependency/1.0.0/dependency-1.0.0.jar b/jib-maven-plugin/src/test/resources/testM2/com/test/dependency/1.0.0/dependency-1.0.0.jar new file mode 100644 index 0000000000000000000000000000000000000000..333ceb39b26dfc6384b092d18cee5733295b5b12 GIT binary patch literal 770 zcmWIWW@h1HVBp|jkd4;%VE_Uq5CH_7KE;?7qUY=O+4sz8A8%c~i@e^tTIbH3-yCFc#rVO~M^BlM3_Bc!YAZL; zu$0t-)V!3`yyQw0dqGN<#;Em5Gcqu&0Ai5cNJ`y{Qd3JZ^V0Q_a}tY-a|2HMF*ypX zNvpJf(%U(2Pb2&HL%xq&6=n(ev=qoss0r}MCP2FnzB>!p0NB=U#CXy43^H?yh|;Ft?_0= z>YJFKg2J23Z)H?_7wp<=sc6eF?NQkATOU-aA`2{nx#vILbRhU&Osmy3YvbJcU)wIo z>HnO1c2e*6cYRK2yYGfZZM^I4wEDL7w*`+(I-L67m&8QeJih)>ie#k-$64jKEoW?h z*Ey_^wUQUl{-&chVUj;1NBRi`?K#3zo0Quhc^zD2vCe=qU2ng`e9t8+GknijooafT z%OE<3Lp;DW*Su!l9R|O{KRwE1-K?9wE?M`HKfs%jNrV}9qyvK$2m~13I)Z3;5 + + 4.0.0 + com.test + dependency + 1.0.0 + POM was created from install:install-file + diff --git a/jib-maven-plugin/src/test/resources/testM2/com/test/dependency/maven-metadata-local.xml b/jib-maven-plugin/src/test/resources/testM2/com/test/dependency/maven-metadata-local.xml new file mode 100644 index 0000000000..d10105b967 --- /dev/null +++ b/jib-maven-plugin/src/test/resources/testM2/com/test/dependency/maven-metadata-local.xml @@ -0,0 +1,12 @@ + + + com.test + dependency + + 1.0.0 + + 1.0.0 + + 20180711223340 + + diff --git a/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/_remote.repositories b/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/_remote.repositories new file mode 100644 index 0000000000..b12c035277 --- /dev/null +++ b/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/_remote.repositories @@ -0,0 +1,4 @@ +#NOTE: This is an Aether internal implementation file, its format can be changed without prior notice. +#Wed Jul 11 18:35:24 EDT 2018 +dependencyX-1.0.0-SNAPSHOT.pom>= +dependencyX-1.0.0-SNAPSHOT.jar>= diff --git a/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/dependencyX-1.0.0-SNAPSHOT.jar b/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/dependencyX-1.0.0-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..333ceb39b26dfc6384b092d18cee5733295b5b12 GIT binary patch literal 770 zcmWIWW@h1HVBp|jkd4;%VE_Uq5CH_7KE;?7qUY=O+4sz8A8%c~i@e^tTIbH3-yCFc#rVO~M^BlM3_Bc!YAZL; zu$0t-)V!3`yyQw0dqGN<#;Em5Gcqu&0Ai5cNJ`y{Qd3JZ^V0Q_a}tY-a|2HMF*ypX zNvpJf(%U(2Pb2&HL%xq&6=n(ev=qoss0r}MCP2FnzB>!p0NB=U#CXy43^H?yh|;Ft?_0= z>YJFKg2J23Z)H?_7wp<=sc6eF?NQkATOU-aA`2{nx#vILbRhU&Osmy3YvbJcU)wIo z>HnO1c2e*6cYRK2yYGfZZM^I4wEDL7w*`+(I-L67m&8QeJih)>ie#k-$64jKEoW?h z*Ey_^wUQUl{-&chVUj;1NBRi`?K#3zo0Quhc^zD2vCe=qU2ng`e9t8+GknijooafT z%OE<3Lp;DW*Su!l9R|O{KRwE1-K?9wE?M`HKfs%jNrV}9qyvK$2m~13I)Z3;5 + + 4.0.0 + com.test + dependencyX + 1.0.0-SNAPSHOT + POM was created from install:install-file + diff --git a/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/maven-metadata-local.xml b/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/maven-metadata-local.xml new file mode 100644 index 0000000000..87f1ad7dcf --- /dev/null +++ b/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/1.0.0-SNAPSHOT/maven-metadata-local.xml @@ -0,0 +1,24 @@ + + + com.test + dependencyX + 1.0.0-SNAPSHOT + + + true + + 20180711223524 + + + jar + 1.0.0-SNAPSHOT + 20180711223524 + + + pom + 1.0.0-SNAPSHOT + 20180711223524 + + + + diff --git a/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/maven-metadata-local.xml b/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/maven-metadata-local.xml new file mode 100644 index 0000000000..8f606ea131 --- /dev/null +++ b/jib-maven-plugin/src/test/resources/testM2/com/test/dependencyX/maven-metadata-local.xml @@ -0,0 +1,11 @@ + + + com.test + dependencyX + + + 1.0.0-SNAPSHOT + + 20180711223524 + + From 47786afc78e85e6499409422c9883e47d7f5f80f Mon Sep 17 00:00:00 2001 From: Appu Goundan Date: Thu, 12 Jul 2018 13:12:19 -0400 Subject: [PATCH 2/4] Make snapshot dependencies also write to /app/libs --- .../cloud/tools/jib/builder/EntrypointBuilder.java | 1 - .../tools/jib/builder/SourceFilesConfiguration.java | 9 +-------- .../builder/steps/BuildAndCacheApplicationLayerStep.java | 2 +- .../cloud/tools/jib/docker/DockerContextGenerator.java | 2 +- .../cloud/tools/jib/builder/EntrypointBuilderTest.java | 5 +---- .../tools/jib/builder/TestSourceFilesConfiguration.java | 5 ----- .../steps/BuildAndCacheApplicationLayerStepTest.java | 2 +- .../tools/jib/docker/DockerContextGeneratorTest.java | 3 --- jib-core/src/test/resources/sampleDockerfile | 4 ++-- .../tools/jib/gradle/GradleSourceFilesConfiguration.java | 5 ----- .../jib/gradle/GradleSourceFilesConfigurationTest.java | 3 --- .../tools/jib/maven/MavenSourceFilesConfiguration.java | 5 ----- .../jib/maven/MavenSourceFilesConfigurationTest.java | 3 --- 13 files changed, 7 insertions(+), 42 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java index 19360ed864..2c1c06ffea 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.java @@ -37,7 +37,6 @@ public static ImmutableList makeEntrypoint( ImmutableList classPaths = ImmutableList.of( sourceFilesConfiguration.getDependenciesPathOnImage() + "*", - sourceFilesConfiguration.getSnapshotDependenciesPathOnImage() + "*", sourceFilesConfiguration.getResourcesPathOnImage(), sourceFilesConfiguration.getClassesPathOnImage()); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java index 0de4968f6b..695227c751 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/SourceFilesConfiguration.java @@ -26,7 +26,6 @@ public interface SourceFilesConfiguration { String DEFAULT_DEPENDENCIES_PATH_ON_IMAGE = "/app/libs/"; - String DEFAULT_SNAPSHOT_DEPENDENCIES_PATH_ON_IMAGE = "/app/snapshot-libs/"; String DEFAULT_RESOURCES_PATH_ON_IMAGE = "/app/resources/"; String DEFAULT_CLASSES_PATH_ON_IMAGE = "/app/classes/"; @@ -55,16 +54,10 @@ public interface SourceFilesConfiguration { /** * @return the Unix-style path where the dependencies source files are placed in the container - * filesystem. Must end with slash. + * filesystem. Must end with slash. This includes both regular and snapshot dependencies. */ String getDependenciesPathOnImage(); - /** - * @return the Unix-style path where the snapshot dependencies sources files are place in the - * container filesystem. Must end with slash. - */ - String getSnapshotDependenciesPathOnImage(); - /** * @return the Unix-style path where the resources source files are placed in the container * filesystem. Must end with slash. diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java index c63a932403..9ac74e24b1 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java @@ -71,7 +71,7 @@ static ImmutableList makeList( LayerConfiguration.builder() .addEntry( sourceFilesConfiguration.getSnapshotDependenciesFiles(), - sourceFilesConfiguration.getSnapshotDependenciesPathOnImage()) + sourceFilesConfiguration.getDependenciesPathOnImage()) .build(), cache)) .add( diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java index d8042a3205..83275229f2 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerContextGenerator.java @@ -184,7 +184,7 @@ String makeDockerfile() throws JsonProcessingException { .append("\n\nCOPY libs ") .append(sourceFilesConfiguration.getDependenciesPathOnImage()) .append("\nCOPY snapshot-libs ") - .append(sourceFilesConfiguration.getSnapshotDependenciesPathOnImage()) + .append(sourceFilesConfiguration.getDependenciesPathOnImage()) .append("\nCOPY resources ") .append(sourceFilesConfiguration.getResourcesPathOnImage()) .append("\nCOPY classes ") diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java index 7298365890..f6da756da0 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java @@ -28,7 +28,6 @@ public class EntrypointBuilderTest { @Test public void testMakeEntrypoint() { String expectedDependenciesPath = "/app/libs/"; - String expectedSnapshotDependenciesPath = "/app/snapshot-libs/"; String expectedResourcesPath = "/app/resources/"; String expectedClassesPath = "/app/classes/"; List expectedJvmFlags = Arrays.asList("-flag", "anotherFlag"); @@ -39,8 +38,6 @@ public void testMakeEntrypoint() { Mockito.when(mockSourceFilesConfiguration.getDependenciesPathOnImage()) .thenReturn(expectedDependenciesPath); - Mockito.when(mockSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()) - .thenReturn(expectedSnapshotDependenciesPath); Mockito.when(mockSourceFilesConfiguration.getResourcesPathOnImage()) .thenReturn(expectedResourcesPath); Mockito.when(mockSourceFilesConfiguration.getClassesPathOnImage()) @@ -52,7 +49,7 @@ public void testMakeEntrypoint() { "-flag", "anotherFlag", "-cp", - "/app/libs/*:/app/snapshot-libs/*:/app/resources/:/app/classes/", + "/app/libs/*:/app/resources/:/app/classes/", "SomeMainClass"), EntrypointBuilder.makeEntrypoint( mockSourceFilesConfiguration, expectedJvmFlags, expectedMainClass)); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java index 92354be594..8e3cbde6cf 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java @@ -67,11 +67,6 @@ public String getDependenciesPathOnImage() { return EXTRACTION_PATH + "libs/"; } - @Override - public String getSnapshotDependenciesPathOnImage() { - return EXTRACTION_PATH + "snapshot-libs/"; - } - @Override public String getResourcesPathOnImage() { return EXTRACTION_PATH + "resources/"; diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java index 297deff9c4..e7b9e2b890 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java @@ -106,7 +106,7 @@ public void testRun() ImmutableList.of( new LayerEntry( testSourceFilesConfiguration.getSnapshotDependenciesFiles(), - testSourceFilesConfiguration.getSnapshotDependenciesPathOnImage())); + testSourceFilesConfiguration.getDependenciesPathOnImage())); ImmutableList resourcesLayerEntry = ImmutableList.of( new LayerEntry( diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java index 838a74f4b3..8dc223fba9 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerContextGeneratorTest.java @@ -65,14 +65,11 @@ private static void assertSameFiles(Path directory1, Path directory2) throws IOE @Before public void setUpMocks() { String expectedDependenciesPath = "/app/libs/"; - String expectedSnapshotDependenciesPath = "/app/snapshot-libs/"; String expectedResourcesPath = "/app/resources/"; String expectedClassesPath = "/app/classes/"; Mockito.when(mockSourceFilesConfiguration.getDependenciesPathOnImage()) .thenReturn(expectedDependenciesPath); - Mockito.when(mockSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()) - .thenReturn(expectedSnapshotDependenciesPath); Mockito.when(mockSourceFilesConfiguration.getResourcesPathOnImage()) .thenReturn(expectedResourcesPath); Mockito.when(mockSourceFilesConfiguration.getClassesPathOnImage()) diff --git a/jib-core/src/test/resources/sampleDockerfile b/jib-core/src/test/resources/sampleDockerfile index d6617b1cde..474ea0c4e2 100644 --- a/jib-core/src/test/resources/sampleDockerfile +++ b/jib-core/src/test/resources/sampleDockerfile @@ -1,11 +1,11 @@ FROM somebaseimage COPY libs /app/libs/ -COPY snapshot-libs /app/snapshot-libs/ +COPY snapshot-libs /app/libs/ COPY resources /app/resources/ COPY classes /app/classes/ EXPOSE 1000/tcp EXPOSE 2000-2010/udp -ENTRYPOINT ["java","-flag","another\"Flag","-cp","/app/libs/*:/app/snapshot-libs/*:/app/resources/:/app/classes/","SomeMainClass"] +ENTRYPOINT ["java","-flag","another\"Flag","-cp","/app/libs/*:/app/resources/:/app/classes/","SomeMainClass"] CMD ["arg1","arg2"] diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java index c48795bd18..56397b79b9 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfiguration.java @@ -133,11 +133,6 @@ public String getDependenciesPathOnImage() { return DEFAULT_DEPENDENCIES_PATH_ON_IMAGE; } - @Override - public String getSnapshotDependenciesPathOnImage() { - return DEFAULT_SNAPSHOT_DEPENDENCIES_PATH_ON_IMAGE; - } - @Override public String getResourcesPathOnImage() { return DEFAULT_RESOURCES_PATH_ON_IMAGE; diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java index 5109f056a6..cd70617596 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleSourceFilesConfigurationTest.java @@ -167,9 +167,6 @@ public void test_noClassesFiles() throws IOException { public void test_correctPathsOnImage() { Assert.assertEquals( "/app/libs/", testGradleSourceFilesConfiguration.getDependenciesPathOnImage()); - Assert.assertEquals( - "/app/snapshot-libs/", - testGradleSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()); Assert.assertEquals( "/app/resources/", testGradleSourceFilesConfiguration.getResourcesPathOnImage()); Assert.assertEquals( diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java index 02279900ad..a3d919eaf5 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java @@ -124,11 +124,6 @@ public String getDependenciesPathOnImage() { return DEFAULT_DEPENDENCIES_PATH_ON_IMAGE; } - @Override - public String getSnapshotDependenciesPathOnImage() { - return DEFAULT_SNAPSHOT_DEPENDENCIES_PATH_ON_IMAGE; - } - @Override public String getResourcesPathOnImage() { return DEFAULT_RESOURCES_PATH_ON_IMAGE; diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java index e2e8b6801c..974f0a7082 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java @@ -107,9 +107,6 @@ public void test_correctFiles() throws URISyntaxException { public void test_correctPathsOnImage() { Assert.assertEquals( "/app/libs/", testMavenSourceFilesConfiguration.getDependenciesPathOnImage()); - Assert.assertEquals( - "/app/snapshot-libs/", - testMavenSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()); Assert.assertEquals( "/app/resources/", testMavenSourceFilesConfiguration.getResourcesPathOnImage()); Assert.assertEquals("/app/classes/", testMavenSourceFilesConfiguration.getClassesPathOnImage()); From a5314b17edea9c1773b5161ce9c7a6d1db87ada1 Mon Sep 17 00:00:00 2001 From: Appu Goundan Date: Thu, 12 Jul 2018 17:26:47 -0400 Subject: [PATCH 3/4] make snapshot optionally build + update test --- .../builder/BuildStepsIntegrationTest.java | 8 +- .../BuildAndCacheApplicationLayerStep.java | 25 ++-- .../builder/TestSourceFilesConfiguration.java | 65 +++++++-- ...BuildAndCacheApplicationLayerStepTest.java | 127 +++++++++++++++--- 4 files changed, 182 insertions(+), 43 deletions(-) diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java index 0e49e8fa68..0320e4d851 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java @@ -54,7 +54,13 @@ public class BuildStepsIntegrationTest { @Before public void setUp() throws IOException, URISyntaxException { - sourceFilesConfiguration = new TestSourceFilesConfiguration(); + sourceFilesConfiguration = + TestSourceFilesConfiguration.builder() + .withClasses() + .withDependencies() + .withSnapshotDependencies() + .withResources() + .build(); } @Test diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java index 9ac74e24b1..0536ea01f6 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java @@ -63,17 +63,6 @@ static ImmutableList makeList( sourceFilesConfiguration.getDependenciesPathOnImage()) .build(), cache)) - .add( - new BuildAndCacheApplicationLayerStep( - "snapshot-dependencies", - listeningExecutorService, - buildConfiguration, - LayerConfiguration.builder() - .addEntry( - sourceFilesConfiguration.getSnapshotDependenciesFiles(), - sourceFilesConfiguration.getDependenciesPathOnImage()) - .build(), - cache)) .add( new BuildAndCacheApplicationLayerStep( "resources", @@ -97,6 +86,20 @@ static ImmutableList makeList( .build(), cache)); + // Adds a snapshot dependencies layer, if snapshot files present. + if (!sourceFilesConfiguration.getSnapshotDependenciesFiles().isEmpty()) { + buildLayerStepsBuilder.add( + new BuildAndCacheApplicationLayerStep( + "snapshot-dependencies", + listeningExecutorService, + buildConfiguration, + LayerConfiguration.builder() + .addEntry( + sourceFilesConfiguration.getSnapshotDependenciesFiles(), + sourceFilesConfiguration.getDependenciesPathOnImage()) + .build(), + cache)); + } // Adds the extra layer to be built, if configured. if (buildConfiguration.getExtraFilesLayerConfiguration() != null) { buildLayerStepsBuilder.add( diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java index 8e3cbde6cf..84f051a5a2 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java @@ -35,11 +35,15 @@ public class TestSourceFilesConfiguration implements SourceFilesConfiguration { private final ImmutableList resourcesSourceFiles; private final ImmutableList classesSourceFiles; - public TestSourceFilesConfiguration() throws URISyntaxException, IOException { - dependenciesSourceFiles = getFilesList("application/dependencies"); - snapshotDependenciesSourceFiles = getFilesList("application/snapshot-dependencies"); - resourcesSourceFiles = getFilesList("application/resources"); - classesSourceFiles = getFilesList("application/classes"); + public TestSourceFilesConfiguration( + ImmutableList dependenciesSourceFiles, + ImmutableList snapshotDependenciesSourceFiles, + ImmutableList resourcesSourceFiles, + ImmutableList classesSourceFiles) { + this.dependenciesSourceFiles = dependenciesSourceFiles; + this.snapshotDependenciesSourceFiles = snapshotDependenciesSourceFiles; + this.resourcesSourceFiles = resourcesSourceFiles; + this.classesSourceFiles = classesSourceFiles; } @Override @@ -77,12 +81,51 @@ public String getClassesPathOnImage() { return EXTRACTION_PATH + "classes/"; } - /** Lists the files in the {@code resourcePath} resources directory. */ - private ImmutableList getFilesList(String resourcePath) - throws URISyntaxException, IOException { - try (Stream fileStream = - Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { - return fileStream.collect(ImmutableList.toImmutableList()); + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private ImmutableList dependenciesSourceFiles = ImmutableList.of(); + private ImmutableList snapshotDependenciesSourceFiles = ImmutableList.of(); + private ImmutableList resourcesSourceFiles = ImmutableList.of(); + private ImmutableList classesSourceFiles = ImmutableList.of(); + + public Builder withDependencies() throws IOException, URISyntaxException { + dependenciesSourceFiles = getFilesList("application/dependencies"); + return this; + } + + public Builder withSnapshotDependencies() throws IOException, URISyntaxException { + snapshotDependenciesSourceFiles = getFilesList("application/snapshot-dependencies"); + return this; + } + + public Builder withResources() throws IOException, URISyntaxException { + resourcesSourceFiles = getFilesList("application/resources"); + return this; + } + + public Builder withClasses() throws IOException, URISyntaxException { + classesSourceFiles = getFilesList("application/classes"); + return this; + } + + public TestSourceFilesConfiguration build() { + return new TestSourceFilesConfiguration( + dependenciesSourceFiles, + snapshotDependenciesSourceFiles, + resourcesSourceFiles, + classesSourceFiles); + } + + /** Lists the files in the {@code resourcePath} resources directory. */ + private ImmutableList getFilesList(String resourcePath) + throws URISyntaxException, IOException { + try (Stream fileStream = + Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { + return fileStream.collect(ImmutableList.toImmutableList()); + } } } } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java index e7b9e2b890..eacaf0659b 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStepTest.java @@ -18,6 +18,7 @@ import com.google.cloud.tools.jib.async.NonBlockingSteps; import com.google.cloud.tools.jib.builder.BuildConfiguration; +import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; import com.google.cloud.tools.jib.builder.TestBuildLogger; import com.google.cloud.tools.jib.builder.TestSourceFilesConfiguration; import com.google.cloud.tools.jib.cache.Cache; @@ -37,6 +38,7 @@ import java.nio.file.Paths; import java.util.concurrent.ExecutionException; import org.junit.Assert; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -49,30 +51,35 @@ @RunWith(MockitoJUnitRunner.class) public class BuildAndCacheApplicationLayerStepTest { + private static final String EXTRA_FILES_LAYER_EXTRACTION_PATH = "/extra"; + @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Mock private BuildConfiguration mockBuildConfiguration; + private Path temporaryCacheDirectory; - @Test - public void testRun() - throws LayerPropertyNotFoundException, IOException, CacheMetadataCorruptedException, - URISyntaxException, ExecutionException { + @Before + public void setUp() throws IOException, URISyntaxException { Mockito.when(mockBuildConfiguration.getBuildLogger()).thenReturn(new TestBuildLogger()); - TestSourceFilesConfiguration testSourceFilesConfiguration = new TestSourceFilesConfiguration(); - Path temporaryCacheDirectory = temporaryFolder.newFolder().toPath(); + temporaryCacheDirectory = temporaryFolder.newFolder().toPath(); + } - // Adds an extra file layer. + private ImmutableList configureExtraFilesLayer() throws URISyntaxException { ImmutableList extraFilesLayerSourceFiles = ImmutableList.of( Paths.get(Resources.getResource("fileA").toURI()), Paths.get(Resources.getResource("fileB").toURI())); - String extraFilesLayerExtractionPath = "/extra"; Mockito.when(mockBuildConfiguration.getExtraFilesLayerConfiguration()) .thenReturn( LayerConfiguration.builder() - .addEntry(extraFilesLayerSourceFiles, extraFilesLayerExtractionPath) + .addEntry(extraFilesLayerSourceFiles, EXTRA_FILES_LAYER_EXTRACTION_PATH) .build()); + return extraFilesLayerSourceFiles; + } + private ImageLayers configureAvailableLayers( + SourceFilesConfiguration testSourceFilesConfiguration) + throws CacheMetadataCorruptedException, IOException, ExecutionException { ImageLayers.Builder applicationLayersBuilder = ImageLayers.builder(); ImageLayers applicationLayers; @@ -91,8 +98,30 @@ public void testRun() applicationLayers = applicationLayersBuilder.build(); cache.addCachedLayersWithMetadataToMetadata(applicationLayers.getLayers()); - Assert.assertEquals(5, applicationLayers.size()); } + return applicationLayers; + } + + @Test + public void testRun() + throws LayerPropertyNotFoundException, IOException, CacheMetadataCorruptedException, + URISyntaxException, ExecutionException { + + TestSourceFilesConfiguration testSourceFilesConfiguration = + TestSourceFilesConfiguration.builder() + .withDependencies() + .withSnapshotDependencies() + .withClasses() + .withResources() + .build(); + + // Adds an extra file layer. + ImmutableList extraFilesLayerSourceFiles = configureExtraFilesLayer(); + + // Populate the cache + ImageLayers applicationLayers = + configureAvailableLayers(testSourceFilesConfiguration); + Assert.assertEquals(5, applicationLayers.size()); // Re-initialize cache with the updated metadata. Cache cache = Cache.init(temporaryCacheDirectory); @@ -118,7 +147,8 @@ public void testRun() testSourceFilesConfiguration.getClassesFiles(), testSourceFilesConfiguration.getClassesPathOnImage())); ImmutableList extraFilesLayerEntry = - ImmutableList.of(new LayerEntry(extraFilesLayerSourceFiles, extraFilesLayerExtractionPath)); + ImmutableList.of( + new LayerEntry(extraFilesLayerSourceFiles, EXTRA_FILES_LAYER_EXTRACTION_PATH)); // Verifies that the cached layers are up-to-date. CacheReader cacheReader = new CacheReader(cache); @@ -127,15 +157,15 @@ public void testRun() cacheReader.getUpToDateLayerByLayerEntries(dependenciesLayerEntry).getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(1).getBlobDescriptor(), - cacheReader - .getUpToDateLayerByLayerEntries(snapshotDependenciesLayerEntry) - .getBlobDescriptor()); + cacheReader.getUpToDateLayerByLayerEntries(resourcesLayerEntry).getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(2).getBlobDescriptor(), - cacheReader.getUpToDateLayerByLayerEntries(resourcesLayerEntry).getBlobDescriptor()); + cacheReader.getUpToDateLayerByLayerEntries(classesLayerEntry).getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(3).getBlobDescriptor(), - cacheReader.getUpToDateLayerByLayerEntries(classesLayerEntry).getBlobDescriptor()); + cacheReader + .getUpToDateLayerByLayerEntries(snapshotDependenciesLayerEntry) + .getBlobDescriptor()); Assert.assertEquals( applicationLayers.get(4).getBlobDescriptor(), cacheReader.getUpToDateLayerByLayerEntries(extraFilesLayerEntry).getBlobDescriptor()); @@ -145,13 +175,70 @@ public void testRun() applicationLayers.get(0).getContentFile(), cacheReader.getLayerFile(dependenciesLayerEntry)); Assert.assertEquals( - applicationLayers.get(1).getContentFile(), - cacheReader.getLayerFile(snapshotDependenciesLayerEntry)); + applicationLayers.get(1).getContentFile(), cacheReader.getLayerFile(resourcesLayerEntry)); Assert.assertEquals( - applicationLayers.get(2).getContentFile(), cacheReader.getLayerFile(resourcesLayerEntry)); + applicationLayers.get(2).getContentFile(), cacheReader.getLayerFile(classesLayerEntry)); Assert.assertEquals( - applicationLayers.get(3).getContentFile(), cacheReader.getLayerFile(classesLayerEntry)); + applicationLayers.get(3).getContentFile(), + cacheReader.getLayerFile(snapshotDependenciesLayerEntry)); Assert.assertEquals( applicationLayers.get(4).getContentFile(), cacheReader.getLayerFile(extraFilesLayerEntry)); } + + @Test + public void testRun_emptyLayersIgnored() + throws IOException, URISyntaxException, CacheMetadataCorruptedException, ExecutionException { + + TestSourceFilesConfiguration testSourceFilesConfiguration = + TestSourceFilesConfiguration.builder() + .withDependencies() + .withClasses() + .withResources() + .build(); + + // Populate the cache + ImageLayers applicationLayers = + configureAvailableLayers(testSourceFilesConfiguration); + Assert.assertEquals(3, applicationLayers.size()); + + ImmutableList dependenciesLayerEntry = + ImmutableList.of( + new LayerEntry( + testSourceFilesConfiguration.getDependenciesFiles(), + testSourceFilesConfiguration.getDependenciesPathOnImage())); + ImmutableList resourcesLayerEntry = + ImmutableList.of( + new LayerEntry( + testSourceFilesConfiguration.getResourcesFiles(), + testSourceFilesConfiguration.getResourcesPathOnImage())); + ImmutableList classesLayerEntry = + ImmutableList.of( + new LayerEntry( + testSourceFilesConfiguration.getClassesFiles(), + testSourceFilesConfiguration.getClassesPathOnImage())); + + // Re-initialize cache with the updated metadata. + Cache cache = Cache.init(temporaryCacheDirectory); + + // Verifies that the cached layers are up-to-date. + CacheReader cacheReader = new CacheReader(cache); + Assert.assertEquals( + applicationLayers.get(0).getBlobDescriptor(), + cacheReader.getUpToDateLayerByLayerEntries(dependenciesLayerEntry).getBlobDescriptor()); + Assert.assertEquals( + applicationLayers.get(1).getBlobDescriptor(), + cacheReader.getUpToDateLayerByLayerEntries(resourcesLayerEntry).getBlobDescriptor()); + Assert.assertEquals( + applicationLayers.get(2).getBlobDescriptor(), + cacheReader.getUpToDateLayerByLayerEntries(classesLayerEntry).getBlobDescriptor()); + + // Verifies that the cache reader gets the same layers as the newest application layers. + Assert.assertEquals( + applicationLayers.get(0).getContentFile(), + cacheReader.getLayerFile(dependenciesLayerEntry)); + Assert.assertEquals( + applicationLayers.get(1).getContentFile(), cacheReader.getLayerFile(resourcesLayerEntry)); + Assert.assertEquals( + applicationLayers.get(2).getContentFile(), cacheReader.getLayerFile(classesLayerEntry)); + } } From ea02e672fbe448a78b8ecafea8c0e4a91225029b Mon Sep 17 00:00:00 2001 From: Appu Goundan Date: Thu, 12 Jul 2018 17:46:00 -0400 Subject: [PATCH 4/4] Style, Fix Tests, Add to CHANGELOG --- .../builder/TestSourceFilesConfiguration.java | 98 +++++++++---------- jib-gradle-plugin/CHANGELOG.md | 1 + jib-maven-plugin/CHANGELOG.md | 1 + .../MavenSourceFilesConfigurationTest.java | 10 +- 4 files changed, 57 insertions(+), 53 deletions(-) diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java index 84f051a5a2..33396e3295 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/TestSourceFilesConfiguration.java @@ -28,6 +28,54 @@ /** Implementation of {@link SourceFilesConfiguration} that uses test resources. */ public class TestSourceFilesConfiguration implements SourceFilesConfiguration { + public static class Builder { + private ImmutableList dependenciesSourceFiles = ImmutableList.of(); + private ImmutableList snapshotDependenciesSourceFiles = ImmutableList.of(); + private ImmutableList resourcesSourceFiles = ImmutableList.of(); + private ImmutableList classesSourceFiles = ImmutableList.of(); + + public Builder withDependencies() throws IOException, URISyntaxException { + dependenciesSourceFiles = getFilesList("application/dependencies"); + return this; + } + + public Builder withSnapshotDependencies() throws IOException, URISyntaxException { + snapshotDependenciesSourceFiles = getFilesList("application/snapshot-dependencies"); + return this; + } + + public Builder withResources() throws IOException, URISyntaxException { + resourcesSourceFiles = getFilesList("application/resources"); + return this; + } + + public Builder withClasses() throws IOException, URISyntaxException { + classesSourceFiles = getFilesList("application/classes"); + return this; + } + + public TestSourceFilesConfiguration build() { + return new TestSourceFilesConfiguration( + dependenciesSourceFiles, + snapshotDependenciesSourceFiles, + resourcesSourceFiles, + classesSourceFiles); + } + + /** Lists the files in the {@code resourcePath} resources directory. */ + private ImmutableList getFilesList(String resourcePath) + throws URISyntaxException, IOException { + try (Stream fileStream = + Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { + return fileStream.collect(ImmutableList.toImmutableList()); + } + } + } + + public static Builder builder() { + return new Builder(); + } + private static final String EXTRACTION_PATH = "/some/extraction/path/"; private final ImmutableList dependenciesSourceFiles; @@ -35,7 +83,7 @@ public class TestSourceFilesConfiguration implements SourceFilesConfiguration { private final ImmutableList resourcesSourceFiles; private final ImmutableList classesSourceFiles; - public TestSourceFilesConfiguration( + private TestSourceFilesConfiguration( ImmutableList dependenciesSourceFiles, ImmutableList snapshotDependenciesSourceFiles, ImmutableList resourcesSourceFiles, @@ -80,52 +128,4 @@ public String getResourcesPathOnImage() { public String getClassesPathOnImage() { return EXTRACTION_PATH + "classes/"; } - - public static Builder builder() { - return new Builder(); - } - - public static class Builder { - private ImmutableList dependenciesSourceFiles = ImmutableList.of(); - private ImmutableList snapshotDependenciesSourceFiles = ImmutableList.of(); - private ImmutableList resourcesSourceFiles = ImmutableList.of(); - private ImmutableList classesSourceFiles = ImmutableList.of(); - - public Builder withDependencies() throws IOException, URISyntaxException { - dependenciesSourceFiles = getFilesList("application/dependencies"); - return this; - } - - public Builder withSnapshotDependencies() throws IOException, URISyntaxException { - snapshotDependenciesSourceFiles = getFilesList("application/snapshot-dependencies"); - return this; - } - - public Builder withResources() throws IOException, URISyntaxException { - resourcesSourceFiles = getFilesList("application/resources"); - return this; - } - - public Builder withClasses() throws IOException, URISyntaxException { - classesSourceFiles = getFilesList("application/classes"); - return this; - } - - public TestSourceFilesConfiguration build() { - return new TestSourceFilesConfiguration( - dependenciesSourceFiles, - snapshotDependenciesSourceFiles, - resourcesSourceFiles, - classesSourceFiles); - } - - /** Lists the files in the {@code resourcePath} resources directory. */ - private ImmutableList getFilesList(String resourcePath) - throws URISyntaxException, IOException { - try (Stream fileStream = - Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { - return fileStream.collect(ImmutableList.toImmutableList()); - } - } - } } diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index c3426cb817..16093e1766 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ## [unreleased] ### Added +- Snapshot dependencies are added as their own layer ([#584](https://github.com/GoogleContainerTools/jib/pull/584)) ### Changed diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index fa707cb743..fbff4fe022 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ## [unreleased] ### Added +- Snapshot dependencies are added as their own layer ([#584](https://github.com/GoogleContainerTools/jib/pull/584)) ### Changed diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java index 974f0a7082..f43e2c8cf3 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java @@ -74,10 +74,12 @@ public void setUp() throws IOException, URISyntaxException, ComponentLookupExcep @Test public void test_correctFiles() throws URISyntaxException { ImmutableList expectedDependenciesFiles = - ImmutableList.of( - testRepository.artifactPathOnDisk("com.test", "dependency", "1.0.0"), - Paths.get("application", "dependencies", "libraryA.jar"), - Paths.get("application", "dependencies", "libraryB.jar")); + // on windows, these files may be in a different order, so sort + ImmutableList.sortedCopyOf( + ImmutableList.of( + testRepository.artifactPathOnDisk("com.test", "dependency", "1.0.0"), + Paths.get("application", "dependencies", "libraryA.jar"), + Paths.get("application", "dependencies", "libraryB.jar"))); ImmutableList expectedSnapshotDependenciesFiles = ImmutableList.of( testRepository.artifactPathOnDisk("com.test", "dependencyX", "1.0.0-SNAPSHOT"));