/
CreateRuntimeImageMojo.java
234 lines (196 loc) · 8.72 KB
/
CreateRuntimeImageMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright 2017 - 2023 The ModiTect authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.moditect.mavenplugin.image;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.moditect.commands.CreateRuntimeImage;
import org.moditect.mavenplugin.image.model.Launcher;
import org.moditect.mavenplugin.util.DependencyHelper;
import org.moditect.mavenplugin.util.MojoLog;
import org.moditect.model.JarInclusionPolicy;
/**
* @author Gunnar Morling
*/
@Mojo(name = "create-runtime-image", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, threadSafe = true)
public class CreateRuntimeImageMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true)
protected MavenProject project;
@Component
private ToolchainManager toolchainManager;
@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession mavenSession;
@Parameter
private String baseJdk;
@Parameter(defaultValue = "[]")
private List<File> modulePath;
@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/image")
private File outputDirectory;
@Parameter(required = true)
private List<String> modules;
@Parameter
private JarInclusionPolicy jarInclusionPolicy;
@Parameter
private Launcher launcher;
@Parameter
private String compression;
@Parameter(defaultValue = "false")
private boolean stripDebug;
@Parameter
List<String> excludedResources;
@Parameter(property = "ignoreSigningInformation", defaultValue = "false")
private boolean ignoreSigningInformation;
// @Parameter(property = "moditect.artifact")
// private String artifactOverride;
//
// @Parameter(property = "moditect.additionalDependencies")
// private String additionalDependenciesOverride;
//
// @Parameter(property = "moditect.moduleName")
// private String moduleNameOverride;
//
// @Parameter(property = "moditect.exportExcludes")
// private String exportExcludesOverride;
@Parameter(defaultValue = "false")
private boolean noHeaderFiles;
@Parameter(defaultValue = "false")
private boolean noManPages;
@Parameter(defaultValue = "false")
private boolean bindServices;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Path jmodsDir = getJModsDir();
Set<Path> effectiveModulePath = this.modulePath.stream()
.map(File::toPath)
.collect(Collectors.toSet());
effectiveModulePath.add(jmodsDir);
final Artifact primaryProjectArtifact = project.getArtifact();
if (primaryProjectArtifact == null || !primaryProjectArtifact.getFile().exists()) {
throw new MojoFailureException(getNoProjectArtifactMessage(primaryProjectArtifact));
}
CreateRuntimeImage createRuntimeImage = new CreateRuntimeImage(
effectiveModulePath,
modules,
jarInclusionPolicy,
DependencyHelper.getDirectAndTransitiveDependencies(project),
primaryProjectArtifact.getFile().toPath(),
launcher != null ? launcher.getName() : null,
launcher != null ? launcher.getModule() : null,
outputDirectory.toPath(),
compression,
stripDebug,
ignoreSigningInformation,
getExcludeResourcesPatterns(),
new MojoLog(getLog()),
noHeaderFiles,
noManPages,
bindServices);
try {
createRuntimeImage.run();
}
catch (IOException ex) {
getLog().error(ex);
throw new MojoExecutionException("Error creating runtime image", ex);
}
}
private static String getNoProjectArtifactMessage(Artifact primaryProjectArtifact) {
final StringBuilder message = new StringBuilder();
message.append("No primary project artifact was found to create a runtime image.");
if (primaryProjectArtifact != null && primaryProjectArtifact.getFile() != null) {
message.append(" The file ");
message.append(primaryProjectArtifact.getFile());
message.append(" does not exist.");
}
message.append(" Please make sure the project artifact has been created (usually in the package lifecycle phase).");
message.append(" This goal should usually not run before artifact creation.");
return message.toString();
}
/**
* Returns the directory with the jmod files to be used for creating the image.
* If {@code baseJdk} has been given, the jmod files from the JDK identified that way
* will be used; otherwise the jmod files from the JDK running the current build
* will be used.
*/
private Path getJModsDir() throws MojoExecutionException {
if (baseJdk != null) {
List<Toolchain> toolChains = toolchainManager.getToolchains(mavenSession, "jdk", getToolChainRequirements(baseJdk));
if (toolChains.isEmpty()) {
throw new MojoExecutionException("Found no tool chain of type 'jdk' and matching requirements '" + baseJdk + "'");
}
else if (toolChains.size() > 1) {
throw new MojoExecutionException("Found more than one tool chain of type 'jdk' and matching requirements '" + baseJdk + "'");
}
else {
Toolchain toolchain = toolChains.get(0);
String javac = toolchain.findTool("javac");
// #63; when building on Linux / OS X but creating a Windows runtime image
// the tool lookup must be for javac.exe explicitly (as the toolchain mechanism
// itself won't append the suffix if not running this build on Windows
if (javac == null) {
javac = toolchain.findTool("javac.exe");
}
if (javac == null) {
throw new MojoExecutionException("Couldn't locate toolchain directory");
}
return new File(javac)
.toPath()
.getParent()
.getParent()
.resolve("jmods");
}
}
else {
String javaHome = System.getProperty("java.home");
return new File(javaHome).toPath().resolve("jmods");
}
}
private Map<String, String> getToolChainRequirements(String baseJdk) throws MojoExecutionException {
Map<String, String> toolChainRequirements = new HashMap<>();
String[] requirements = baseJdk.split(",");
for (String requirement : requirements) {
String[] keyAndValue = requirement.split("=");
if (keyAndValue.length != 2) {
throw new MojoExecutionException(
"Toolchain requirements must be given in the form 'key1=value1,key2=value2,...'." +
"Given value '" + baseJdk + "' doesn't match this pattern.");
}
toolChainRequirements.put(keyAndValue[0].trim(), keyAndValue[1].trim());
}
return toolChainRequirements;
}
private List<String> getExcludeResourcesPatterns() {
return excludedResources != null ? excludedResources : Collections.emptyList();
}
}