1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
40
41
42
43
44
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 }