/
GenerateModuleList.java
79 lines (67 loc) · 2.65 KB
/
GenerateModuleList.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
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright The original authors
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.moditect.commands;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.spi.ToolProvider;
import java.util.stream.Collectors;
import org.moditect.internal.command.LogWriter;
import org.moditect.model.Version;
import org.moditect.spi.log.Log;
public class GenerateModuleList {
private final Path projectJar;
private final Set<Path> dependencies;
private final Version jvmVersion;
private final Log log;
private final ToolProvider jdeps;
public GenerateModuleList(Path projectJar, Set<Path> dependencies, Version jvmVersion, Log log) {
this.projectJar = projectJar;
this.dependencies = dependencies;
this.jvmVersion = jvmVersion;
this.log = log;
this.jdeps = ToolProvider
.findFirst("jdeps")
.orElseThrow(() -> new RuntimeException("jdeps tool not found"));
}
public void run() {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
PrintStream out = new PrintStream(outStream);
jdeps.run(out, System.err, "--version");
out.close();
int jdepsVersion = Runtime.Version
.parse(outStream.toString().strip())
.feature();
if (jdepsVersion < 12) {
log.error("The jdeps option this plugin uses to list JDK modules only works flawlessly on JDK 12+, so please use that to run this goal.");
return;
}
List<String> command = new ArrayList<>();
command.add("--print-module-deps");
command.add("--ignore-missing-deps");
command.add("--multi-release");
command.add(String.valueOf(jvmVersion.feature()));
command.add("--class-path");
String classPath = dependencies.stream()
.map(Path::toAbsolutePath)
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));
command.add(classPath);
command.add(projectJar.toAbsolutePath().toString());
log.debug("Running jdeps " + String.join(" ", command));
LogWriter logWriter = new LogWriter(log);
int result = jdeps.run(logWriter, logWriter, command.toArray(new String[0]));
if (result != 0) {
throw new IllegalStateException("Invocation of jdeps failed: jdeps " + String.join(" ", command));
}
}
}