Search in sources :

Example 1 with JibMavenPluginExtension

use of com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension 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 2 with JibMavenPluginExtension

use of com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension 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 3 with JibMavenPluginExtension

use of com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension in project jib by google.

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 4 with JibMavenPluginExtension

use of com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension in project jib by google.

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)

Aggregations

JibMavenPluginExtension (com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension)4 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)4 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)2 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)2 PluginExtensionLogger (com.google.cloud.tools.jib.plugins.common.PluginExtensionLogger)2 ExtensionConfiguration (com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtensionConfiguration)2 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2