|
16 | 16 |
|
17 | 17 | package com.google.cloud.tools.jib.maven; |
18 | 18 |
|
| 19 | +import com.google.cloud.tools.jib.builder.BuildConfiguration; |
| 20 | +import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; |
| 21 | +import com.google.cloud.tools.jib.frontend.BuildDockerStepsRunner; |
| 22 | +import com.google.cloud.tools.jib.frontend.BuildImageStepsExecutionException; |
| 23 | +import com.google.cloud.tools.jib.frontend.CacheDirectoryCreationException; |
| 24 | +import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; |
| 25 | +import com.google.cloud.tools.jib.image.ImageReference; |
| 26 | +import com.google.cloud.tools.jib.image.InvalidImageReferenceException; |
| 27 | +import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; |
| 28 | +import com.google.common.base.Preconditions; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.nio.file.Paths; |
| 31 | +import javax.annotation.Nullable; |
| 32 | +import org.apache.maven.execution.MavenSession; |
19 | 33 | import org.apache.maven.plugin.MojoExecutionException; |
20 | 34 | import org.apache.maven.plugin.MojoFailureException; |
21 | 35 | import org.apache.maven.plugins.annotations.Mojo; |
| 36 | +import org.apache.maven.plugins.annotations.Parameter; |
22 | 37 | import org.apache.maven.plugins.annotations.ResolutionScope; |
23 | 38 |
|
24 | 39 | /** Builds a container image and exports to the default Docker daemon. */ |
25 | 40 | @Mojo(name = "buildDocker", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM) |
26 | 41 | public class BuildDockerMojo extends JibPluginConfiguration { |
27 | 42 |
|
| 43 | + /** |
| 44 | + * Directory name for the cache. The directory will be relative to the build output directory. |
| 45 | + * |
| 46 | + * <p>TODO: Move to ProjectProperties. |
| 47 | + */ |
| 48 | + private static final String CACHE_DIRECTORY_NAME = "jib-cache"; |
| 49 | + |
| 50 | + private static final HelpfulSuggestions HELPFUL_SUGGESTIONS = |
| 51 | + HelpfulSuggestionsProvider.get("Build image failed"); |
| 52 | + |
| 53 | + @Nullable |
| 54 | + @Parameter(defaultValue = "${session}", readonly = true) |
| 55 | + private MavenSession session; |
| 56 | + |
| 57 | + /** TODO: Consolidate with BuildImageMojo. */ |
28 | 58 | @Override |
29 | 59 | public void execute() throws MojoExecutionException, MojoFailureException { |
30 | | - getLog().warn("Doing maven jib:buildDocker!"); |
| 60 | + ProjectProperties projectProperties = new ProjectProperties(getProject(), getLog()); |
| 61 | + String inferredMainClass = projectProperties.getMainClass(getMainClass()); |
| 62 | + |
| 63 | + SourceFilesConfiguration sourceFilesConfiguration = |
| 64 | + projectProperties.getSourceFilesConfiguration(); |
| 65 | + |
| 66 | + // Parses 'from' and 'to' into image reference. |
| 67 | + ImageReference baseImage = getBaseImageReference(); |
| 68 | + ImageReference targetImage = getTargetImageReference(); |
| 69 | + |
| 70 | + // Checks Maven settings for registry credentials. |
| 71 | + MavenSettingsServerCredentials mavenSettingsServerCredentials = |
| 72 | + new MavenSettingsServerCredentials(Preconditions.checkNotNull(session).getSettings()); |
| 73 | + RegistryCredentials knownBaseRegistryCredentials = |
| 74 | + mavenSettingsServerCredentials.retrieve(baseImage.getRegistry()); |
| 75 | + |
| 76 | + BuildConfiguration buildConfiguration = |
| 77 | + BuildConfiguration.builder(new MavenBuildLogger(getLog())) |
| 78 | + .setBaseImage(baseImage) |
| 79 | + .setBaseImageCredentialHelperName(getBaseImageCredentialHelperName()) |
| 80 | + .setKnownBaseRegistryCredentials(knownBaseRegistryCredentials) |
| 81 | + .setTargetImage(targetImage) |
| 82 | + .setMainClass(inferredMainClass) |
| 83 | + .setJvmFlags(getJvmFlags()) |
| 84 | + .setEnvironment(getEnvironment()) |
| 85 | + .build(); |
| 86 | + |
| 87 | + // Uses a directory in the Maven build cache as the Jib cache. |
| 88 | + Path cacheDirectory = Paths.get(getProject().getBuild().getDirectory(), CACHE_DIRECTORY_NAME); |
| 89 | + try { |
| 90 | + BuildDockerStepsRunner buildDockerStepsRunner = |
| 91 | + BuildDockerStepsRunner.newRunner( |
| 92 | + buildConfiguration, |
| 93 | + sourceFilesConfiguration, |
| 94 | + cacheDirectory, |
| 95 | + getUseOnlyProjectCache()); |
| 96 | + |
| 97 | + getLog().info(""); |
| 98 | + getLog().info("Building to docker daemon as " + targetImage); |
| 99 | + getLog().info(""); |
| 100 | + |
| 101 | + buildDockerStepsRunner.buildDocker(HELPFUL_SUGGESTIONS); |
| 102 | + |
| 103 | + getLog().info(""); |
| 104 | + getLog().info("Built image to Docker daemon as " + targetImage); |
| 105 | + getLog().info(""); |
| 106 | + |
| 107 | + } catch (CacheDirectoryCreationException | BuildImageStepsExecutionException ex) { |
| 108 | + throw new MojoExecutionException(ex.getMessage(), ex.getCause()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** @return the {@link ImageReference} parsed from {@link #from}. */ |
| 113 | + private ImageReference getBaseImageReference() throws MojoFailureException { |
| 114 | + try { |
| 115 | + ImageReference baseImage = ImageReference.parse(getBaseImage()); |
| 116 | + |
| 117 | + if (baseImage.usesDefaultTag()) { |
| 118 | + getLog() |
| 119 | + .warn( |
| 120 | + "Base image '" |
| 121 | + + baseImage |
| 122 | + + "' does not use a specific image digest - build may not be reproducible"); |
| 123 | + } |
| 124 | + |
| 125 | + return baseImage; |
| 126 | + |
| 127 | + } catch (InvalidImageReferenceException ex) { |
| 128 | + throw new MojoFailureException("Parameter 'from' is invalid", ex); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** @return the {@link ImageReference} parsed from {@link #to}. */ |
| 133 | + private ImageReference getTargetImageReference() throws MojoFailureException { |
| 134 | + try { |
| 135 | + return ImageReference.parse(getTargetImage()); |
| 136 | + |
| 137 | + } catch (InvalidImageReferenceException ex) { |
| 138 | + throw new MojoFailureException("Parameter 'to' is invalid", ex); |
| 139 | + } |
31 | 140 | } |
32 | 141 | } |
0 commit comments