-
Notifications
You must be signed in to change notification settings - Fork 2
/
BuildImage.java
558 lines (461 loc) · 20.4 KB
/
BuildImage.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package de.fluxparticle.jpackage;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.modules.ModuleDeclaration;
import com.github.javaparser.ast.modules.ModuleExportsDirective;
import com.github.javaparser.ast.modules.ModuleRequiresDirective;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
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.Execute;
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.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ModuleVisitor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.stream.IntStream;
import java.util.zip.ZipEntry;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_9;
import static java.lang.String.join;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static org.objectweb.asm.Opcodes.ACC_MANDATED;
import static org.objectweb.asm.Opcodes.ACC_MODULE;
import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC;
import static org.objectweb.asm.Opcodes.V9;
/**
* jPackageMavenPlugin - A Maven Plugin to patch all non-modular dependencies and runs jpackage (JDK 14)
* Copyright (C) 2020 Sven Reinck
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/gpl-3.0
*/
@Mojo(name = "image", requiresDependencyResolution = ResolutionScope.RUNTIME)
@Execute(phase = LifecyclePhase.COMPILE)
public class BuildImage extends AbstractMojo {
private static final Path JAVA_HOME = Path.of(System.getProperty("java.home"));
static {
StaticJavaParser.getConfiguration().setLanguageLevel(JAVA_9);
}
@Component
private ArtifactResolver artifactResolver;
@Parameter(defaultValue = "${project}", required = true)
private MavenProject project;
@Parameter(defaultValue = "${session}", required = true, readonly = true)
private MavenSession session;
@Parameter(property = "skip", readonly = true)
private boolean skip;
@Parameter(property = "name", readonly = true)
private String name;
@Parameter(property = "mainClass", readonly = true)
private String mainClass;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
return;
}
if (name == null) {
throw new MojoFailureException("name required");
}
if (mainClass == null) {
throw new MojoFailureException("mainClass required");
}
try {
String version = project.getArtifact().getVersion().replace("-SNAPSHOT", "");
String target = project.getBuild().getDirectory();
Path modulesDir = Path.of(target, "modules");
List<String> classpathElements = processJars(project.getRuntimeClasspathElements(), modulesDir);
Path appDir = Path.of(target, name + ".app");
if (Files.exists(appDir)) {
System.out.println("Deleting: " + appDir);
deleteDir(appDir);
}
String modulePath = join(":", classpathElements);
if (!jPackage(name, version, modulePath, mainClass, target)) {
throw new MojoExecutionException("jpackage error");
}
} catch (IOException | DependencyResolutionRequiredException | InterruptedException e) {
throw new MojoExecutionException(e.toString(), e);
}
}
private List<String> processJars(List<String> classpathElements, Path modulesDir) throws IOException, InterruptedException {
List<String> result = new ArrayList<>();
List<List<String>> lines = new ArrayList<>();
lines.add(Arrays.asList("classpathElements:", "modular:"));
for (int i = 0; i < classpathElements.size(); i++) {
String classpathElement = classpathElements.get(i);
Path path = Path.of(classpathElement);
String fileName = path.getFileName().toString();
if (!fileName.endsWith(".jar")) {
result.add(classpathElement);
continue;
}
List<String> line = new ArrayList<>();
line.add(fileName);
String modular;
if (isModular(classpathElement)) {
result.add(classpathElement);
modular = "yes";
} else {
String newElement = null;
String action = null;
if (fileName.startsWith("kotlin-stdlib-")) {
newElement = replaceKotlinStdLib(fileName);
action = "replaced";
}
if (fileName.startsWith("javafx-")) {
newElement = "";
}
if (newElement == null) {
String modulePath = classpathElements.stream()
.filter(p -> p.endsWith(".jar"))
.filter(p -> !p.equals(path.toString()))
.collect(joining(":"));
newElement = fix(modulesDir, modulePath, path);
action = "fixed";
}
if (newElement == null) {
modular = "no";
} else if (newElement.isEmpty()) {
modular = "removed";
} else {
result.add(newElement);
modular = action;
}
}
line.add(modular);
lines.add(line);
}
printLines(lines);
return result;
}
private static String fix(Path modulesDir, String modulePath, Path jar) throws IOException, InterruptedException {
String fileName = jar.getFileName().toString();
Path target = modulesDir.resolve(fileName);
if (Files.exists(target)) {
System.out.println("Already Fixed: " + fileName);
} else try {
System.out.println("Fix: " + jar);
if (!jDeps(modulesDir, modulePath, jar)) {
return null;
}
int splitVersion = fileName.lastIndexOf('-');
String moduleName = fileName.substring(0, splitVersion).replace('-', '.');
Path mod = modulesDir.resolve(moduleName);
Path moduleInfo = mod.resolve("module-info.java");
patch(jar, moduleInfo, target);
/*
Path out = mod.resolve("classes");
extract(jar, out);
if (!javaCompiler(modulePath, moduleInfo, out)) {
return null;
}
pack(out, target);
*/
} finally {
System.out.println();
}
return target.toString();
}
private static void extract(Path jar, Path out) throws IOException {
deleteDir(out);
System.out.println("Extract: " + jar);
JarFile jarFile = new JarFile(jar.toString());
Iterator<JarEntry> iterator = jarFile.entries().asIterator();
while (iterator.hasNext()) {
JarEntry entry = iterator.next();
String name = entry.getName();
Path path = out.resolve(name);
if (name.endsWith("/")) {
Files.createDirectories(path);
} else {
Files.copy(jarFile.getInputStream(entry), path);
}
}
}
private static void pack(Path out, Path jar) throws IOException {
System.out.println("Pack: " + jar);
try (JarOutputStream target = new JarOutputStream(new FileOutputStream(jar.toString()))) {
Files.walkFileTree(out, new FileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
JarEntry entry = new JarEntry(out.relativize(dir).toString() + "/");
target.putNextEntry(entry);
target.closeEntry();
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
JarEntry entry = new JarEntry(out.relativize(file).toString());
target.putNextEntry(entry);
Files.copy(file, target);
target.closeEntry();
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
}
private static boolean jDeps(Path modulesDir, String modulePath, Path path) throws IOException, InterruptedException {
Path jDepsBinary = JAVA_HOME.resolve("bin/jdeps");
// List<String> versions = getVersions(path.toString());
// if (versions.isEmpty()) {
String[] cmdArray = {
jDepsBinary.toString(),
"--generate-module-info", modulesDir.toString(),
"--module-path", modulePath,
path.toString(),
};
return exec(cmdArray);
// } else for (String version : versions) {
// String[] cmdArray = {
// jDepsBinary.toString(),
// "--multi-release", version,
// "--generate-module-info", modulesDir.toString(),
// "--module-path", modulePath,
// path.toString(),
// };
//
// exec(cmdArray);
// }
}
private String replaceKotlinStdLib(String fileName) {
String element;
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
DefaultArtifactCoordinate artifactCoordinate = toArtifactCoordinate(fileName);
artifactCoordinate.setClassifier("modular");
try {
ArtifactResult artifactResult = artifactResolver.resolveArtifact(buildingRequest, artifactCoordinate);
element = artifactResult.getArtifact().getFile().toString();
} catch (ArtifactResolverException | IllegalArgumentException e) {
element = null;
}
return element;
}
private static void deleteDir(Path dir) throws IOException {
Files.walkFileTree(dir, new FileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
private static void printLines(List<List<String>> lines) {
Map<Integer, Integer> cols = lines.stream()
.flatMap(line -> IntStream.range(0, line.size()).boxed()
.collect(toMap(
identity(),
idx -> line.get(idx).length()
))
.entrySet().stream()
)
.collect(toMap(
Entry::getKey,
Entry::getValue,
Math::max
));
for (List<String> line : lines) {
StringBuilder sb = new StringBuilder();
String delimiter = "";
for (int i = 0; i < line.size(); i++) {
sb.append(delimiter); delimiter = " ";
String str = line.get(i);
sb.append(str);
sb.append(" ".repeat(cols.get(i) - str.length()));
}
System.out.println(sb);
}
}
private static boolean jPackage(String name, String version, String modulePath, String mainClass, String target) throws IOException, InterruptedException {
Path jPackageBinary = JAVA_HOME.resolve("bin/jpackage");
String[] cmdArray = {
jPackageBinary.toString(),
"--type", "app-image",
"--name", name,
"--app-version", version,
"--module-path", modulePath,
"--module", mainClass,
"--dest", target,
// "--verbose",
};
System.out.println(String.join(" ", cmdArray));
return exec(cmdArray);
}
private static boolean javaCompiler(String modulePath, Path moduleInfo, Path out) throws IOException, InterruptedException {
System.out.println("Compile: " + moduleInfo);
Path javaCompilerBinary = JAVA_HOME.resolve("bin/javac");
String[] cmdArray = {
javaCompilerBinary.toString(),
"--module-path", modulePath,
"-d", out.toString(),
moduleInfo.toString(),
};
return exec(cmdArray);
}
private static void patch(Path inputJar, Path moduleInfo, Path outputJar) {
System.out.println("Compile (In-Memory): " + moduleInfo);
try {
CompilationUnit compilationUnit = StaticJavaParser.parse(moduleInfo);
ModuleDeclaration module = compilationUnit.getModule().get();
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V9, ACC_MODULE, "module-info", null, null, null);
ModuleVisitor mv = classWriter.visitModule(module.getNameAsString(), ACC_SYNTHETIC, null);
for (ModuleRequiresDirective requires : module.findAll(ModuleRequiresDirective.class)) {
mv.visitRequire(
requires.getName().asString(),
0,
null
);
}
for (ModuleExportsDirective export : module.findAll(ModuleExportsDirective.class)) {
mv.visitExport(
export.getNameAsString().replace('.', '/'),
0,
export.getModuleNames()
.stream()
.map(Name::toString)
.toArray(String[]::new)
);
}
mv.visitRequire("java.base", ACC_MANDATED, null);
mv.visitEnd();
classWriter.visitEnd();
byte[] bytes = classWriter.toByteArray();
try (JarOutputStream targetStream = new JarOutputStream(new FileOutputStream(outputJar.toString()))) {
{
JarEntry entry = new JarEntry("module-info.class");
targetStream.putNextEntry(entry);
targetStream.write(bytes);
targetStream.closeEntry();
}
JarFile jarFile = new JarFile(inputJar.toString());
Iterator<JarEntry> iterator = jarFile.entries().asIterator();
while (iterator.hasNext()) {
JarEntry entry = iterator.next();
String name = entry.getName();
if (name.endsWith("/")) {
targetStream.putNextEntry(entry);
targetStream.closeEntry();
} else {
targetStream.putNextEntry(entry);
try (InputStream stream = jarFile.getInputStream(entry)) {
stream.transferTo(targetStream);
}
targetStream.closeEntry();
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static boolean exec(String[] cmdArray) throws IOException, InterruptedException {
Process process = new ProcessBuilder(cmdArray).inheritIO().start();
// Runtime.getRuntime().exec(cmdArray);
try (Scanner sc = new Scanner(process.getInputStream())) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
System.out.println(line);
}
}
if (process.waitFor() != 0) {
try (Scanner sc = new Scanner(process.getErrorStream())) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
System.out.println(line);
}
}
return false;
}
return true;
}
private DefaultArtifactCoordinate toArtifactCoordinate(String fileName) {
DefaultArtifactCoordinate artifactCoordinate = new DefaultArtifactCoordinate();
int splitVersion = fileName.lastIndexOf('-');
int splitExtension = fileName.lastIndexOf('.');
artifactCoordinate.setGroupId("org.jetbrains.kotlin");
artifactCoordinate.setArtifactId(fileName.substring(0, splitVersion));
artifactCoordinate.setVersion(fileName.substring(splitVersion + 1, splitExtension));
artifactCoordinate.setExtension(fileName.substring(splitExtension + 1));
return artifactCoordinate;
}
private static boolean isModular(String jar) throws IOException {
return new JarFile(jar).stream()
.anyMatch(jarEntry -> jarEntry.getName().equals("module-info.class"));
}
private static List<String> getVersions(String jar) throws IOException {
JarFile jarFile = new JarFile(jar);
Manifest manifest = jarFile.getManifest();
System.out.println("manifest = " + manifest.getMainAttributes().keySet());
String prefixVersions = "META-INF/versions/";
return jarFile.stream()
.map(ZipEntry::toString)
.filter(s -> s.startsWith(prefixVersions))
.map(s -> s.substring(prefixVersions.length()))
.collect(toList());
}
}