View Javadoc
1   /*
2    * Copyright 2017-2022 original authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package io.micronaut.build;
17  
18  import com.github.dockerjava.api.command.BuildImageCmd;
19  import com.google.cloud.tools.jib.plugins.common.PropertyNames;
20  import io.micronaut.build.jib.JibMicronautExtension;
21  import io.micronaut.build.services.ApplicationConfigurationService;
22  import io.micronaut.build.services.DockerService;
23  import io.micronaut.build.services.JibConfigurationService;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.ResolutionScope;
27  import org.apache.maven.project.MavenProject;
28  
29  import javax.inject.Inject;
30  import java.io.File;
31  import java.io.IOException;
32  import java.nio.file.Files;
33  import java.nio.file.LinkOption;
34  import java.nio.file.StandardCopyOption;
35  
36  /**
37   * <p>Allows using a provided Dockerfile.</p>
38   * <p><strong>WARNING</strong>: this goal is not intended to be executed directly. Instead, specify the packaging type
39   * using the <code>packaging</code> property, eg:</p>
40   *
41   * <pre>mvn package -Dpackaging=docker</pre>
42   *
43   * @author Álvaro Sánchez-Mariscal
44   * @since 1.1
45   */
46  @Mojo(name = DockerMojo.DOCKER_PACKAGING, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
47  public class DockerMojo extends AbstractDockerMojo {
48  
49      public static final String DOCKER_PACKAGING = "docker";
50  
51      @SuppressWarnings("CdiInjectionPointsInspection")
52      @Inject
53      public DockerMojo(MavenProject mavenProject, JibConfigurationService jibConfigurationService,
54                        ApplicationConfigurationService applicationConfigurationService, DockerService dockerService) {
55          super(mavenProject, jibConfigurationService, applicationConfigurationService, dockerService);
56      }
57  
58      @Override
59      public void execute() throws MojoExecutionException {
60          File dockerfile = new File(mavenProject.getBasedir(), DockerfileMojo.DOCKERFILE);
61          if (dockerfile.exists()) {
62              try {
63                  getLog().info("Using provided Dockerfile: " + dockerfile.getAbsolutePath());
64                  mavenProject.getProperties().put(PropertyNames.SKIP, "true");
65  
66                  copyDependencies();
67  
68                  String targetDir = mavenProject.getBuild().getDirectory();
69                  File targetDockerfile = new File(targetDir, dockerfile.getName());
70                  Files.copy(dockerfile.toPath(), targetDockerfile.toPath(), LinkOption.NOFOLLOW_LINKS,
71                          StandardCopyOption.REPLACE_EXISTING);
72  
73                  BuildImageCmd buildImageCmd = dockerService.buildImageCmd()
74                          .withDockerfile(targetDockerfile)
75                          .withTags(getTags())
76                          .withBaseDirectory(new File(targetDir));
77                  dockerService.buildImage(buildImageCmd);
78              } catch (IOException e) {
79                  throw new MojoExecutionException(e.getMessage(), e);
80              }
81          } else if (!jibConfigurationService.getFromImage().isPresent()) {
82              mavenProject.getProperties().setProperty(PropertyNames.FROM_IMAGE, JibMicronautExtension.DEFAULT_BASE_IMAGE);
83          }
84      }
85  
86  }