Search in sources :

Example 21 with AbsoluteUnixPath

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

the class StandardExplodedProcessorTest method testCreateLayers_withClassPathInManifest.

@Test
public void testCreateLayers_withClassPathInManifest() throws IOException, URISyntaxException {
    Path standardJar = Paths.get(Resources.getResource(STANDARD_JAR_WITH_CLASS_PATH_MANIFEST).toURI());
    Path destDir = temporaryFolder.newFolder().toPath();
    StandardExplodedProcessor standardExplodedModeProcessor = new StandardExplodedProcessor(standardJar, destDir, JAR_JAVA_VERSION);
    List<FileEntriesLayer> layers = standardExplodedModeProcessor.createLayers();
    assertThat(layers.size()).isEqualTo(4);
    FileEntriesLayer nonSnapshotLayer = layers.get(0);
    FileEntriesLayer snapshotLayer = layers.get(1);
    FileEntriesLayer resourcesLayer = layers.get(2);
    FileEntriesLayer classesLayer = layers.get(3);
    // Validate dependencies layers.
    assertThat(nonSnapshotLayer.getName()).isEqualTo("dependencies");
    assertThat(nonSnapshotLayer.getEntries().stream().map(FileEntry::getExtractionPath).collect(Collectors.toList())).isEqualTo(ImmutableList.of(AbsoluteUnixPath.get("/app/dependencies/dependency1"), AbsoluteUnixPath.get("/app/dependencies/dependency2"), AbsoluteUnixPath.get("/app/dependencies/dependency4")));
    assertThat(snapshotLayer.getName()).isEqualTo("snapshot dependencies");
    assertThat(snapshotLayer.getEntries().size()).isEqualTo(1);
    assertThat(snapshotLayer.getEntries().get(0).getExtractionPath()).isEqualTo(AbsoluteUnixPath.get("/app/dependencies/dependency3-SNAPSHOT-1.jar"));
    // Validate resources layer.
    assertThat(resourcesLayer.getName()).isEqualTo("resources");
    List<AbsoluteUnixPath> actualResourcesPaths = resourcesLayer.getEntries().stream().map(FileEntry::getExtractionPath).collect(Collectors.toList());
    assertThat(actualResourcesPaths).containsExactly(AbsoluteUnixPath.get("/app/explodedJar/META-INF/MANIFEST.MF"), AbsoluteUnixPath.get("/app/explodedJar/directory1/resource1.txt"), AbsoluteUnixPath.get("/app/explodedJar/directory2/directory3/resource2.sql"), AbsoluteUnixPath.get("/app/explodedJar/directory4/resource3.txt"), AbsoluteUnixPath.get("/app/explodedJar/resource4.sql"));
    // Validate classes layer.
    assertThat(classesLayer.getName()).isEqualTo("classes");
    List<AbsoluteUnixPath> actualClassesPaths = classesLayer.getEntries().stream().map(FileEntry::getExtractionPath).collect(Collectors.toList());
    assertThat(actualClassesPaths).containsExactly(AbsoluteUnixPath.get("/app/explodedJar/class5.class"), AbsoluteUnixPath.get("/app/explodedJar/directory1/class1.class"), AbsoluteUnixPath.get("/app/explodedJar/directory1/class2.class"), AbsoluteUnixPath.get("/app/explodedJar/directory2/class4.class"), AbsoluteUnixPath.get("/app/explodedJar/directory2/directory3/class3.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 22 with AbsoluteUnixPath

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

the class StandardExplodedProcessorTest method testCreateLayers_withoutClassPathInManifest.

@Test
public void testCreateLayers_withoutClassPathInManifest() throws IOException, URISyntaxException {
    Path standardJar = Paths.get(Resources.getResource(STANDARD_JAR_WITHOUT_CLASS_PATH_MANIFEST).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.getName()).isEqualTo("resources");
    List<AbsoluteUnixPath> actualResourcesPaths = resourcesLayer.getEntries().stream().map(FileEntry::getExtractionPath).collect(Collectors.toList());
    assertThat(actualResourcesPaths).containsExactly(AbsoluteUnixPath.get("/app/explodedJar/META-INF/MANIFEST.MF"), AbsoluteUnixPath.get("/app/explodedJar/directory1/resource1.txt"), AbsoluteUnixPath.get("/app/explodedJar/directory2/directory3/resource2.sql"), AbsoluteUnixPath.get("/app/explodedJar/directory4/resource3.txt"), AbsoluteUnixPath.get("/app/explodedJar/resource4.sql"));
    // Validate classes layer.
    assertThat(classesLayer.getName()).isEqualTo("classes");
    List<AbsoluteUnixPath> actualClassesPaths = classesLayer.getEntries().stream().map(FileEntry::getExtractionPath).collect(Collectors.toList());
    assertThat(actualClassesPaths).containsExactly(AbsoluteUnixPath.get("/app/explodedJar/class5.class"), AbsoluteUnixPath.get("/app/explodedJar/directory1/class1.class"), AbsoluteUnixPath.get("/app/explodedJar/directory1/class2.class"), AbsoluteUnixPath.get("/app/explodedJar/directory2/class4.class"), AbsoluteUnixPath.get("/app/explodedJar/directory2/directory3/class3.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 23 with AbsoluteUnixPath

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

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 24 with AbsoluteUnixPath

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

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 25 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project component-runtime by Talend.

the class ConnectorLoader method createConnectorLayer.

public ConnectorLayer createConnectorLayer(final AbsoluteUnixPath rootContainerPath, final Path workDir, final Path car) {
    String gav = null;
    final Map<String, Path> toCopy = new HashMap<>();
    try (final JarInputStream jarInputStream = new JarInputStream(Files.newInputStream(car))) {
        JarEntry entry;
        while ((entry = jarInputStream.getNextJarEntry()) != null) {
            if ("TALEND-INF/metadata.properties".equals(entry.getName())) {
                // don't close, it is the jar!
                gav = IO.loadProperties(new BufferedReader(new InputStreamReader(jarInputStream)).lines().collect(joining("\n"))).getProperty("component_coordinates");
                continue;
            }
            if (!entry.getName().startsWith("MAVEN-INF/repository/")) {
                continue;
            }
            final String relativeName = entry.getName().substring("MAVEN-INF/repository/".length());
            final Path local = workDir.resolve(relativeName);
            if (entry.isDirectory()) {
                if (!Files.exists(local)) {
                    Files.createDirectories(local);
                }
            } else {
                if (local.getParent() != null && !Files.exists(local.getParent())) {
                    Files.createDirectories(local.getParent());
                }
                Files.copy(jarInputStream, local);
                if (entry.getLastModifiedTime() != null) {
                    Files.setLastModifiedTime(local, entry.getLastModifiedTime());
                }
                toCopy.put(relativeName, local);
            }
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    return new ConnectorLayer(toCopy.entrySet().stream().collect(Collector.of(LayerConfiguration::builder, (builder, entry) -> {
        try {
            builder.addEntry(new LayerEntry(entry.getValue(), rootContainerPath.resolve(entry.getKey()), FilePermissions.DEFAULT_FILE_PERMISSIONS, Instant.ofEpochMilli(Files.getLastModifiedTime(entry.getValue()).toMillis())));
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }, (builder1, builder2) -> {
        builder2.build().getLayerEntries().forEach(builder1::addEntry);
        return builder1;
    })).build(), toCopy, requireNonNull(gav, "GAV was not found in '" + car + "', ensure it is a valid component archive."));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) LayerEntry(com.google.cloud.tools.jib.api.LayerEntry) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Files(java.nio.file.Files) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) IO(org.talend.sdk.component.remoteengine.customizer.lang.IO) IOException(java.io.IOException) HashMap(java.util.HashMap) Instant(java.time.Instant) InputStreamReader(java.io.InputStreamReader) Collectors.joining(java.util.stream.Collectors.joining) JarEntry(java.util.jar.JarEntry) LayerConfiguration(com.google.cloud.tools.jib.api.LayerConfiguration) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) JarInputStream(java.util.jar.JarInputStream) Data(lombok.Data) BufferedReader(java.io.BufferedReader) Collector(java.util.stream.Collector) Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) JarInputStream(java.util.jar.JarInputStream) LayerEntry(com.google.cloud.tools.jib.api.LayerEntry) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) BufferedReader(java.io.BufferedReader)

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