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.lang.reflect.Method;
42 import java.util.Collection;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Optional;
46
47 import org.apache.maven.execution.MavenSession;
48 import org.apache.maven.plugin.AbstractMojo;
49 import org.apache.maven.plugins.annotations.Component;
50 import org.apache.maven.plugins.annotations.Parameter;
51 import org.apache.maven.project.MavenProject;
52 import org.apache.maven.toolchain.Toolchain;
53 import org.apache.maven.toolchain.ToolchainManager;
54
55
56
57
58 public abstract class AbstractJLinkMojo extends AbstractMojo {
59
60
61
62
63
64
65
66 @Parameter
67 private Map<String, String> jdkToolchain;
68
69 @Parameter(defaultValue = "${project}", readonly = true, required = true)
70 private MavenProject project;
71
72 @Parameter(defaultValue = "${session}", readonly = true, required = true)
73 private MavenSession session;
74
75 @Component
76 private ToolchainManager toolchainManager;
77
78
79
80
81
82 protected abstract String getClassifier();
83
84 protected JLinkExecutor getJlinkExecutor() {
85 return new JLinkExecutor(getToolchain().orElse(null), getLog());
86 }
87
88 protected Optional<Toolchain> getToolchain() {
89 Toolchain tc = null;
90
91 if (jdkToolchain != null) {
92
93 try {
94 Method getToolchainsMethod = toolchainManager
95 .getClass()
96 .getMethod("getToolchains", MavenSession.class, String.class, Map.class);
97
98 @SuppressWarnings("unchecked")
99 List<Toolchain> tcs = (List<Toolchain>)
100 getToolchainsMethod.invoke(toolchainManager, getSession(), "jdk", jdkToolchain);
101
102 if (tcs != null && tcs.size() > 0) {
103 tc = tcs.get(0);
104 }
105 } catch (ReflectiveOperationException | SecurityException | IllegalArgumentException e) {
106
107 }
108 }
109
110 if (tc == null) {
111
112 tc = toolchainManager.getToolchainFromBuildContext("jdk", getSession());
113 }
114
115 return Optional.ofNullable(tc);
116 }
117
118 protected MavenProject getProject() {
119 return project;
120 }
121
122 protected MavenSession getSession() {
123 return session;
124 }
125
126
127
128
129
130
131
132
133
134
135 protected File getArchiveFile(File basedir, String finalName, String classifier, String archiveExt) {
136 if (basedir == null) {
137 throw new IllegalArgumentException("basedir is not allowed to be null");
138 }
139 if (finalName == null) {
140 throw new IllegalArgumentException("finalName is not allowed to be null");
141 }
142 if (archiveExt == null) {
143 throw new IllegalArgumentException("archiveExt is not allowed to be null");
144 }
145
146 if (finalName.isEmpty()) {
147 throw new IllegalArgumentException("finalName is not allowed to be empty.");
148 }
149 if (archiveExt.isEmpty()) {
150 throw new IllegalArgumentException("archiveExt is not allowed to be empty.");
151 }
152
153 StringBuilder fileName = new StringBuilder(finalName);
154
155 if (hasClassifier(classifier)) {
156 fileName.append("-").append(classifier);
157 }
158
159 fileName.append('.');
160 fileName.append(archiveExt);
161
162 return new File(basedir, fileName.toString());
163 }
164
165 protected boolean hasClassifier(String classifier) {
166 boolean result = false;
167 if (classifier != null && !classifier.isEmpty()) {
168 result = true;
169 }
170
171 return result;
172 }
173
174
175
176
177
178
179
180
181 protected StringBuilder convertSeparatedModulePathToPlatformSeparatedModulePath(String pluginModulePath) {
182 StringBuilder sb = new StringBuilder();
183
184
185 String[] splitModule = pluginModulePath.split("[;:]");
186 for (String module : splitModule) {
187 if (sb.length() > 0) {
188 sb.append(File.pathSeparatorChar);
189 }
190 sb.append(module);
191 }
192 return sb;
193 }
194
195
196
197
198
199
200
201 protected String getPlatformDependSeparateList(Collection<String> modulePaths) {
202 return String.join(Character.toString(File.pathSeparatorChar), modulePaths);
203 }
204
205
206
207
208
209
210 protected String getCommaSeparatedList(Collection<String> modules) {
211 return String.join(",", modules);
212 }
213 }