Search in sources :

Example 1 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project jib by GoogleContainerTools.

the class StandardExplodedProcessorTest method testCreateLayers_withoutClassPathInManifest_containsOnlyClasses.

@Test
public void testCreateLayers_withoutClassPathInManifest_containsOnlyClasses() throws IOException, URISyntaxException {
    Path standardJar = Paths.get(Resources.getResource(STANDARD_JAR_WITH_ONLY_CLASSES).toURI());
    Path destDir = temporaryFolder.newFolder().toPath();
    StandardExplodedProcessor standardExplodedModeProcessor = new StandardExplodedProcessor(standardJar, destDir, JAR_JAVA_VERSION);
    List<FileEntriesLayer> layers = standardExplodedModeProcessor.createLayers();
    assertThat(layers.size()).isEqualTo(2);
    FileEntriesLayer resourcesLayer = layers.get(0);
    FileEntriesLayer classesLayer = layers.get(1);
    // Validate resources layer.
    assertThat(resourcesLayer.getEntries().size()).isEqualTo(1);
    assertThat(resourcesLayer.getEntries().get(0).getExtractionPath()).isEqualTo(AbsoluteUnixPath.get("/app/explodedJar/META-INF/MANIFEST.MF"));
    // Validate classes layer.
    List<AbsoluteUnixPath> actualClassesPath = classesLayer.getEntries().stream().map(FileEntry::getExtractionPath).collect(Collectors.toList());
    assertThat(actualClassesPath).containsExactly(AbsoluteUnixPath.get("/app/explodedJar/class1.class"), AbsoluteUnixPath.get("/app/explodedJar/class2.class"));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Test(org.junit.Test)

Example 2 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project jib by GoogleContainerTools.

the class JavaContainerBuilderTest method testToJibContainerBuilder_setAppRootLate.

@Test
public void testToJibContainerBuilder_setAppRootLate() throws URISyntaxException, IOException, InvalidImageReferenceException, CacheDirectoryCreationException {
    BuildContext buildContext = JavaContainerBuilder.from("scratch").addClasses(getResource("core/application/classes")).addResources(getResource("core/application/resources")).addDependencies(getResource("core/application/dependencies/libraryA.jar")).addToClasspath(getResource("core/fileA")).setAppRoot("/different").setMainClass("HelloWorld").toContainerBuilder().toBuildContext(Containerizer.to(RegistryImage.named("hello")));
    // Check entrypoint
    Assert.assertEquals(ImmutableList.of("java", "-cp", "/different/classes:/different/resources:/different/libs/*:/different/classpath", "HelloWorld"), buildContext.getContainerConfiguration().getEntrypoint());
    // Check classes
    List<AbsoluteUnixPath> expectedClasses = ImmutableList.of(AbsoluteUnixPath.get("/different/classes/HelloWorld.class"), AbsoluteUnixPath.get("/different/classes/some.class"));
    Assert.assertEquals(expectedClasses, getExtractionPaths(buildContext, "classes"));
    // Check resources
    List<AbsoluteUnixPath> expectedResources = ImmutableList.of(AbsoluteUnixPath.get("/different/resources/resourceA"), AbsoluteUnixPath.get("/different/resources/resourceB"), AbsoluteUnixPath.get("/different/resources/world"));
    Assert.assertEquals(expectedResources, getExtractionPaths(buildContext, "resources"));
    // Check dependencies
    List<AbsoluteUnixPath> expectedDependencies = ImmutableList.of(AbsoluteUnixPath.get("/different/libs/libraryA.jar"));
    Assert.assertEquals(expectedDependencies, getExtractionPaths(buildContext, "dependencies"));
    Assert.assertEquals(expectedClasses, getExtractionPaths(buildContext, "classes"));
    // Check additional classpath files
    List<AbsoluteUnixPath> expectedOthers = ImmutableList.of(AbsoluteUnixPath.get("/different/classpath/fileA"));
    Assert.assertEquals(expectedOthers, getExtractionPaths(buildContext, "extra files"));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Test(org.junit.Test)

Example 3 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project jib by GoogleContainerTools.

the class JavaContainerBuilderTest method testToJibContainerBuilder_all.

@Test
public void testToJibContainerBuilder_all() throws InvalidImageReferenceException, URISyntaxException, IOException, CacheDirectoryCreationException {
    BuildContext buildContext = JavaContainerBuilder.from("scratch").setAppRoot("/hello").addResources(getResource("core/application/resources")).addClasses(getResource("core/application/classes")).addDependencies(getResource("core/application/dependencies/dependency-1.0.0.jar"), getResource("core/application/dependencies/more/dependency-1.0.0.jar")).addSnapshotDependencies(getResource("core/application/snapshot-dependencies/dependency-1.0.0-SNAPSHOT.jar")).addProjectDependencies(getResource("core/application/dependencies/libraryA.jar"), getResource("core/application/dependencies/libraryB.jar")).addToClasspath(getResource("core/fileA"), getResource("core/fileB")).setClassesDestination(RelativeUnixPath.get("different-classes")).setResourcesDestination(RelativeUnixPath.get("different-resources")).setDependenciesDestination(RelativeUnixPath.get("different-libs")).setOthersDestination(RelativeUnixPath.get("different-classpath")).addJvmFlags("-xflag1", "-xflag2").setMainClass("HelloWorld").toContainerBuilder().toBuildContext(Containerizer.to(RegistryImage.named("hello")));
    // Check entrypoint
    Assert.assertEquals(ImmutableList.of("java", "-xflag1", "-xflag2", "-cp", "/hello/different-resources:/hello/different-classes:/hello/different-libs/*:/hello/different-classpath", "HelloWorld"), buildContext.getContainerConfiguration().getEntrypoint());
    // Check dependencies
    List<AbsoluteUnixPath> expectedDependencies = ImmutableList.of(AbsoluteUnixPath.get("/hello/different-libs/dependency-1.0.0-770.jar"), AbsoluteUnixPath.get("/hello/different-libs/dependency-1.0.0-200.jar"));
    Assert.assertEquals(expectedDependencies, getExtractionPaths(buildContext, "dependencies"));
    // Check snapshots
    List<AbsoluteUnixPath> expectedSnapshotDependencies = ImmutableList.of(AbsoluteUnixPath.get("/hello/different-libs/dependency-1.0.0-SNAPSHOT.jar"));
    Assert.assertEquals(expectedSnapshotDependencies, getExtractionPaths(buildContext, "snapshot dependencies"));
    List<AbsoluteUnixPath> expectedProjectDependencies = ImmutableList.of(AbsoluteUnixPath.get("/hello/different-libs/libraryA.jar"), AbsoluteUnixPath.get("/hello/different-libs/libraryB.jar"));
    Assert.assertEquals(expectedProjectDependencies, getExtractionPaths(buildContext, "project dependencies"));
    // Check resources
    List<AbsoluteUnixPath> expectedResources = ImmutableList.of(AbsoluteUnixPath.get("/hello/different-resources/resourceA"), AbsoluteUnixPath.get("/hello/different-resources/resourceB"), AbsoluteUnixPath.get("/hello/different-resources/world"));
    Assert.assertEquals(expectedResources, getExtractionPaths(buildContext, "resources"));
    // Check classes
    List<AbsoluteUnixPath> expectedClasses = ImmutableList.of(AbsoluteUnixPath.get("/hello/different-classes/HelloWorld.class"), AbsoluteUnixPath.get("/hello/different-classes/some.class"));
    Assert.assertEquals(expectedClasses, getExtractionPaths(buildContext, "classes"));
    // Check additional classpath files
    List<AbsoluteUnixPath> expectedOthers = ImmutableList.of(AbsoluteUnixPath.get("/hello/different-classpath/fileA"), AbsoluteUnixPath.get("/hello/different-classpath/fileB"));
    Assert.assertEquals(expectedOthers, getExtractionPaths(buildContext, "extra files"));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Test(org.junit.Test)

Example 4 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project jib by GoogleContainerTools.

the class JavaContainerBuilderTest method testToJibContainerBuilder_missingAndMultipleAdds.

@Test
public void testToJibContainerBuilder_missingAndMultipleAdds() throws InvalidImageReferenceException, URISyntaxException, IOException, CacheDirectoryCreationException {
    BuildContext buildContext = JavaContainerBuilder.from("scratch").addDependencies(getResource("core/application/dependencies/libraryA.jar")).addDependencies(getResource("core/application/dependencies/libraryB.jar")).addSnapshotDependencies(getResource("core/application/snapshot-dependencies/dependency-1.0.0-SNAPSHOT.jar")).addClasses(getResource("core/application/classes/")).addClasses(getResource("core/class-finder-tests/extension")).setMainClass("HelloWorld").toContainerBuilder().toBuildContext(Containerizer.to(RegistryImage.named("hello")));
    // Check entrypoint
    Assert.assertEquals(ImmutableList.of("java", "-cp", "/app/libs/*:/app/classes", "HelloWorld"), buildContext.getContainerConfiguration().getEntrypoint());
    // Check dependencies
    List<AbsoluteUnixPath> expectedDependencies = ImmutableList.of(AbsoluteUnixPath.get("/app/libs/libraryA.jar"), AbsoluteUnixPath.get("/app/libs/libraryB.jar"));
    Assert.assertEquals(expectedDependencies, getExtractionPaths(buildContext, "dependencies"));
    // Check snapshots
    List<AbsoluteUnixPath> expectedSnapshotDependencies = ImmutableList.of(AbsoluteUnixPath.get("/app/libs/dependency-1.0.0-SNAPSHOT.jar"));
    Assert.assertEquals(expectedSnapshotDependencies, getExtractionPaths(buildContext, "snapshot dependencies"));
    // Check classes
    List<AbsoluteUnixPath> expectedClasses = ImmutableList.of(AbsoluteUnixPath.get("/app/classes/HelloWorld.class"), AbsoluteUnixPath.get("/app/classes/some.class"), AbsoluteUnixPath.get("/app/classes/main/"), AbsoluteUnixPath.get("/app/classes/main/MainClass.class"), AbsoluteUnixPath.get("/app/classes/pack/"), AbsoluteUnixPath.get("/app/classes/pack/Apple.class"), AbsoluteUnixPath.get("/app/classes/pack/Orange.class"));
    Assert.assertEquals(expectedClasses, getExtractionPaths(buildContext, "classes"));
    // Check empty layers
    Assert.assertEquals(ImmutableList.of(), getExtractionPaths(buildContext, "resources"));
    Assert.assertEquals(ImmutableList.of(), getExtractionPaths(buildContext, "extra files"));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Test(org.junit.Test)

Example 5 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project jib by GoogleContainerTools.

the class JavaContainerBuilder method addDirectoryContentsToLayer.

private void addDirectoryContentsToLayer(Map<LayerType, FileEntriesLayer.Builder> layerBuilders, LayerType layerType, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
    if (!layerBuilders.containsKey(layerType)) {
        layerBuilders.put(layerType, FileEntriesLayer.builder());
    }
    FileEntriesLayer.Builder builder = layerBuilders.get(layerType);
    new DirectoryWalker(sourceRoot).filterRoot().filter(path -> Files.isDirectory(path) || pathFilter.test(path)).walk(path -> {
        AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
        Instant modificationTime = modificationTimeProvider.get(path, pathOnContainer);
        builder.addEntry(path, pathOnContainer, modificationTime);
    });
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) Streams(com.google.common.collect.Streams) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ProjectInfo(com.google.cloud.tools.jib.ProjectInfo) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) Preconditions(com.google.common.base.Preconditions) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Aggregations

AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)46 Path (java.nio.file.Path)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)27 IOException (java.io.IOException)18 Test (org.junit.Test)18 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)15 Files (java.nio.file.Files)14 Map (java.util.Map)14 List (java.util.List)13 Instant (java.time.Instant)11 ArrayList (java.util.ArrayList)10 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)9 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 Optional (java.util.Optional)8 Predicate (java.util.function.Predicate)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)7 BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)7