Search in sources :

Example 11 with JibPluginExtensionException

use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib-extensions by GoogleContainerTools.

the class JibNativeImageExtension method extendContainerBuildPlan.

@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Void> config, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
    logger.log(LogLevel.LIFECYCLE, "Running Jib Native Image extension");
    MavenProject project = mavenData.getMavenProject();
    Optional<String> executableName = getExecutableName(project, properties);
    if (!executableName.isPresent()) {
        throw new JibPluginExtensionException(getClass(), "cannot auto-detect native-image executable name; consider setting 'imageName' property");
    }
    String outputDirectory = project.getBuild().getDirectory();
    Path localExecutable = Paths.get(outputDirectory, executableName.get());
    if (!Files.isRegularFile(localExecutable)) {
        throw new JibPluginExtensionException(getClass(), "Native-image executable does not exist or not a file: " + localExecutable + "\nDid you run the 'native-image:native-image' goal?");
    }
    // TODO: also check system and Maven properties (e.g., -Djib.container.appRoot).
    String appRoot = getPluginConfigValue(project, JIB_APP_ROOT).orElse("/app");
    AbsoluteUnixPath targetExecutable = AbsoluteUnixPath.get(appRoot).resolve(executableName.get());
    ContainerBuildPlan.Builder planBuilder = buildPlan.toBuilder();
    FileEntriesLayer nativeImageLayer = FileEntriesLayer.builder().setName("native image").addEntry(localExecutable, targetExecutable, FilePermissions.fromOctalString("755")).build();
    planBuilder.setLayers(Collections.singletonList(nativeImageLayer));
    // Preserve extra directories layers.
    String extraFilesLayerName = JavaContainerBuilder.LayerType.EXTRA_FILES.getName();
    buildPlan.getLayers().stream().filter(layer -> layer.getName().startsWith(extraFilesLayerName)).forEach(planBuilder::addLayer);
    // TODO: also check system and Maven properties (e.g., -Djib.container.entrypoint).
    if (!getPluginConfigValue(project, JIB_ENTRYPOINT).isPresent()) {
        planBuilder.setEntrypoint(Collections.singletonList(targetExecutable.toString()));
    }
    return planBuilder.build();
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ValueContainer(com.google.cloud.tools.jib.maven.extension.nativeimage.ConfigValueLocation.ValueContainer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Map(java.util.Map) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Files(java.nio.file.Files) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) Plugin(org.apache.maven.model.Plugin) Paths(java.nio.file.Paths) MavenData(com.google.cloud.tools.jib.maven.extension.MavenData) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) MavenProject(org.apache.maven.project.MavenProject) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Example 12 with JibPluginExtensionException

use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib-extensions by GoogleContainerTools.

the class JibOwnershipExtensionTest method testExtendContainerBuildPlan_noGlobGiven.

@Test
public void testExtendContainerBuildPlan_noGlobGiven() {
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
    Configuration.Rule rule = mockRule("", "doesn't matter");
    when(config.getRules()).thenReturn(Arrays.asList(rule));
    try {
        new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
        fail();
    } catch (JibPluginExtensionException ex) {
        assertEquals(JibOwnershipExtension.class, ex.getExtensionClass());
        assertEquals("glob pattern not given in ownership configuration", ex.getMessage());
    }
}
Also used : JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 13 with JibPluginExtensionException

use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException 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 14 with JibPluginExtensionException

use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib-extensions by GoogleContainerTools.

the class JibNativeImageExtensionTest method testNoExecutableNameDetected.

@Test
public void testNoExecutableNameDetected() {
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
    try {
        new JibNativeImageExtension().extendContainerBuildPlan(buildPlan, Collections.emptyMap(), Optional.empty(), mavenData, logger);
        fail();
    } catch (JibPluginExtensionException ex) {
        assertEquals("cannot auto-detect native-image executable name; consider setting 'imageName' property", ex.getMessage());
    }
}
Also used : JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 15 with JibPluginExtensionException

use of com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException in project jib-extensions by GoogleContainerTools.

the class JibOwnershipExtensionTest method testExtendContainerBuildPlan_noGlobGiven.

@Test
public void testExtendContainerBuildPlan_noGlobGiven() {
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
    Configuration.Rule rule = mockRule("", "doesn't matter");
    when(config.getRules()).thenReturn(Arrays.asList(rule));
    try {
        new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
        fail();
    } catch (JibPluginExtensionException ex) {
        assertEquals(JibOwnershipExtension.class, ex.getExtensionClass());
        assertEquals("glob pattern not given in ownership configuration", ex.getMessage());
    }
}
Also used : JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Aggregations

JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)45 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)22 Optional (java.util.Optional)20 Test (org.junit.Test)18 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)16 IOException (java.io.IOException)14 CacheDirectoryCreationException (com.google.cloud.tools.jib.api.CacheDirectoryCreationException)12 TempDirectoryProvider (com.google.cloud.tools.jib.filesystem.TempDirectoryProvider)12 BuildStepsExecutionException (com.google.cloud.tools.jib.plugins.common.BuildStepsExecutionException)12 ExtraDirectoryNotFoundException (com.google.cloud.tools.jib.plugins.common.ExtraDirectoryNotFoundException)12 IncompatibleBaseImageJavaVersionException (com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException)12 InvalidAppRootException (com.google.cloud.tools.jib.plugins.common.InvalidAppRootException)12 InvalidContainerVolumeException (com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException)12 InvalidContainerizingModeException (com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException)12 InvalidCreationTimeException (com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException)12 InvalidFilesModificationTimeException (com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException)12 InvalidPlatformException (com.google.cloud.tools.jib.plugins.common.InvalidPlatformException)12 InvalidWorkingDirectoryException (com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException)12 MainClassInferenceException (com.google.cloud.tools.jib.plugins.common.MainClassInferenceException)12 GlobalConfig (com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig)12