protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, Map<String, String> buildArgs)
            throws DockerAccessException, MojoExecutionException {

        String imageName = imageConfig.getName();
        ImageName.validate(imageName);

        BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();

        String oldImageId = null;

        CleanupMode cleanupMode = buildConfig.cleanupMode();
        if (cleanupMode.isRemove()) {
            oldImageId = queryService.getImageId(imageName);
        }

        if (buildConfig.getDockerArchive() != null) {
            File tarArchive = buildConfig.getAbsoluteDockerTarPath(params);
            String archiveImageName = getArchiveImageName(buildConfig, tarArchive);

            long time = System.currentTimeMillis();

            docker.loadImage(imageName, tarArchive);
            log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));

            if(archiveImageName != null && !archiveImageName.equals(imageName)) {
                docker.tag(archiveImageName, imageName, true);
            }

            return;
        }

        long time = System.currentTimeMillis();

        File dockerArchive = archiveService.createArchive(imageName, buildConfig, params, log);
        log.info("%s: Created %s in %s", imageConfig.getDescription(), dockerArchive.getName(), EnvUtil.formatDurationTill(time));

        Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);

        // auto is now supported by docker, consider switching?
        BuildOptions opts =
                new BuildOptions(buildConfig.getBuildOptions())
                        .dockerfile(getDockerfileName(buildConfig))
                        .forceRemove(cleanupMode.isRemove())
                        .noCache(noCache)
                        .cacheFrom(buildConfig.getCacheFrom())
                        .buildArgs(mergedBuildMap);
        String newImageId = doBuildImage(imageName, dockerArchive, opts);
        log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);

        if (oldImageId != null && !oldImageId.equals(newImageId)) {
            try {
                docker.removeImage(oldImageId, true);
                log.info("%s: Removed old image %s", imageConfig.getDescription(), oldImageId);
            } catch (DockerAccessException exp) {
                if (cleanupMode == CleanupMode.TRY_TO_REMOVE) {
                    log.warn("%s: %s (old image)%s", imageConfig.getDescription(), exp.getMessage(),
                            (exp.getCause() != null ? " [" + exp.getCause().getMessage() + "]" : ""));
                } else {
                    throw exp;
                }
            }
        }
    }
protected void buildImage ( ImageConfiguration imageConfig , MojoParameters params , boolean noCache , Map < String , String > buildArgs ) throws DockerAccessException , MojoExecutionException { String imageName = imageConfig . getName ( ) ; ImageName . validate ( imageName ) ; BuildImageConfiguration buildConfig = imageConfig . getBuildConfiguration ( ) ; String oldImageId = null ; CleanupMode cleanupMode = buildConfig . cleanupMode ( ) ; if ( cleanupMode . isRemove ( ) ) { oldImageId = queryService . getImageId ( imageName ) ; } if ( buildConfig . getDockerArchive ( ) != null ) { File tarArchive = buildConfig . getAbsoluteDockerTarPath ( params ) ; String archiveImageName = getArchiveImageName ( buildConfig , tarArchive ) ; long time = System . currentTimeMillis ( ) ; docker . loadImage ( imageName , tarArchive ) ; log . info ( "%s: Loaded tarball in %s" , buildConfig . getDockerArchive ( ) , EnvUtil . formatDurationTill ( time ) ) ; if ( archiveImageName != null && ! archiveImageName . equals ( imageName ) ) { docker . tag ( archiveImageName , imageName , true ) ; } return ; } long time = System . currentTimeMillis ( ) ; File dockerArchive = archiveService . createArchive ( imageName , buildConfig , params , log ) ; log . info ( "%s: Created %s in %s" , imageConfig . getDescription ( ) , dockerArchive . getName ( ) , EnvUtil . formatDurationTill ( time ) ) ; Map < String , String > mergedBuildMap = prepareBuildArgs ( buildArgs , buildConfig ) ; BuildOptions opts = new BuildOptions ( buildConfig . getBuildOptions ( ) ) . dockerfile ( getDockerfileName ( buildConfig ) ) . forceRemove ( cleanupMode . isRemove ( ) ) . noCache ( noCache ) . cacheFrom ( buildConfig . getCacheFrom ( ) ) . buildArgs ( mergedBuildMap ) ; String newImageId = doBuildImage ( imageName , dockerArchive , opts ) ; log . info ( "%s: Built image %s" , imageConfig . getDescription ( ) , newImageId ) ; if ( oldImageId != null && ! oldImageId . equals ( newImageId ) ) { try { docker . removeImage ( oldImageId , true ) ; log . info ( "%s: Removed old image %s" , imageConfig . getDescription ( ) , oldImageId ) ; } catch ( DockerAccessException exp ) { if ( cleanupMode == CleanupMode . TRY_TO_REMOVE ) { log . warn ( "%s: %s (old image)%s" , imageConfig . getDescription ( ) , exp . getMessage ( ) , ( exp . getCause ( ) != null ? " [" + exp . getCause ( ) . getMessage ( ) + "]" : "" ) ) ; } else { throw exp ; } } } }
Build an image

@param imageConfig the image configuration
@param params mojo params for the project
@param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
@param buildArgs
@throws DockerAccessException
@throws MojoExecutionException