All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.wouterdanes.docker.maven.BuildImageMojo Maven / Gradle / Ivy

package net.wouterdanes.docker.maven;

import java.util.List;

import com.google.common.base.Optional;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.InstantiationStrategy;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import net.wouterdanes.docker.provider.model.ImageBuildConfiguration;
import net.wouterdanes.docker.remoteapi.exception.DockerException;

/**
 * This class is responsible for building docker images specified in the POM file. It runs by default during the
 * package phase of a maven project.
 */
@Mojo(name = "build-images", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true,
        instantiationStrategy = InstantiationStrategy.PER_LOOKUP)
public class BuildImageMojo extends AbstractDockerMojo {

    @Parameter(required = true)
    private List images;

    public void setImages(final List images) {
        this.images = images;
    }

    @Override
    protected void doExecute() throws MojoExecutionException, MojoFailureException {
        validateAllImages();

        for (ImageBuildConfiguration image : images) {
            try {
                logImageConfig(image);
                String imageId = getDockerProvider().buildImage(image);
                getLog().info(String.format("Image '%s' has Id '%s'", image.getId(), imageId));
                registerBuiltImage(image.getId(), imageId, image.isKeep());
            } catch (DockerException e) {
                getLog().error(String.format("Cannot build image '%s'", image.getId()), e);
                Optional apiResponse = e.getApiResponse();
                if (apiResponse.isPresent()) {
                    getLog().info(String.format("Api response:\n%s", apiResponse.get()));
                }
            }
        }
    }

    private void logImageConfig(final ImageBuildConfiguration image) {
        StringBuilder builder = new StringBuilder(String.format("Building image '%s'", image.getId()));
        if (image.getNameAndTag() != null) {
            builder.append(String.format(", with name and tag '%s'", image.getNameAndTag()));
        }
        builder.append("..");
        getLog().info(builder.toString());
    }

    private void validateAllImages() throws MojoExecutionException {
        for (ImageBuildConfiguration image : images) {
            if (!image.isValid()) {
                throw new MojoExecutionException(String.format("Image '%s' not valid, did you specify a Dockerfile?",
                        image.getId()));
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy