Skip to content

Commit

Permalink
Add support for SNAPSHOT dependencies layer
Browse files Browse the repository at this point in the history
  • Loading branch information
loosebazooka committed Jul 12, 2018
1 parent 4e5f2a7 commit bb71d9f
Show file tree
Hide file tree
Showing 171 changed files with 8,256 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static ImmutableList<String> makeEntrypoint(
ImmutableList<String> classPaths =
ImmutableList.of(
sourceFilesConfiguration.getDependenciesPathOnImage() + "*",
sourceFilesConfiguration.getSnapshotDependenciesPathOnImage() + "*",
sourceFilesConfiguration.getResourcesPathOnImage(),
sourceFilesConfiguration.getClassesPathOnImage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

Expand All @@ -35,6 +36,12 @@ public interface SourceFilesConfiguration {
*/
ImmutableList<Path> getDependenciesFiles();

/**
* @return the source files for snapshot dependencies. These files should be in a deterministic
* order
*/
ImmutableList<Path> getSnapshotDependenciesFiles();

/**
* @return the source files for the resources layer. These files should be in a deterministic
* order.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ static ImmutableList<BuildAndCacheApplicationLayerStep> 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> expectedJvmFlags = Arrays.asList("-flag", "anotherFlag");
Expand All @@ -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())
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public class TestSourceFilesConfiguration implements SourceFilesConfiguration {
private static final String EXTRACTION_PATH = "/some/extraction/path/";

private final ImmutableList<Path> dependenciesSourceFiles;
private final ImmutableList<Path> snapshotDependenciesSourceFiles;
private final ImmutableList<Path> resourcesSourceFiles;
private final ImmutableList<Path> classesSourceFiles;

public TestSourceFilesConfiguration() throws URISyntaxException, IOException {
dependenciesSourceFiles = getFilesList("application/dependencies");
snapshotDependenciesSourceFiles = getFilesList("application/snapshot-dependencies");
resourcesSourceFiles = getFilesList("application/resources");
classesSourceFiles = getFilesList("application/classes");
}
Expand All @@ -45,6 +47,11 @@ public ImmutableList<Path> getDependenciesFiles() {
return dependenciesSourceFiles;
}

@Override
public ImmutableList<Path> getSnapshotDependenciesFiles() {
return snapshotDependenciesSourceFiles;
}

@Override
public ImmutableList<Path> getResourcesFiles() {
return resourcesSourceFiles;
Expand All @@ -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/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -102,6 +102,11 @@ public void testRun()
new LayerEntry(
testSourceFilesConfiguration.getDependenciesFiles(),
testSourceFilesConfiguration.getDependenciesPathOnImage()));
ImmutableList<LayerEntry> snapshotDependenciesLayerEntry =
ImmutableList.of(
new LayerEntry(
testSourceFilesConfiguration.getSnapshotDependenciesFiles(),
testSourceFilesConfiguration.getSnapshotDependenciesPathOnImage()));
ImmutableList<LayerEntry> resourcesLayerEntry =
ImmutableList.of(
new LayerEntry(
Expand All @@ -122,23 +127,31 @@ 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.
Assert.assertEquals(
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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<Path> expectedDependenciesFiles =
new DirectoryWalker(testDependencies).filterRoot().walk();
ImmutableList<Path> expectedSnapshotDependenciesFiles =
new DirectoryWalker(testSnapshotDependencies).filterRoot().walk();
ImmutableList<Path> expectedResourcesFiles =
new DirectoryWalker(testResources).filterRoot().walk();
ImmutableList<Path> 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);
Expand All @@ -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);
}
Expand Down
Binary file not shown.
3 changes: 2 additions & 1 deletion jib-core/src/test/resources/sampleDockerfile
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static GradleSourceFilesConfiguration getForProject(
}

private final ImmutableList<Path> dependenciesFiles;
private final ImmutableList<Path> snapshotDependenciesFiles;
private final ImmutableList<Path> resourcesFiles;
private final ImmutableList<Path> classesFiles;

Expand All @@ -55,6 +56,7 @@ private GradleSourceFilesConfiguration(Project project, GradleBuildLogger gradle
SourceSet mainSourceSet = javaPluginConvention.getSourceSets().getByName(MAIN_SOURCE_SET_NAME);

List<Path> dependenciesFiles = new ArrayList<>();
List<Path> snapshotDependenciesFiles = new ArrayList<>();
List<Path> resourcesFiles = new ArrayList<>();
List<Path> classesFiles = new ArrayList<>();

Expand Down Expand Up @@ -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);
}
Expand All @@ -106,6 +113,11 @@ public ImmutableList<Path> getDependenciesFiles() {
return dependenciesFiles;
}

@Override
public ImmutableList<Path> getSnapshotDependenciesFiles() {
return snapshotDependenciesFiles;
}

@Override
public ImmutableList<Path> getResourcesFiles() {
return resourcesFiles;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Path> expectedSnapshotDependenciesFiles =
ImmutableList.of(
Paths.get(
Resources.getResource("application/dependencies/dependencyX-1.0.0-SNAPSHOT.jar")
.toURI()));
ImmutableList<Path> expectedResourcesFiles =
ImmutableList.of(
Paths.get(Resources.getResource("application/resources").toURI()).resolve("resourceA"),
Expand All @@ -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());
Expand All @@ -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(
Expand Down
Binary file not shown.
14 changes: 14 additions & 0 deletions jib-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@
<version>2.12.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.5.4</version>
<scope>test</scope>
</dependency>

</dependencies>

<profiles>
Expand Down
Loading

0 comments on commit bb71d9f

Please sign in to comment.