View Javadoc
1   package io.micronaut.build;
2   
3   import com.github.dockerjava.api.command.BuildImageCmd;
4   import com.google.cloud.tools.jib.plugins.common.PropertyNames;
5   import io.micronaut.build.services.ApplicationConfigurationService;
6   import io.micronaut.build.services.DockerService;
7   import io.micronaut.build.services.JibConfigurationService;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugins.annotations.Mojo;
10  import org.apache.maven.plugins.annotations.ResolutionScope;
11  import org.apache.maven.project.MavenProject;
12  import org.apache.maven.shared.utils.io.FileUtils;
13  
14  import javax.inject.Inject;
15  import java.io.File;
16  
17  /**
18   * <p>Allows using a provided Dockerfile.</p>
19   * <p><strong>WARNING</strong>: this goal is not intended to be executed directly. Instead, specify the packaging type
20   * using the <code>packaging</code> property, eg:</p>
21   *
22   * <pre>mvn package -Dpackaging=docker</pre>
23   *
24   * @author Álvaro Sánchez-Mariscal
25   * @since 1.1
26   */
27  @Mojo(name = DockerMojo.DOCKER_PACKAGING, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
28  public class DockerMojo extends AbstractDockerMojo {
29  
30      public static final String DOCKER_PACKAGING = "docker";
31  
32      @SuppressWarnings("CdiInjectionPointsInspection")
33      @Inject
34      public DockerMojo(MavenProject mavenProject, JibConfigurationService jibConfigurationService,
35                        ApplicationConfigurationService applicationConfigurationService, DockerService dockerService) {
36          super(mavenProject, jibConfigurationService, applicationConfigurationService, dockerService);
37      }
38  
39      @Override
40      public void execute() throws MojoExecutionException {
41          File dockerfile = new File(mavenProject.getBasedir(), "Dockerfile");
42          if (dockerfile.exists()) {
43              try {
44                  getLog().info("Using provided Dockerfile: " + dockerfile.getAbsolutePath());
45                  mavenProject.getProperties().put(PropertyNames.SKIP, "true");
46  
47                  copyDependencies();
48  
49                  String targetDir = mavenProject.getBuild().getDirectory();
50                  File targetDockerfile = new File(targetDir, dockerfile.getName());
51                  FileUtils.copyFile(dockerfile, targetDockerfile);
52  
53                  BuildImageCmd buildImageCmd = dockerService.buildImageCmd()
54                          .withDockerfile(targetDockerfile)
55                          .withTags(getTags())
56                          .withBaseDirectory(new File(targetDir));
57                  dockerService.buildImage(buildImageCmd);
58              } catch (Exception e) {
59                  throw new MojoExecutionException(e.getMessage(), e);
60              }
61          }
62      }
63  
64  }