Search in sources :

Example 1 with JibPluginExtensionException

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

the class BuildDockerMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    checkJibVersion();
    if (MojoCommon.shouldSkipJibExecution(this)) {
        return;
    }
    Path dockerExecutable = getDockerClientExecutable();
    boolean isDockerInstalled = dockerExecutable == null ? DockerClient.isDefaultDockerInstalled() : DockerClient.isDockerInstalled(dockerExecutable);
    if (!isDockerInstalled) {
        throw new MojoExecutionException(HelpfulSuggestions.forDockerNotInstalled(HELPFUL_SUGGESTIONS_PREFIX));
    }
    MavenSettingsProxyProvider.activateHttpAndHttpsProxies(getSession().getSettings(), getSettingsDecrypter());
    TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();
    MavenProjectProperties projectProperties = MavenProjectProperties.getForProject(Preconditions.checkNotNull(descriptor), getProject(), getSession(), getLog(), tempDirectoryProvider, getInjectedPluginExtensions());
    Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
    try {
        GlobalConfig globalConfig = GlobalConfig.readConfig();
        updateCheckFuture = MojoCommon.newUpdateChecker(projectProperties, globalConfig, getLog());
        PluginConfigurationProcessor.createJibBuildRunnerForDockerDaemonImage(new MavenRawConfiguration(this), new MavenSettingsServerCredentials(getSession().getSettings(), getSettingsDecrypter()), projectProperties, globalConfig, new MavenHelpfulSuggestions(HELPFUL_SUGGESTIONS_PREFIX)).runBuild();
    } catch (InvalidAppRootException ex) {
        throw new MojoExecutionException("<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
    } catch (InvalidContainerizingModeException ex) {
        throw new MojoExecutionException("invalid value for <containerizingMode>: " + ex.getInvalidContainerizingMode(), ex);
    } catch (InvalidWorkingDirectoryException ex) {
        throw new MojoExecutionException("<container><workingDirectory> is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
    } catch (InvalidPlatformException ex) {
        throw new MojoExecutionException("<from><platforms> contains a platform configuration that is missing required values or has invalid values: " + ex.getMessage() + ": " + ex.getInvalidPlatform(), ex);
    } catch (InvalidContainerVolumeException ex) {
        throw new MojoExecutionException("<container><volumes> is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
    } catch (InvalidFilesModificationTimeException ex) {
        throw new MojoExecutionException("<container><filesModificationTime> should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or special keyword \"EPOCH_PLUS_SECOND\": " + ex.getInvalidFilesModificationTime(), ex);
    } catch (InvalidCreationTimeException ex) {
        throw new MojoExecutionException("<container><creationTime> should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH\", " + "\"USE_CURRENT_TIMESTAMP\"): " + ex.getInvalidCreationTime(), ex);
    } catch (JibPluginExtensionException ex) {
        String extensionName = ex.getExtensionClass().getName();
        throw new MojoExecutionException("error running extension '" + extensionName + "': " + ex.getMessage(), ex);
    } catch (IncompatibleBaseImageJavaVersionException ex) {
        throw new MojoExecutionException(HelpfulSuggestions.forIncompatibleBaseImageJavaVersionForMaven(ex.getBaseImageMajorJavaVersion(), ex.getProjectMajorJavaVersion()), ex);
    } catch (InvalidImageReferenceException ex) {
        throw new MojoExecutionException(HelpfulSuggestions.forInvalidImageReference(ex.getInvalidReference()), ex);
    } catch (IOException | CacheDirectoryCreationException | MainClassInferenceException | InvalidGlobalConfigException ex) {
        throw new MojoExecutionException(ex.getMessage(), ex);
    } catch (BuildStepsExecutionException ex) {
        throw new MojoExecutionException(ex.getMessage(), ex.getCause());
    } catch (ExtraDirectoryNotFoundException ex) {
        throw new MojoExecutionException("<extraDirectories><paths> contain \"from\" directory that doesn't exist locally: " + ex.getPath(), ex);
    } finally {
        tempDirectoryProvider.close();
        MojoCommon.finishUpdateChecker(projectProperties, updateCheckFuture);
        projectProperties.waitForLoggingThread();
        getLog().info("");
    }
}
Also used : InvalidContainerVolumeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) ExtraDirectoryNotFoundException(com.google.cloud.tools.jib.plugins.common.ExtraDirectoryNotFoundException) InvalidFilesModificationTimeException(com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException) IncompatibleBaseImageJavaVersionException(com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException) InvalidWorkingDirectoryException(com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider) InvalidContainerizingModeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException) InvalidPlatformException(com.google.cloud.tools.jib.plugins.common.InvalidPlatformException) Path(java.nio.file.Path) InvalidAppRootException(com.google.cloud.tools.jib.plugins.common.InvalidAppRootException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Optional(java.util.Optional) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) BuildStepsExecutionException(com.google.cloud.tools.jib.plugins.common.BuildStepsExecutionException) IOException(java.io.IOException) InvalidGlobalConfigException(com.google.cloud.tools.jib.plugins.common.globalconfig.InvalidGlobalConfigException) CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) InvalidCreationTimeException(com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException) MainClassInferenceException(com.google.cloud.tools.jib.plugins.common.MainClassInferenceException)

Example 2 with JibPluginExtensionException

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

the class BuildTarMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    checkJibVersion();
    if (MojoCommon.shouldSkipJibExecution(this)) {
        return;
    }
    MavenSettingsProxyProvider.activateHttpAndHttpsProxies(getSession().getSettings(), getSettingsDecrypter());
    TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();
    MavenProjectProperties projectProperties = MavenProjectProperties.getForProject(Preconditions.checkNotNull(descriptor), getProject(), getSession(), getLog(), tempDirectoryProvider, getInjectedPluginExtensions());
    Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
    try {
        GlobalConfig globalConfig = GlobalConfig.readConfig();
        updateCheckFuture = MojoCommon.newUpdateChecker(projectProperties, globalConfig, getLog());
        PluginConfigurationProcessor.createJibBuildRunnerForTarImage(new MavenRawConfiguration(this), new MavenSettingsServerCredentials(getSession().getSettings(), getSettingsDecrypter()), projectProperties, globalConfig, new MavenHelpfulSuggestions(HELPFUL_SUGGESTIONS_PREFIX)).runBuild();
    } catch (InvalidAppRootException ex) {
        throw new MojoExecutionException("<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
    } catch (InvalidContainerizingModeException ex) {
        throw new MojoExecutionException("invalid value for <containerizingMode>: " + ex.getInvalidContainerizingMode(), ex);
    } catch (InvalidWorkingDirectoryException ex) {
        throw new MojoExecutionException("<container><workingDirectory> is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
    } catch (InvalidPlatformException ex) {
        throw new MojoExecutionException("<from><platforms> contains a platform configuration that is missing required values or has invalid values: " + ex.getMessage() + ": " + ex.getInvalidPlatform(), ex);
    } catch (InvalidContainerVolumeException ex) {
        throw new MojoExecutionException("<container><volumes> is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
    } catch (InvalidFilesModificationTimeException ex) {
        throw new MojoExecutionException("<container><filesModificationTime> should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or special keyword \"EPOCH_PLUS_SECOND\": " + ex.getInvalidFilesModificationTime(), ex);
    } catch (InvalidCreationTimeException ex) {
        throw new MojoExecutionException("<container><creationTime> should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH\", " + "\"USE_CURRENT_TIMESTAMP\"): " + ex.getInvalidCreationTime(), ex);
    } catch (JibPluginExtensionException ex) {
        String extensionName = ex.getExtensionClass().getName();
        throw new MojoExecutionException("error running extension '" + extensionName + "': " + ex.getMessage(), ex);
    } catch (IncompatibleBaseImageJavaVersionException ex) {
        throw new MojoExecutionException(HelpfulSuggestions.forIncompatibleBaseImageJavaVersionForMaven(ex.getBaseImageMajorJavaVersion(), ex.getProjectMajorJavaVersion()), ex);
    } catch (InvalidImageReferenceException ex) {
        throw new MojoExecutionException(HelpfulSuggestions.forInvalidImageReference(ex.getInvalidReference()), ex);
    } catch (IOException | CacheDirectoryCreationException | MainClassInferenceException | InvalidGlobalConfigException ex) {
        throw new MojoExecutionException(ex.getMessage(), ex);
    } catch (BuildStepsExecutionException ex) {
        throw new MojoExecutionException(ex.getMessage(), ex.getCause());
    } catch (ExtraDirectoryNotFoundException ex) {
        throw new MojoExecutionException("<extraDirectories><paths> contain \"from\" directory that doesn't exist locally: " + ex.getPath(), ex);
    } finally {
        tempDirectoryProvider.close();
        MojoCommon.finishUpdateChecker(projectProperties, updateCheckFuture);
        projectProperties.waitForLoggingThread();
        getLog().info("");
    }
}
Also used : InvalidContainerVolumeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) ExtraDirectoryNotFoundException(com.google.cloud.tools.jib.plugins.common.ExtraDirectoryNotFoundException) InvalidFilesModificationTimeException(com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException) IncompatibleBaseImageJavaVersionException(com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException) InvalidWorkingDirectoryException(com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider) InvalidContainerizingModeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException) InvalidPlatformException(com.google.cloud.tools.jib.plugins.common.InvalidPlatformException) InvalidAppRootException(com.google.cloud.tools.jib.plugins.common.InvalidAppRootException) Optional(java.util.Optional) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) BuildStepsExecutionException(com.google.cloud.tools.jib.plugins.common.BuildStepsExecutionException) IOException(java.io.IOException) InvalidGlobalConfigException(com.google.cloud.tools.jib.plugins.common.globalconfig.InvalidGlobalConfigException) CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) InvalidCreationTimeException(com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException) MainClassInferenceException(com.google.cloud.tools.jib.plugins.common.MainClassInferenceException)

Example 3 with JibPluginExtensionException

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

the class MavenProjectProperties method runPluginExtensions.

@Override
public JibContainerBuilder runPluginExtensions(List<? extends ExtensionConfiguration> extensionConfigs, JibContainerBuilder jibContainerBuilder) throws JibPluginExtensionException {
    if (extensionConfigs.isEmpty()) {
        log(LogEvent.debug("No Jib plugin extensions configured to load"));
        return jibContainerBuilder;
    }
    // Add the injected extensions at first to prefer them over the ones from JDK service
    // loader.
    // Extensions might support both approaches (injection and JDK service loader) at the same
    // time for compatibility reasons.
    List<JibMavenPluginExtension<?>> loadedExtensions = new ArrayList<>(injectedExtensions);
    loadedExtensions.addAll(extensionLoader.get());
    JibMavenPluginExtension<?> extension = null;
    ContainerBuildPlan buildPlan = jibContainerBuilder.toContainerBuildPlan();
    try {
        for (ExtensionConfiguration config : extensionConfigs) {
            extension = findConfiguredExtension(loadedExtensions, config);
            log(LogEvent.lifecycle("Running extension: " + config.getExtensionClass()));
            buildPlan = runPluginExtension(extension.getExtraConfigType(), extension, config, buildPlan);
            // to validate image reference
            ImageReference.parse(buildPlan.getBaseImage());
        }
        return jibContainerBuilder.applyContainerBuildPlan(buildPlan);
    } catch (InvalidImageReferenceException ex) {
        throw new JibPluginExtensionException(Verify.verifyNotNull(extension).getClass(), "invalid base image reference: " + buildPlan.getBaseImage(), ex);
    }
}
Also used : JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) ExtensionConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtensionConfiguration) ArrayList(java.util.ArrayList) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Example 4 with JibPluginExtensionException

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

the class MavenProjectProperties method runPluginExtension.

// Unchecked casting: "getExtraConfiguration()" (Optional<Object>) to Object<T> and "extension"
// (JibMavenPluginExtension<?>) to JibMavenPluginExtension<T> where T is the extension-defined
// config type (as requested by "JibMavenPluginExtension.getExtraConfigType()").
@SuppressWarnings({ "unchecked" })
private <T> ContainerBuildPlan runPluginExtension(Optional<Class<T>> extraConfigType, JibMavenPluginExtension<?> extension, ExtensionConfiguration config, ContainerBuildPlan buildPlan) throws JibPluginExtensionException {
    Optional<T> extraConfig = Optional.empty();
    Optional<Object> configs = config.getExtraConfiguration();
    if (configs.isPresent()) {
        if (!extraConfigType.isPresent()) {
            throw new IllegalArgumentException("extension " + extension.getClass().getSimpleName() + " does not expect extension-specific configuration; remove the inapplicable " + "<pluginExtension><configuration> from pom.xml");
        } else if (!extraConfigType.get().isInstance(configs.get())) {
            throw new JibPluginExtensionException(extension.getClass(), "extension-specific <configuration> for " + extension.getClass().getSimpleName() + " is not of type " + extraConfigType.get().getName() + " but " + configs.get().getClass().getName() + "; specify the correct type with <pluginExtension><configuration " + "implementation=\"" + extraConfigType.get().getName() + "\">");
        } else {
            // configs is of type Optional, so this cast always succeeds
            // without the isInstance() check above. (Note generic <T> is erased at runtime.)
            extraConfig = (Optional<T>) configs;
        }
    }
    try {
        return ((JibMavenPluginExtension<T>) extension).extendContainerBuildPlan(buildPlan, config.getProperties(), extraConfig, new MavenExtensionData(project, session), new PluginExtensionLogger(this::log));
    } catch (RuntimeException ex) {
        throw new JibPluginExtensionException(extension.getClass(), "extension crashed: " + ex.getMessage(), ex);
    }
}
Also used : PluginExtensionLogger(com.google.cloud.tools.jib.plugins.common.PluginExtensionLogger) Optional(java.util.Optional) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension)

Example 5 with JibPluginExtensionException

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

the class GradleProjectProperties method runPluginExtension.

// Unchecked casting: "getExtraConfiguration().get()" (Object) to Action<T> and "extension"
// (JibGradlePluginExtension<?>) to JibGradlePluginExtension<T> where T is the extension-defined
// config type (as requested by "JibGradlePluginExtension.getExtraConfigType()").
@SuppressWarnings({ "unchecked" })
private <T> ContainerBuildPlan runPluginExtension(Optional<Class<T>> extraConfigType, JibGradlePluginExtension<?> extension, ExtensionConfiguration config, ContainerBuildPlan buildPlan) throws JibPluginExtensionException {
    T extraConfig = null;
    Optional<Object> configs = config.getExtraConfiguration();
    if (configs.isPresent()) {
        if (!extraConfigType.isPresent()) {
            throw new IllegalArgumentException("extension " + extension.getClass().getSimpleName() + " does not expect extension-specific configuration; remove the inapplicable " + "'pluginExtension.configuration' from Gradle build script");
        } else {
            // configs.get() is of type Action, so this cast always succeeds.
            // (Note generic <T> is erased at runtime.)
            Action<T> action = (Action<T>) configs.get();
            extraConfig = project.getObjects().newInstance(extraConfigType.get(), project);
            action.execute(extraConfig);
        }
    }
    try {
        return ((JibGradlePluginExtension<T>) extension).extendContainerBuildPlan(buildPlan, config.getProperties(), Optional.ofNullable(extraConfig), () -> project, new PluginExtensionLogger(this::log));
    } catch (RuntimeException ex) {
        throw new JibPluginExtensionException(extension.getClass(), "extension crashed: " + ex.getMessage(), ex);
    }
}
Also used : PluginExtensionLogger(com.google.cloud.tools.jib.plugins.common.PluginExtensionLogger) Action(org.gradle.api.Action) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) JibGradlePluginExtension(com.google.cloud.tools.jib.gradle.extension.JibGradlePluginExtension)

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