Search in sources :

Example 46 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.

the class JibQuarkusExtension method extendContainerBuildPlan.

@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Void> config, GradleData gradleData, ExtensionLogger logger) throws JibPluginExtensionException {
    try {
        logger.log(LogLevel.LIFECYCLE, "Running Quarkus Jib extension");
        readJibConfigurations(gradleData.getProject());
        Project project = gradleData.getProject();
        Path buildDir = project.getBuildDir().toPath();
        Jar jarTask = (Jar) project.getTasks().findByName("jar");
        String jarName = jarTask.getArchiveFile().get().getAsFile().getName();
        Path jar = buildDir.resolve(jarName.replaceAll("\\.jar$", "-runner.jar"));
        if (!Files.isRegularFile(jar)) {
            throw new JibPluginExtensionException(getClass(), jar + " doesn't exist; did you run the Qaurkus Gradle plugin ('quarkusBuild' task)?");
        }
        ContainerBuildPlan.Builder planBuilder = buildPlan.toBuilder();
        planBuilder.setLayers(Collections.emptyList());
        // dependency layers
        addDependencyLayers(project, planBuilder, buildDir.resolve("lib"));
        // Quarkus runner JAR layer
        AbsoluteUnixPath appRootJar = appRoot.resolve("app.jar");
        FileEntriesLayer jarLayer = FileEntriesLayer.builder().setName("quarkus jar").addEntry(jar, appRootJar).build();
        planBuilder.addLayer(jarLayer);
        // Preserve extra directories layers.
        String extraFilesLayerName = JavaContainerBuilder.LayerType.EXTRA_FILES.getName();
        buildPlan.getLayers().stream().filter(layer -> layer.getName().startsWith(extraFilesLayerName)).forEach(planBuilder::addLayer);
        // set entrypoint
        List<String> entrypoint = new ArrayList<>();
        entrypoint.add("java");
        entrypoint.addAll(jvmFlags);
        entrypoint.add("-jar");
        entrypoint.add(appRootJar.toString());
        planBuilder.setEntrypoint(entrypoint);
        return planBuilder.build();
    } catch (IOException ex) {
        throw new JibPluginExtensionException(getClass(), Verify.verifyNotNull(ex.getMessage()), ex);
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) JavaPlugin(org.gradle.api.plugins.JavaPlugin) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Map(java.util.Map) Path(java.nio.file.Path) GradleData(com.google.cloud.tools.jib.gradle.extension.GradleData) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Verify(com.google.common.base.Verify) Files(java.nio.file.Files) Project(org.gradle.api.Project) Predicate(java.util.function.Predicate) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Jar(org.gradle.jvm.tasks.Jar) JibExtension(com.google.cloud.tools.jib.gradle.JibExtension) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) JibGradlePluginExtension(com.google.cloud.tools.jib.gradle.extension.JibGradlePluginExtension) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Collections(java.util.Collections) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Project(org.gradle.api.Project) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Jar(org.gradle.jvm.tasks.Jar) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Example 47 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.

the class JibQuarkusExtensionTest method testExtendContainerBuildPlan_layers.

@Test
public void testExtendContainerBuildPlan_layers() throws JibPluginExtensionException {
    FileEntriesLayer originalLayer = buildLayer("to be reset", Arrays.asList("/ignored"));
    FileEntriesLayer extraLayer1 = buildLayer("extra files", Arrays.asList("/extra/files/1"));
    FileEntriesLayer extraLayer2 = buildLayer("extra files", Arrays.asList("/extra/files/2"));
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().setLayers(Arrays.asList(originalLayer, extraLayer1, extraLayer2)).build();
    ContainerBuildPlan newPlan = new JibQuarkusExtension().extendContainerBuildPlan(buildPlan, null, Optional.empty(), gradleData, logger);
    assertEquals(6, newPlan.getLayers().size());
    FileEntriesLayer layer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
    FileEntriesLayer layer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
    FileEntriesLayer layer3 = (FileEntriesLayer) newPlan.getLayers().get(2);
    FileEntriesLayer layer4 = (FileEntriesLayer) newPlan.getLayers().get(3);
    FileEntriesLayer layer5 = (FileEntriesLayer) newPlan.getLayers().get(4);
    FileEntriesLayer layer6 = (FileEntriesLayer) newPlan.getLayers().get(5);
    assertEquals("dependencies", layer1.getName());
    assertEquals("snapshot dependencies", layer2.getName());
    assertEquals("project dependencies", layer3.getName());
    assertEquals("quarkus jar", layer4.getName());
    assertEquals("extra files", layer5.getName());
    assertEquals("extra files", layer6.getName());
    assertEquals(Arrays.asList("/new/appRoot/lib/com.example.third-party-artifact.jar"), layerToExtractionPaths(layer1));
    assertEquals(Arrays.asList("/new/appRoot/lib/com.example.third-party-SNAPSHOT-artifact.jar"), layerToExtractionPaths(layer2));
    assertEquals(Arrays.asList("/new/appRoot/lib/com.example.sub-module-artifact.jar"), layerToExtractionPaths(layer3));
    assertEquals(Arrays.asList("/new/appRoot/app.jar"), layerToExtractionPaths(layer4));
    assertEquals(extraLayer1.getEntries(), layer5.getEntries());
    assertEquals(extraLayer2.getEntries(), layer6.getEntries());
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 48 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.

the class JibOwnershipExtensionTest method testExtendContainerBuildPlan.

@Test
public void testExtendContainerBuildPlan() throws JibPluginExtensionException {
    FileEntriesLayer layer1 = FileEntriesLayer.builder().addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/another")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/another")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/another")).build();
    FileEntriesLayer layer2 = FileEntriesLayer.builder().addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/bar")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/bar")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/bar")).build();
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer1).addLayer(layer2).build();
    Configuration.Rule rule1 = mockRule("/target/**", "10:20");
    Configuration.Rule rule2 = mockRule("**/bar", "999:777");
    when(config.getRules()).thenReturn(Arrays.asList(rule1, rule2));
    ContainerBuildPlan newPlan = new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
    FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
    FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
    assertEquals(Arrays.asList(AbsoluteUnixPath.get("/target/file"), AbsoluteUnixPath.get("/target/another"), AbsoluteUnixPath.get("/target/sub/dir/file"), AbsoluteUnixPath.get("/target/sub/dir/another"), AbsoluteUnixPath.get("/untouched/file"), AbsoluteUnixPath.get("/untouched/another")), mapLayerEntries(newLayer1, FileEntry::getExtractionPath));
    assertEquals(Arrays.asList("10:20", "10:20", "10:20", "10:20", "", ""), mapLayerEntries(newLayer1, FileEntry::getOwnership));
    assertEquals(Arrays.asList(AbsoluteUnixPath.get("/target/foo"), AbsoluteUnixPath.get("/target/bar"), AbsoluteUnixPath.get("/target/sub/dir/foo"), AbsoluteUnixPath.get("/target/sub/dir/bar"), AbsoluteUnixPath.get("/untouched/foo"), AbsoluteUnixPath.get("/untouched/bar")), mapLayerEntries(newLayer2, FileEntry::getExtractionPath));
    assertEquals(Arrays.asList("10:20", "999:777", "10:20", "999:777", "", "999:777"), mapLayerEntries(newLayer2, FileEntry::getOwnership));
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 49 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.

the class JibSpringBootExtensionTest method testExtendContainerBuildPlan_noFiltering.

@Test
public void testExtendContainerBuildPlan_noFiltering() throws JibPluginExtensionException {
    // set up <excludeDevtools>false (no filtering required)
    Xpp3Dom configuration = new Xpp3Dom("configuration");
    Xpp3Dom excludeDevtools = new Xpp3Dom("excludeDevtools");
    excludeDevtools.setValue("false");
    configuration.addChild(excludeDevtools);
    when(project.getPlugin("org.springframework.boot:spring-boot-maven-plugin")).thenReturn(bootPlugin);
    when(bootPlugin.getConfiguration()).thenReturn(configuration);
    FileEntriesLayer layer1 = buildLayer("dependencies", Paths.get("spring-boot-devtools-1.2.3.jar"), Paths.get("archive").resolve("bar.zip"));
    FileEntriesLayer layer2 = buildLayer("NOT dependencies", Paths.get("spring-boot-devtools-1.2.3.jar"));
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer1).addLayer(layer2).build();
    ContainerBuildPlan newPlan = new JibSpringBootExtension().extendContainerBuildPlan(buildPlan, null, Optional.empty(), mavenData, logger);
    assertSame(buildPlan, newPlan);
    verify(logger).log(LogLevel.INFO, "Keeping spring-boot-devtools (if any)");
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 50 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.

the class JibSpringBootExtensionTest method testFilterOutDevtools.

@Test
public void testFilterOutDevtools() {
    FileEntriesLayer layer = buildLayer("dependencies", Paths.get("static").resolve("foo.txt"), Paths.get("lib").resolve("spring-boot-devtools-1.2.3.jar"), Paths.get("archive").resolve("bar.zip"));
    FileEntriesLayer filtered = (FileEntriesLayer) JibSpringBootExtension.filterOutDevtools(layer);
    assertEquals(Arrays.asList("/dest/foo.txt", "/dest/bar.zip"), layerToExtractionPaths(filtered));
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Test(org.junit.Test)

Aggregations

FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)154 Test (org.junit.Test)111 Path (java.nio.file.Path)92 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)85 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)55 List (java.util.List)29 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)24 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)23 Map (java.util.Map)23 Files (java.nio.file.Files)21 Predicate (java.util.function.Predicate)20 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)16 Collectors (java.util.stream.Collectors)16 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)13 Paths (java.nio.file.Paths)13 Optional (java.util.Optional)13 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)12 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)12