View Javadoc
1   package io.micronaut.build;
2   
3   import io.micronaut.build.services.ApplicationConfigurationService;
4   import io.micronaut.build.services.DockerService;
5   import io.micronaut.build.services.JibConfigurationService;
6   import org.apache.maven.artifact.Artifact;
7   import org.apache.maven.artifact.versioning.ArtifactVersion;
8   import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
9   import org.apache.maven.plugin.AbstractMojo;
10  import org.apache.maven.plugins.annotations.Parameter;
11  import org.apache.maven.project.MavenProject;
12  
13  import java.io.File;
14  import java.io.IOException;
15  import java.nio.file.Files;
16  import java.nio.file.StandardCopyOption;
17  import java.util.*;
18  import java.util.stream.Collectors;
19  
20  import static io.micronaut.build.DockerNativeMojo.DEFAULT_GRAAL_JVM_VERSION;
21  
22  /**
23   * Abstract base class for mojos related to Docker files and builds.
24   *
25   * @author Álvaro Sánchez-Mariscal
26   * @since 1.1
27   */
28  public abstract class AbstractDockerMojo extends AbstractMojo {
29  
30      protected final MavenProject mavenProject;
31      protected final JibConfigurationService jibConfigurationService;
32      protected final ApplicationConfigurationService applicationConfigurationService;
33      protected final DockerService dockerService;
34  
35  
36      /**
37       * Additional arguments that will be passed to the <code>native-image</code> executable. Note that this will only
38       * be used when using a packaging of type <code>docker-native</code>. For <code>native-image</code> packaging
39       * you should use the
40       * <a href="https://www.graalvm.org/reference-manual/native-image/NativeImageMavenPlugin/#maven-plugin-customization">
41       * Native Image Maven Plugin
42       * </a> configuration options.
43       */
44      @Parameter(property = "micronaut.native-image.args")
45      protected String nativeImageBuildArgs;
46  
47      /**
48       * List of additional arguments that will be passed to the application.
49       */
50      @Parameter(property = "mn.appArgs")
51      protected List<String> appArguments;
52  
53      /**
54       * The main class of the application, as defined in the
55       * <a href="https://www.mojohaus.org/exec-maven-plugin/java-mojo.html#mainClass">Exec Maven Plugin</a>.
56       */
57      @Parameter(defaultValue = "${exec.mainClass}", required = true)
58      protected String mainClass;
59  
60      /**
61       * Whether to produce a static native image when using <code>docker-native</code> packaging
62       */
63      @Parameter(defaultValue = "false", property = "micronaut.native-image.static")
64      protected Boolean staticNativeImage;
65  
66      /**
67       * The target runtime of the application
68       */
69      @Parameter(property = MicronautRuntime.PROPERTY, defaultValue = "NONE")
70      protected String micronautRuntime;
71  
72      protected AbstractDockerMojo(MavenProject mavenProject, JibConfigurationService jibConfigurationService, ApplicationConfigurationService applicationConfigurationService, DockerService dockerService) {
73          this.mavenProject = mavenProject;
74          this.jibConfigurationService = jibConfigurationService;
75          this.applicationConfigurationService = applicationConfigurationService;
76          this.dockerService = dockerService;
77      }
78  
79      protected ArtifactVersion javaVersion() {
80          return new DefaultArtifactVersion(Optional.ofNullable(mavenProject.getProperties().getProperty("maven.compiler.target")).orElse(System.getProperty("java.version")));
81      }
82  
83      protected String graalVmVersion() {
84          return mavenProject.getProperties().getProperty("graal.version");
85      }
86  
87      protected String graalVmJvmVersion() {
88          String graalVmJvmVersion = "java8";
89          if (javaVersion().getMajorVersion() >= 11) {
90              graalVmJvmVersion = "java11";
91          }
92          return graalVmJvmVersion;
93      }
94  
95      protected String getFrom() {
96          return jibConfigurationService.getFromImage().orElse("oracle/graalvm-ce:" + graalVmVersion() + "-" + DEFAULT_GRAAL_JVM_VERSION);
97      }
98  
99      protected Set<String> getTags() {
100         Set<String> tags = new HashSet<>();
101         Optional<String> toImageOptional = jibConfigurationService.getToImage();
102         String imageName = mavenProject.getArtifactId();
103         if (toImageOptional.isPresent()) {
104             tags.add(toImageOptional.get());
105             imageName = toImageOptional.get().split(":")[0];
106         } else {
107             tags.add(imageName + ":latest");
108         }
109         for (String tag : jibConfigurationService.getTags()) {
110             tags.add(imageName + ":" + tag);
111         }
112         return tags;
113     }
114 
115     protected String getPort() {
116         Map<String, Object> applicationConfiguration = applicationConfigurationService.getApplicationConfiguration();
117         String port = applicationConfiguration.getOrDefault("MICRONAUT_SERVER_PORT", applicationConfiguration.getOrDefault("micronaut.server.port", 8080)).toString();
118         return "-1".equals(port) ? "8080" : port;
119     }
120 
121     protected void copyDependencies() throws IOException {
122         List<String> imageClasspathScopes = Arrays.asList(Artifact.SCOPE_COMPILE, Artifact.SCOPE_RUNTIME);
123         mavenProject.setArtifactFilter(artifact -> imageClasspathScopes.contains(artifact.getScope()));
124         File target = new File(mavenProject.getBuild().getDirectory(), "dependency");
125         if (!target.exists()) {
126             target.mkdirs();
127         }
128         for (Artifact dependency : mavenProject.getArtifacts()) {
129             Files.copy(dependency.getFile().toPath(), target.toPath().resolve(dependency.getFile().getName()), StandardCopyOption.REPLACE_EXISTING);
130         }
131     }
132 
133     protected String getCmd() {
134         return "CMD [" +
135                 appArguments.stream()
136                         .map(s -> "\"" + s + "\"")
137                         .collect(Collectors.joining(", ")) +
138                 "]";
139     }
140 
141 }