1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.jlink;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.io.PrintWriter;
43 import java.io.StringWriter;
44 import java.util.List;
45 import java.util.Optional;
46 import java.util.spi.ToolProvider;
47
48 import org.apache.maven.plugin.MojoExecutionException;
49 import org.apache.maven.plugin.logging.Log;
50 import org.apache.maven.toolchain.Toolchain;
51
52
53
54
55
56
57
58 class JLinkExecutor extends AbstractJLinkToolchainExecutor {
59 private final ToolProvider toolProvider;
60
61 JLinkExecutor(Toolchain toolchain, Log log) {
62 super(toolchain, log);
63 this.toolProvider = getJLinkExecutable();
64 }
65
66 protected final ToolProvider getJLinkExecutable() {
67 return ToolProvider.findFirst("jlink").orElseThrow(() -> new IllegalStateException("No jlink tool found."));
68 }
69
70 @Override
71 public int executeJlink(List<String> jlinkArgs) throws MojoExecutionException {
72 if (getToolchain().isPresent()) {
73 return super.executeJlink(jlinkArgs);
74 }
75
76 if (getLog().isDebugEnabled()) {
77
78 getLog().debug(this.toolProvider.name() + " " + jlinkArgs);
79 }
80
81 try (StringWriter strErr = new StringWriter();
82 PrintWriter err = new PrintWriter(strErr);
83 StringWriter strOut = new StringWriter();
84 PrintWriter out = new PrintWriter(strOut)) {
85 int exitCode = this.toolProvider.run(out, err, jlinkArgs.toArray(new String[0]));
86 out.flush();
87 err.flush();
88
89 String outAsString = strOut.toString();
90 String output = outAsString.isBlank() ? null : '\n' + outAsString.trim();
91
92 if (exitCode != 0) {
93 if (output != null && !output.isBlank()) {
94
95
96 for (String outputLine : output.split("\n")) {
97 getLog().error(outputLine);
98 }
99 }
100
101 StringBuilder msg = new StringBuilder("\nExit code: ");
102 msg.append(exitCode);
103 String errAsString = strErr.toString();
104 if (errAsString != null && !errAsString.isBlank()) {
105 msg.append(" - ").append(errAsString);
106 }
107 msg.append('\n');
108 msg.append("Command line was: ")
109 .append(this.toolProvider.name())
110 .append(' ')
111 .append(jlinkArgs)
112 .append('\n')
113 .append('\n');
114
115 throw new MojoExecutionException(msg.toString());
116 }
117
118 if (output != null && !output.isBlank()) {
119
120 for (String outputLine : output.split("\n")) {
121 getLog().info(outputLine);
122 }
123 }
124
125 return exitCode;
126 } catch (IOException e) {
127 throw new MojoExecutionException("Unable to execute jlink command: " + e.getMessage(), e);
128 }
129 }
130
131 @Override
132 public Optional<File> getJmodsFolder( File sourceJdkModules) {
133 if (getToolchain().isPresent()) {
134 return super.getJmodsFolder(sourceJdkModules);
135 }
136
137 if (sourceJdkModules != null && sourceJdkModules.isDirectory()) {
138 return Optional.of(new File(sourceJdkModules, JMODS));
139 }
140
141
142 return Optional.empty();
143 }
144 }