artifacts = project.getDependencies().stream()
153 | .map(d -> new DefaultArtifact(d.getGroupId(), d.getArtifactId(), d.getClassifier(), d.getType(), d.getVersion()))
154 | .flatMap(a -> resolver.resolve(a).stream())
155 | .collect(Collectors.toSet());
156 | if (artifacts != null) {
157 | project.setArtifacts(artifacts);
158 | }
159 | }
160 | setVariableValueToObject(mojo, "compilePath", project.getCompileClasspathElements());
161 | setVariableValueToObject(mojo, "session", session);
162 | setVariableValueToObject(mojo, "executable", "java");
163 | setVariableValueToObject(mojo, "basedir", new File(getBasedir(), testPom.getParent()));
164 |
165 | return mojo;
166 | }
167 |
168 | private String execute(AbstractMojo mojo) throws MojoFailureException, MojoExecutionException, InterruptedException {
169 | PrintStream out = System.out;
170 | StringOutputStream stringOutputStream = new StringOutputStream();
171 | System.setOut(new PrintStream(stringOutputStream));
172 | mojo.setLog(new DefaultLog(new ConsoleLogger(Logger.LEVEL_ERROR, "javafx:run")));
173 |
174 | try {
175 | mojo.execute();
176 | } finally {
177 | Thread.sleep(300);
178 | System.setOut(out);
179 | }
180 |
181 | return stringOutputStream.toString();
182 | }
183 |
184 | private void setUpProject(File pomFile, AbstractMojo mojo) throws Exception {
185 | super.setUp();
186 |
187 | MockitoAnnotations.initMocks(this);
188 |
189 | ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
190 | buildingRequest.setResolveDependencies(true);
191 | when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
192 | DefaultRepositorySystemSession repositorySession = MavenRepositorySystemUtils.newSession();
193 | repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
194 | .newInstance(repositorySession, new LocalRepository(RepositorySystem.defaultUserLocalRepository)));
195 | when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
196 |
197 | ProjectBuilder builder = lookup(ProjectBuilder.class);
198 | ProjectBuildingResult build = builder.build(pomFile, buildingRequest);
199 | MavenProject project = build.getProject();
200 |
201 | project.getBuild().setOutputDirectory(new File( "target/test-classes").getAbsolutePath());
202 | setVariableValueToObject(mojo, "project", project);
203 | }
204 |
205 | private void checkMojo(String expectedCommandLine) {
206 | assertEquals(1, mojo.getAmountExecutedCommandLines());
207 | CommandLine commandline = mojo.getExecutedCommandline(0);
208 | // do NOT depend on Commandline toString()
209 | assertEquals(expectedCommandLine, getCommandLineAsString(commandline));
210 | }
211 |
212 | private String getCommandLineAsString(CommandLine commandline) {
213 | // for the sake of the test comparisons, cut out the eventual
214 | // cmd /c *.bat conversion
215 | String result = commandline.getExecutable();
216 | boolean isCmd = false;
217 | if (OS.isFamilyWindows() && result.equals("cmd")) {
218 | result = "";
219 | isCmd = true;
220 | }
221 | String[] arguments = commandline.getArguments();
222 | for (int i = 0; i < arguments.length; i++) {
223 | String arg = arguments[i];
224 | if (isCmd && i == 0 && "/c".equals(arg)) {
225 | continue;
226 | }
227 | if (isCmd && i == 1 && arg.endsWith(".bat")) {
228 | arg = arg.substring(0, arg.length() - ".bat".length());
229 | }
230 | result += (result.length() == 0 ? "" : " ") + arg;
231 | }
232 | return result;
233 | }
234 |
235 | @Test
236 | public void testSplitComplexArgumentString() {
237 | String option = "param1 " +
238 | "param2 \n " +
239 | "param3\n" +
240 | "param4=\"/path/to/my file.log\" " +
241 | "'var\"foo var\"foo' " +
242 | "'var\"foo' " +
243 | "'var\"foo' " +
244 | "\"foo'var foo'var\" " +
245 | "\"foo'var\" " +
246 | "\"foo'var\"";
247 |
248 | String expected = "START," +
249 | "param1," +
250 | "param2," +
251 | "param3," +
252 | "param4=\"/path/to/my file.log\"," +
253 | "'var\"foo var\"foo'," +
254 | "'var\"foo'," +
255 | "'var\"foo'," +
256 | "\"foo'var foo'var\"," +
257 | "\"foo'var\"," +
258 | "\"foo'var\"";
259 |
260 | String splitedOption = new JavaFXRunMojo().splitComplexArgumentStringAdapter(option)
261 | .stream().reduce("START", (s1, s2) -> s1 + "," + s2);
262 |
263 | Assert.assertEquals(expected, splitedOption);
264 | }
265 | }
266 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/src/main/java/org/openjfx/JavaFXJLinkMojo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Gluon
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.openjfx;
18 |
19 | import org.apache.commons.exec.CommandLine;
20 | import org.apache.commons.exec.DefaultExecutor;
21 | import org.apache.commons.exec.ExecuteException;
22 | import org.apache.commons.exec.Executor;
23 | import org.apache.commons.exec.OS;
24 | import org.apache.maven.plugin.MojoExecutionException;
25 | import org.apache.maven.plugin.MojoFailureException;
26 | import org.apache.maven.plugins.annotations.Component;
27 | import org.apache.maven.plugins.annotations.Execute;
28 | import org.apache.maven.plugins.annotations.LifecyclePhase;
29 | import org.apache.maven.plugins.annotations.Mojo;
30 | import org.apache.maven.plugins.annotations.Parameter;
31 | import org.apache.maven.plugins.annotations.ResolutionScope;
32 | import org.codehaus.plexus.archiver.Archiver;
33 | import org.codehaus.plexus.archiver.ArchiverException;
34 | import org.codehaus.plexus.archiver.zip.ZipArchiver;
35 | import org.codehaus.plexus.util.IOUtil;
36 | import org.codehaus.plexus.util.StringUtils;
37 |
38 | import java.io.ByteArrayOutputStream;
39 | import java.io.File;
40 | import java.io.FileOutputStream;
41 | import java.io.IOException;
42 | import java.nio.file.Files;
43 | import java.nio.file.Path;
44 | import java.nio.file.Paths;
45 | import java.util.ArrayList;
46 | import java.util.Comparator;
47 | import java.util.List;
48 | import java.util.Map;
49 | import java.util.Objects;
50 | import java.util.regex.Pattern;
51 | import java.util.stream.Collectors;
52 |
53 | @Mojo(name = "jlink", requiresDependencyResolution = ResolutionScope.RUNTIME)
54 | @Execute(phase = LifecyclePhase.PROCESS_CLASSES)
55 | public class JavaFXJLinkMojo extends JavaFXBaseMojo {
56 |
57 | private static final Pattern JLINK_VERSION_PATTERN = Pattern.compile("(1[3-9]|[2-9][0-9]|\\d{3,})");
58 |
59 | /**
60 | * Strips debug information out, equivalent to -G, --strip-debug,
61 | * default false
62 | */
63 | @Parameter(property = "javafx.stripDebug", defaultValue = "false")
64 | private boolean stripDebug;
65 |
66 | /**
67 | * Strip Java debug attributes out, equivalent to --strip-java-debug-attributes,
68 | * default false
69 | */
70 | @Parameter(property = "javafx.stripJavaDebugAttributes", defaultValue = "false")
71 | private boolean stripJavaDebugAttributes;
72 |
73 | /**
74 | * Compression level of the resources being used, equivalent to:
75 | * -c, --compress=level. Valid values: 0, 1, 2,
76 | * default 0
77 | */
78 | @Parameter(property = "javafx.compress", defaultValue = "0")
79 | private Integer compress;
80 |
81 | /**
82 | * Remove the includes directory in the resulting runtime image,
83 | * equivalent to: --no-header-files, default false
84 | */
85 | @Parameter(property = "javafx.noHeaderFiles", defaultValue = "false")
86 | private boolean noHeaderFiles;
87 |
88 | /**
89 | * Remove the man directory in the resulting Java runtime image,
90 | * equivalent to: --no-man-pages, default false
91 | */
92 | @Parameter(property = "javafx.noManPages", defaultValue = "false")
93 | private boolean noManPages;
94 |
95 | /**
96 | * Add the option --bind-services or not, default false.
97 | */
98 | @Parameter(property = "javafx.bindServices", defaultValue = "false")
99 | private boolean bindServices;
100 |
101 | /**
102 | * --ignore-signing-information, default false
103 | */
104 | @Parameter(property = "javafx.ignoreSigningInformation", defaultValue = "false")
105 | private boolean ignoreSigningInformation;
106 |
107 | /**
108 | * Turn on verbose mode, equivalent to: --verbose, default false
109 | */
110 | @Parameter(property = "javafx.jlinkVerbose", defaultValue = "false")
111 | private boolean jlinkVerbose;
112 |
113 | /**
114 | * Add a launcher script, equivalent to:
115 | * --launcher <name>=<module>[/<mainclass>].
116 | */
117 | @Parameter(property = "javafx.launcher")
118 | private String launcher;
119 |
120 | /**
121 | * The name of the folder with the resulting runtime image,
122 | * equivalent to --output <path>
123 | */
124 | @Parameter(property = "javafx.jlinkImageName", defaultValue = "image")
125 | private String jlinkImageName;
126 |
127 | /**
128 | * When set, creates a zip of the resulting runtime image.
129 | */
130 | @Parameter(property = "javafx.jlinkZipName")
131 | private String jlinkZipName;
132 |
133 | /**
134 | *
135 | * The executable. Can be a full path or the name of the executable.
136 | * In the latter case, the executable must be in the PATH for the execution to work.
137 | *
138 | */
139 | @Parameter(property = "javafx.jlinkExecutable", defaultValue = "jlink")
140 | private String jlinkExecutable;
141 |
142 | /**
143 | * Optional jmodsPath path for local builds.
144 | */
145 | @Parameter(property = "javafx.jmodsPath")
146 | private String jmodsPath;
147 |
148 | /**
149 | * The JAR archiver needed for archiving the environments.
150 | */
151 | @Component(role = Archiver.class, hint = "zip")
152 | private ZipArchiver zipArchiver;
153 |
154 | public void execute() throws MojoExecutionException {
155 | if (skip) {
156 | getLog().info( "skipping execute as per configuration" );
157 | return;
158 | }
159 |
160 | if (jlinkExecutable == null) {
161 | throw new MojoExecutionException("The parameter 'jlinkExecutable' is missing or invalid");
162 | }
163 |
164 | if (basedir == null) {
165 | throw new IllegalStateException( "basedir is null. Should not be possible." );
166 | }
167 |
168 | handleWorkingDirectory();
169 |
170 | Map enviro = handleSystemEnvVariables();
171 | CommandLine commandLine = getExecutablePath(jlinkExecutable, enviro, workingDirectory);
172 |
173 | if (isTargetUsingJava8(commandLine)) {
174 | getLog().info("Jlink not supported with Java 1.8");
175 | return;
176 | }
177 |
178 | if (stripJavaDebugAttributes && !isJLinkVersion13orHigher(commandLine.getExecutable())) {
179 | stripJavaDebugAttributes = false;
180 | getLog().warn("JLink parameter --strip-java-debug-attributes only supported for version 13 and higher");
181 | getLog().warn("The option 'stripJavaDebugAttributes' was skipped");
182 | }
183 |
184 | try {
185 |
186 | List commandArguments = createCommandArguments();
187 | String[] args = commandArguments.toArray(new String[commandArguments.size()]);
188 | commandLine.addArguments(args, false);
189 | getLog().debug("Executing command line: " + commandLine);
190 |
191 | Executor exec = new DefaultExecutor();
192 | exec.setWorkingDirectory(workingDirectory);
193 |
194 | try {
195 | int resultCode;
196 | if (outputFile != null) {
197 | if ( !outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs()) {
198 | getLog().warn( "Could not create non existing parent directories for log file: " + outputFile );
199 | }
200 |
201 | FileOutputStream outputStream = null;
202 | try {
203 | outputStream = new FileOutputStream(outputFile);
204 | resultCode = executeCommandLine(exec, commandLine, enviro, outputStream);
205 | } finally {
206 | IOUtil.close(outputStream);
207 | }
208 | } else {
209 | resultCode = executeCommandLine(exec, commandLine, enviro, System.out, System.err);
210 | }
211 |
212 | if (resultCode != 0) {
213 | String message = "Result of " + commandLine.toString() + " execution is: '" + resultCode + "'.";
214 | getLog().error(message);
215 | throw new MojoExecutionException(message);
216 | }
217 |
218 | if (launcher != null && ! launcher.isEmpty()) {
219 | patchLauncherScript(launcher);
220 |
221 | if (OS.isFamilyWindows()) {
222 | patchLauncherScript(launcher + ".bat");
223 | }
224 | }
225 |
226 | if (jlinkZipName != null && ! jlinkZipName.isEmpty()) {
227 | getLog().debug("Creating zip of runtime image");
228 | File createZipArchiveFromImage = createZipArchiveFromImage();
229 | project.getArtifact().setFile(createZipArchiveFromImage);
230 | }
231 |
232 | } catch (ExecuteException e) {
233 | getLog().error("Command execution failed.", e);
234 | e.printStackTrace();
235 | throw new MojoExecutionException("Command execution failed.", e);
236 | } catch (IOException e) {
237 | getLog().error("Command execution failed.", e);
238 | throw new MojoExecutionException("Command execution failed.", e);
239 | }
240 | } catch (Exception e) {
241 | throw new MojoExecutionException("Error", e);
242 | }
243 | }
244 |
245 | private void patchLauncherScript(String launcherFilename) throws IOException {
246 | Path launcherPath = Paths.get(builddir.getAbsolutePath(), jlinkImageName, "bin", launcherFilename);
247 |
248 | if (!Files.exists(launcherPath)) {
249 | getLog().debug("Launcher file not exist: " + launcherPath);
250 | return;
251 | }
252 |
253 | if (options != null) {
254 | String optionsString = options.stream()
255 | .filter(Objects::nonNull)
256 | .filter(String.class::isInstance)
257 | .map(String.class::cast)
258 | .collect(Collectors.joining(" "));
259 |
260 | // Add vm options to launcher script
261 | List lines = Files.lines(launcherPath)
262 | .map(line -> {
263 | boolean unixOptionsLine = "JLINK_VM_OPTIONS=".equals(line);
264 | boolean winOptionsLine = "set JLINK_VM_OPTIONS=".equals(line);
265 |
266 | if (unixOptionsLine || winOptionsLine) {
267 | String lineWrapper = unixOptionsLine ? "\"" : "";
268 | return line + lineWrapper + optionsString + lineWrapper;
269 | }
270 | return line;
271 | })
272 | .collect(Collectors.toList());
273 | Files.write(launcherPath, lines);
274 | }
275 |
276 | if (commandlineArgs != null) {
277 | // Add options to launcher script
278 | List lines = Files.lines(launcherPath)
279 | .map(line -> {
280 | if (line.endsWith("$@")) {
281 | return line.replace("$@", commandlineArgs + " $@");
282 | }
283 | return line;
284 | })
285 | .collect(Collectors.toList());
286 | Files.write(launcherPath, lines);
287 | }
288 | }
289 |
290 | private List createCommandArguments() throws MojoExecutionException, MojoFailureException {
291 | List commandArguments = new ArrayList<>();
292 | preparePaths(getParent(Paths.get(jlinkExecutable), 2));
293 | if (modulepathElements != null && !modulepathElements.isEmpty()) {
294 | commandArguments.add(" --module-path");
295 | String modulePath = StringUtils.join(modulepathElements.iterator(), File.pathSeparator);
296 | if (jmodsPath != null && ! jmodsPath.isEmpty()) {
297 | getLog().debug("Including jmods from local path: " + jmodsPath);
298 | modulePath = jmodsPath + File.pathSeparator + modulePath;
299 | }
300 | commandArguments.add(modulePath);
301 |
302 | commandArguments.add(" --add-modules");
303 | if (moduleDescriptor != null) {
304 | commandArguments.add(" " + moduleDescriptor.name());
305 | } else {
306 | throw new MojoExecutionException("jlink requires a module descriptor");
307 | }
308 | }
309 |
310 | commandArguments.add(" --output");
311 | File image = new File(builddir, jlinkImageName);
312 | getLog().debug("image output: " + image.getAbsolutePath());
313 | if (image.exists()) {
314 | try {
315 | Files.walk(image.toPath())
316 | .sorted(Comparator.reverseOrder())
317 | .map(Path::toFile)
318 | .forEach(File::delete);
319 | } catch (IOException e) {
320 | throw new MojoExecutionException("Image can't be removed " + image.getAbsolutePath(), e);
321 | }
322 | }
323 | commandArguments.add(" " + image.getAbsolutePath());
324 |
325 | if (stripDebug) {
326 | commandArguments.add(" --strip-debug");
327 | }
328 | if (stripJavaDebugAttributes) {
329 | commandArguments.add(" --strip-java-debug-attributes");
330 | }
331 | if (bindServices) {
332 | commandArguments.add(" --bind-services");
333 | }
334 | if (ignoreSigningInformation) {
335 | commandArguments.add(" --ignore-signing-information");
336 | }
337 | if (compress != null) {
338 | commandArguments.add(" --compress");
339 | if (compress < 0 || compress > 2) {
340 | throw new MojoFailureException("The given compress parameters " + compress + " is not in the valid value range from 0..2");
341 | }
342 | commandArguments.add(" " + compress);
343 | }
344 | if (noHeaderFiles) {
345 | commandArguments.add(" --no-header-files");
346 | }
347 | if (noManPages) {
348 | commandArguments.add(" --no-man-pages");
349 | }
350 | if (jlinkVerbose) {
351 | commandArguments.add(" --verbose");
352 | }
353 |
354 | if (launcher != null && ! launcher.isEmpty()) {
355 | commandArguments.add(" --launcher");
356 | String moduleMainClass;
357 | if (mainClass.contains("/")) {
358 | moduleMainClass = mainClass;
359 | } else {
360 | moduleMainClass = moduleDescriptor.name() + "/" + mainClass;
361 | }
362 | commandArguments.add(" " + launcher + "=" + moduleMainClass);
363 | }
364 | return commandArguments;
365 | }
366 |
367 | private File createZipArchiveFromImage() throws MojoExecutionException {
368 | File imageArchive = new File(builddir, jlinkImageName);
369 | zipArchiver.addDirectory(imageArchive);
370 |
371 | File resultArchive = new File(builddir, jlinkZipName + ".zip");
372 | zipArchiver.setDestFile(resultArchive);
373 | try {
374 | zipArchiver.createArchive();
375 | } catch (ArchiverException | IOException e) {
376 | throw new MojoExecutionException(e.getMessage(), e);
377 | }
378 | return resultArchive;
379 | }
380 |
381 | private boolean isJLinkVersion13orHigher(String jlinkExePath) {
382 | CommandLine versionCommandLine = new CommandLine(jlinkExePath)
383 | .addArgument("--version");
384 |
385 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
386 | int resultCode = -1;
387 |
388 | try {
389 | resultCode = executeCommandLine(new DefaultExecutor(), versionCommandLine, null, baos, System.err);
390 | } catch (IOException e) {
391 | if (getLog().isDebugEnabled()) {
392 | getLog().error("Error getting JLink version", e);
393 | }
394 | }
395 |
396 | if (resultCode != 0) {
397 | getLog().error("Unable to get JLink version");
398 | getLog().error("Result of " + versionCommandLine + " execution is: '" + resultCode + "'");
399 | return false;
400 | }
401 |
402 | String versionStr = new String(baos.toByteArray());
403 | return JLINK_VERSION_PATTERN.matcher(versionStr).lookingAt();
404 | }
405 |
406 | // for tests
407 | }
408 |
--------------------------------------------------------------------------------
/src/main/java/org/openjfx/JavaFXBaseMojo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019, 2020, Gluon
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.openjfx;
18 |
19 | import static org.openjfx.model.RuntimePathOption.CLASSPATH;
20 | import static org.openjfx.model.RuntimePathOption.MODULEPATH;
21 |
22 | import java.io.BufferedOutputStream;
23 | import java.io.File;
24 | import java.io.FileOutputStream;
25 | import java.io.IOException;
26 | import java.io.OutputStream;
27 | import java.net.MalformedURLException;
28 | import java.net.URL;
29 | import java.net.URLClassLoader;
30 | import java.nio.file.Files;
31 | import java.nio.file.Path;
32 | import java.nio.file.Paths;
33 | import java.util.ArrayList;
34 | import java.util.Arrays;
35 | import java.util.Collection;
36 | import java.util.HashMap;
37 | import java.util.LinkedHashMap;
38 | import java.util.List;
39 | import java.util.Map;
40 | import java.util.Objects;
41 | import java.util.Properties;
42 | import java.util.stream.Collectors;
43 | import java.util.stream.Stream;
44 |
45 | import org.apache.commons.exec.CommandLine;
46 | import org.apache.commons.exec.ExecuteException;
47 | import org.apache.commons.exec.ExecuteResultHandler;
48 | import org.apache.commons.exec.Executor;
49 | import org.apache.commons.exec.OS;
50 | import org.apache.commons.exec.ProcessDestroyer;
51 | import org.apache.commons.exec.PumpStreamHandler;
52 | import org.apache.commons.exec.ShutdownHookProcessDestroyer;
53 | import org.apache.maven.artifact.Artifact;
54 | import org.apache.maven.artifact.DependencyResolutionRequiredException;
55 | import org.apache.maven.execution.MavenSession;
56 | import org.apache.maven.plugin.AbstractMojo;
57 | import org.apache.maven.plugin.BuildPluginManager;
58 | import org.apache.maven.plugin.MojoExecutionException;
59 | import org.apache.maven.plugins.annotations.Component;
60 | import org.apache.maven.plugins.annotations.Parameter;
61 | import org.apache.maven.project.MavenProject;
62 | import org.apache.maven.toolchain.Toolchain;
63 | import org.apache.maven.toolchain.ToolchainManager;
64 | import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
65 | import org.codehaus.plexus.languages.java.jpms.LocationManager;
66 | import org.codehaus.plexus.languages.java.jpms.ModuleNameSource;
67 | import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;
68 | import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
69 | import org.codehaus.plexus.util.StringUtils;
70 | import org.codehaus.plexus.util.cli.CommandLineUtils;
71 | import org.openjfx.model.RuntimePathOption;
72 |
73 | abstract class JavaFXBaseMojo extends AbstractMojo {
74 |
75 | private static final String JAVAFX_APPLICATION_CLASS_NAME = "javafx.application.Application";
76 | static final String JAVAFX_PREFIX = "javafx";
77 |
78 | @Parameter(defaultValue = "${project}", readonly = true)
79 | MavenProject project;
80 |
81 | @Parameter(defaultValue = "${session}", readonly = true)
82 | private MavenSession session;
83 |
84 | @Component
85 | private BuildPluginManager pluginManager;
86 |
87 | @Component
88 | private LocationManager locationManager;
89 |
90 | @Parameter(property = "javafx.mainClass", required = true)
91 | String mainClass;
92 |
93 | /**
94 | * Skip the execution.
95 | */
96 | @Parameter(property = "javafx.skip", defaultValue = "false")
97 | boolean skip;
98 |
99 | @Parameter(readonly = true, required = true, defaultValue = "${basedir}")
100 | File basedir;
101 |
102 | @Parameter(readonly = true, required = true, defaultValue = "${project.build.directory}")
103 | File builddir;
104 |
105 | /**
106 | * Type of {@link RuntimePathOption} to run the application.
107 | */
108 | @Parameter(property = "javafx.runtimePathOption")
109 | RuntimePathOption runtimePathOption;
110 |
111 | /**
112 | * The current working directory. Optional. If not specified, basedir will be used.
113 | */
114 | @Parameter(property = "javafx.workingDirectory")
115 | File workingDirectory;
116 |
117 | @Parameter(defaultValue = "${project.compileClasspathElements}", readonly = true, required = true)
118 | private List compilePath;
119 |
120 | @Parameter(property = "javafx.outputFile")
121 | File outputFile;
122 |
123 | /**
124 | * If set to true the child process executes asynchronously and build execution continues in parallel.
125 | */
126 | @Parameter(property = "javafx.async", defaultValue = "false")
127 | private boolean async;
128 |
129 | /**
130 | * If set to true, the asynchronous child process is destroyed upon JVM shutdown. If set to false, asynchronous
131 | * child process continues execution after JVM shutdown. Applies only to asynchronous processes; ignored for
132 | * synchronous processes.
133 | */
134 | @Parameter(property = "javafx.asyncDestroyOnShutdown", defaultValue = "true")
135 | private boolean asyncDestroyOnShutdown;
136 |
137 | /**
138 | *
139 | * A list of vm options passed to the {@code executable}.
140 | *
141 | *
142 | */
143 | @Parameter
144 | List> options;
145 |
146 | /**
147 | * Arguments separated by space for the executed program. For example: "-j 20"
148 | */
149 | @Parameter(property = "javafx.args")
150 | String commandlineArgs;
151 |
152 | /**
153 | * If set to true, it will include the dependencies that
154 | * generate path exceptions in the classpath. Default is false.
155 | */
156 | @Parameter(property = "javafx.includePathExceptionsInClasspath", defaultValue = "false")
157 | private boolean includePathExceptionsInClasspath;
158 |
159 | /**
160 | *
161 | */
162 | @Component
163 | private ToolchainManager toolchainManager;
164 |
165 | List classpathElements;
166 | List modulepathElements;
167 | Map pathElements;
168 | JavaModuleDescriptor moduleDescriptor;
169 | private ProcessDestroyer processDestroyer;
170 |
171 | static boolean isMavenUsingJava8() {
172 | return System.getProperty("java.version").startsWith("1.8");
173 | }
174 |
175 | static boolean isTargetUsingJava8(CommandLine commandLine) {
176 | final String java = commandLine.getExecutable();
177 | return java != null && Files.exists(Paths.get(java).resolve("../../jre/lib/rt.jar").normalize());
178 | }
179 |
180 | void preparePaths(Path jdkHome) throws MojoExecutionException {
181 | if (project == null) {
182 | return;
183 | }
184 |
185 | String outputDirectory = project.getBuild().getOutputDirectory();
186 | if (outputDirectory == null || outputDirectory.isEmpty()) {
187 | throw new MojoExecutionException("Error: Output directory doesn't exist");
188 | }
189 |
190 | File[] classes = new File(outputDirectory).listFiles();
191 | if (classes == null || classes.length == 0) {
192 | throw new MojoExecutionException("Error: Output directory is empty");
193 | }
194 |
195 | File moduleDescriptorPath = Stream
196 | .of(classes)
197 | .filter(file -> "module-info.class".equals(file.getName()))
198 | .findFirst()
199 | .orElse(null);
200 |
201 | modulepathElements = new ArrayList<>(compilePath.size());
202 | classpathElements = new ArrayList<>(compilePath.size());
203 | pathElements = new LinkedHashMap<>(compilePath.size());
204 |
205 | try {
206 | Collection dependencyArtifacts = getCompileClasspathElements(project);
207 | getLog().debug("Total dependencyArtifacts: " + dependencyArtifacts.size());
208 | ResolvePathsRequest fileResolvePathsRequest = ResolvePathsRequest.ofFiles(dependencyArtifacts);
209 |
210 | getLog().debug("module descriptor path: " + moduleDescriptorPath);
211 | if (moduleDescriptorPath != null) {
212 | fileResolvePathsRequest.setMainModuleDescriptor(moduleDescriptorPath);
213 | }
214 | if (jdkHome != null) {
215 | fileResolvePathsRequest.setJdkHome(jdkHome.toFile());
216 | }
217 | ResolvePathsResult resolvePathsResult = locationManager.resolvePaths(fileResolvePathsRequest);
218 | resolvePathsResult.getPathElements().forEach((key, value) -> pathElements.put(key.getPath(), value));
219 |
220 | if (!resolvePathsResult.getPathExceptions().isEmpty()) {
221 | getLog().warn("There are " + resolvePathsResult.getPathExceptions().size() + " pathException(s). The related dependencies will be ignored.");
222 | resolvePathsResult.getPathExceptions().forEach((key, value) -> {
223 | String message = "Dependency: " + key;
224 | if (value != null) {
225 | message += "\n - exception: " + value.getMessage();
226 | Throwable t = value.getCause();
227 | if (t != null) {
228 | message += "\n - cause: " + t.getMessage();
229 | }
230 | }
231 | getLog().warn(message);
232 | });
233 | }
234 |
235 | if (runtimePathOption == MODULEPATH && moduleDescriptorPath == null) {
236 | throw new MojoExecutionException("module-info.java file is required for MODULEPATH runtimePathOption");
237 | }
238 |
239 | if (moduleDescriptorPath != null) {
240 | if (!resolvePathsResult.getPathExceptions().isEmpty() && !isMavenUsingJava8()) {
241 | // for each path exception, show a warning to plugin user...
242 | for (Map.Entry pathException : resolvePathsResult.getPathExceptions().entrySet()) {
243 | Throwable cause = pathException.getValue();
244 | while (cause.getCause() != null) {
245 | cause = cause.getCause();
246 | }
247 | String fileName = pathException.getKey().getName();
248 | getLog().warn("Can't extract module name from " + fileName + ": " + cause.getMessage());
249 | }
250 | // ...if includePathExceptionsInClasspath is NOT enabled; provide configuration hint to plugin user
251 | if (!includePathExceptionsInClasspath) {
252 | getLog().warn("Some dependencies encountered issues while attempting to be resolved as modules" +
253 | " and will not be included in the classpath; you can change this behavior via the " +
254 | " 'includePathExceptionsInClasspath' configuration parameter.");
255 | }
256 | }
257 | moduleDescriptor = createModuleDescriptor(resolvePathsResult);
258 | for (Map.Entry entry : resolvePathsResult.getModulepathElements().entrySet()) {
259 | if (ModuleNameSource.FILENAME.equals(entry.getValue())) {
260 | final String message = "Required filename-based automodules detected. "
261 | + "Please don't publish this project to a public artifact repository!";
262 |
263 | if (moduleDescriptor != null && moduleDescriptor.exports().isEmpty()) {
264 | // application
265 | getLog().info(message);
266 | } else {
267 | // library
268 | getLog().warn(message);
269 | }
270 | break;
271 | }
272 | }
273 | resolvePathsResult.getClasspathElements().forEach(file -> classpathElements.add(file.getPath()));
274 | resolvePathsResult.getModulepathElements().keySet().forEach(file -> modulepathElements.add(file.getPath()));
275 |
276 | if (includePathExceptionsInClasspath) {
277 | resolvePathsResult.getPathExceptions().keySet()
278 | .forEach(file -> classpathElements.add(file.getPath()));
279 | }
280 | } else {
281 | // non-modular projects
282 | pathElements.forEach((k, v) -> {
283 | if (v != null && v.name() != null && v.name().startsWith(JAVAFX_PREFIX)) {
284 | // only JavaFX jars are required in the module-path
285 | modulepathElements.add(k);
286 | } else {
287 | classpathElements.add(k);
288 | }
289 | });
290 | }
291 | } catch (Exception e) {
292 | getLog().warn(e.getMessage());
293 | }
294 |
295 | if (runtimePathOption == MODULEPATH) {
296 | getLog().debug(runtimePathOption + " runtimePathOption set by user. Moving all jars to modulepath.");
297 | modulepathElements.addAll(classpathElements);
298 | classpathElements.clear();
299 | } else if (runtimePathOption == CLASSPATH) {
300 | getLog().debug(runtimePathOption + " runtimePathOption set by user. Moving all jars to classpath.");
301 | classpathElements.addAll(modulepathElements);
302 | modulepathElements.clear();
303 | if (mainClass.contains("/")) {
304 | getLog().warn("Module name found in with runtimePathOption set as CLASSPATH. Module name will be ignored.");
305 | }
306 | if (doesExtendFXApplication(createMainClassString(mainClass, moduleDescriptor, runtimePathOption))) {
307 | throw new MojoExecutionException("Launcher class is required. Main-class cannot extend Application when running JavaFX application on CLASSPATH");
308 | }
309 | }
310 |
311 | getLog().debug("Classpath:" + classpathElements.size());
312 | classpathElements.forEach(s -> getLog().debug(" " + s));
313 |
314 | getLog().debug("Modulepath: " + modulepathElements.size());
315 | modulepathElements.forEach(s -> getLog().debug(" " + s));
316 |
317 | getLog().debug("pathElements: " + pathElements.size());
318 | pathElements.forEach((k, v) -> getLog().debug(" " + k + " :: " + (v != null && v.name() != null ? v.name() : v)));
319 | }
320 |
321 | private JavaModuleDescriptor createModuleDescriptor(ResolvePathsResult resolvePathsResult) throws MojoExecutionException {
322 | if (runtimePathOption == CLASSPATH) {
323 | getLog().info(CLASSPATH + " runtimePathOption set by user. module-info.java will be ignored.");
324 | return null;
325 | }
326 | return resolvePathsResult.getMainModuleDescriptor();
327 | }
328 |
329 | private List getCompileClasspathElements(MavenProject project) {
330 | List list = new ArrayList<>();
331 | list.add(new File(project.getBuild().getOutputDirectory()));
332 |
333 | // include systemPath dependencies
334 | list.addAll(project.getDependencies().stream()
335 | .filter(d -> d.getSystemPath() != null && ! d.getSystemPath().isEmpty())
336 | .map(d -> new File(d.getSystemPath()))
337 | .collect(Collectors.toList()));
338 |
339 | list.addAll(project.getArtifacts().stream()
340 | .sorted((a1, a2) -> {
341 | int compare = a1.compareTo(a2);
342 | if (compare == 0) {
343 | // give precedence to classifiers
344 | return a1.hasClassifier() ? 1 : (a2.hasClassifier() ? -1 : 0);
345 | }
346 | return compare;
347 | })
348 | .map(Artifact::getFile)
349 | .collect(Collectors.toList()));
350 | return list.stream()
351 | .distinct()
352 | .collect(Collectors.toList());
353 | }
354 |
355 | void handleWorkingDirectory() throws MojoExecutionException {
356 | if (workingDirectory == null) {
357 | workingDirectory = basedir;
358 | }
359 |
360 | if (!workingDirectory.exists()) {
361 | getLog().debug("Making working directory '" + workingDirectory.getAbsolutePath() + "'.");
362 | if (!workingDirectory.mkdirs()) {
363 | throw new MojoExecutionException("Could not make working directory: '" + workingDirectory.getAbsolutePath() + "'");
364 | }
365 | }
366 | }
367 |
368 | Map handleSystemEnvVariables() {
369 | Map enviro = new HashMap<>();
370 | try {
371 | Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
372 | for (Map.Entry, ?> entry : systemEnvVars.entrySet()) {
373 | enviro.put((String) entry.getKey(), (String) entry.getValue());
374 | }
375 | } catch (IOException x) {
376 | getLog().error("Could not assign default system environment variables.", x);
377 | }
378 |
379 | return enviro;
380 | }
381 |
382 | CommandLine getExecutablePath(String executable, Map enviro, File dir) {
383 | File execFile = new File(executable);
384 | String exec = null;
385 | if (execFile.isFile()) {
386 | getLog().debug("'executable' parameter is set to " + executable);
387 | exec = execFile.getAbsolutePath();
388 | }
389 |
390 | if (exec == null && toolchainManager != null) {
391 | Toolchain toolchain = toolchainManager.getToolchainFromBuildContext("jdk", session);
392 | if (toolchain != null) {
393 | getLog().info("Toolchain in javafx-maven-plugin " + toolchain);
394 | exec = toolchain.findTool("java");
395 | getLog().debug("Tool in toolchain in javafx-maven-plugin " + exec);
396 | }
397 | }
398 |
399 | if (exec == null) {
400 | String javaHomeFromEnv = getJavaHomeEnv(enviro);
401 | if (javaHomeFromEnv != null && ! javaHomeFromEnv.isEmpty()) {
402 | exec = findExecutable(executable, Arrays.asList(javaHomeFromEnv.concat(File.separator).concat("bin")));
403 | }
404 | }
405 |
406 | if (exec == null && OS.isFamilyWindows()) {
407 | List paths = this.getExecutablePaths(enviro);
408 | paths.add(0, dir.getAbsolutePath());
409 | exec = findExecutable(executable, paths);
410 | }
411 |
412 | if (exec == null) {
413 | exec = executable;
414 | }
415 |
416 | CommandLine toRet;
417 | if (OS.isFamilyWindows() && !hasNativeExtension(exec) && hasExecutableExtension(exec) ) {
418 | // run the windows batch script in isolation and exit at the end
419 | final String comSpec = System.getenv( "ComSpec" );
420 | toRet = new CommandLine( comSpec == null ? "cmd" : comSpec );
421 | toRet.addArgument( "/c" );
422 | toRet.addArgument( exec );
423 | } else {
424 | toRet = new CommandLine(exec);
425 | }
426 | getLog().debug("Executable " + toRet.toString());
427 | return toRet;
428 | }
429 |
430 | int executeCommandLine(Executor exec, CommandLine commandLine, Map enviro,
431 | OutputStream out, OutputStream err) throws ExecuteException, IOException {
432 | // note: don't use BufferedOutputStream here since it delays the outputs MEXEC-138
433 | PumpStreamHandler psh = new PumpStreamHandler(out, err, System.in);
434 | return executeCommandLine(exec, commandLine, enviro, psh);
435 | }
436 |
437 | int executeCommandLine(Executor exec, CommandLine commandLine, Map enviro,
438 | FileOutputStream outputFile) throws ExecuteException, IOException {
439 | BufferedOutputStream bos = new BufferedOutputStream(outputFile);
440 | PumpStreamHandler psh = new PumpStreamHandler(bos);
441 | return executeCommandLine(exec, commandLine, enviro, psh);
442 | }
443 |
444 | /**
445 | * Returns the path of the parent directory.
446 | * At the given depth if the path has no parent, the method returns null.
447 | * @param path Path against which the parent needs to be evaluated
448 | * @param depth Depth of the path relative to parent
449 | * @return Path to the parent, if exists. Null, otherwise.
450 | */
451 | static Path getParent(Path path, int depth) {
452 | if (path == null || !Files.exists(path) || depth > path.getNameCount()) {
453 | return null;
454 | }
455 | return path.getRoot().resolve(path.subpath(0, path.getNameCount() - depth));
456 | }
457 |
458 | String createMainClassString(String mainClass, JavaModuleDescriptor moduleDescriptor, RuntimePathOption runtimePathOption) {
459 | Objects.requireNonNull(mainClass, "Main class cannot be null");
460 | if (runtimePathOption == CLASSPATH) {
461 | if (mainClass.contains("/")) {
462 | return mainClass.substring(mainClass.indexOf("/") + 1);
463 | }
464 | return mainClass;
465 | }
466 | if (moduleDescriptor != null && !mainClass.contains("/")) {
467 | getLog().warn("Module name not found in . Module name will be assumed from module-info.java");
468 | return moduleDescriptor.name() + "/" + mainClass;
469 | }
470 | return mainClass;
471 | }
472 |
473 | private static String findExecutable(final String executable, final List paths) {
474 | File f = null;
475 | search: for (final String path : paths) {
476 | f = new File(path, executable);
477 | if (!OS.isFamilyWindows() && f.isFile()) {
478 | break;
479 | } else {
480 | for (final String extension : getExecutableExtensions()) {
481 | f = new File(path, executable + extension);
482 | if (f.isFile()) {
483 | break search;
484 | }
485 | }
486 | }
487 | }
488 |
489 | if (f == null || !f.exists()) {
490 | return null;
491 | }
492 | return f.getAbsolutePath();
493 | }
494 |
495 | private static boolean hasNativeExtension(final String exec) {
496 | final String lowerCase = exec.toLowerCase();
497 | return lowerCase.endsWith(".exe") || lowerCase.endsWith(".com");
498 | }
499 |
500 | private static boolean hasExecutableExtension(final String exec) {
501 | final String lowerCase = exec.toLowerCase();
502 | for (final String ext : getExecutableExtensions()) {
503 | if (lowerCase.endsWith(ext)) {
504 | return true;
505 | }
506 | }
507 | return false;
508 | }
509 |
510 | private static List getExecutableExtensions() {
511 | final String pathExt = System.getenv("PATHEXT");
512 | return pathExt == null ? Arrays.asList(".bat", ".cmd")
513 | : Arrays.asList(StringUtils.split(pathExt.toLowerCase(), File.pathSeparator));
514 | }
515 |
516 | private List getExecutablePaths(Map enviro) {
517 | List paths = new ArrayList<>();
518 | paths.add("");
519 |
520 | String path = enviro.get("PATH");
521 | if (path != null) {
522 | paths.addAll(Arrays.asList(StringUtils.split(path, File.pathSeparator)));
523 | }
524 | return paths;
525 | }
526 |
527 | private String getJavaHomeEnv(Map enviro) {
528 | String javahome = enviro.get("JAVA_HOME");
529 | if (javahome == null || javahome.isEmpty()) {
530 | return null;
531 | }
532 |
533 | int pathStartIndex = javahome.charAt(0) == '"' ? 1 : 0;
534 | int pathEndIndex = javahome.charAt(javahome.length() - 1) == '"' ? javahome.length() - 1 : javahome.length();
535 |
536 | return javahome.substring(pathStartIndex, pathEndIndex);
537 | }
538 |
539 | private int executeCommandLine(Executor exec, final CommandLine commandLine, Map enviro,
540 | final PumpStreamHandler psh) throws ExecuteException, IOException {
541 | exec.setStreamHandler(psh);
542 |
543 | int result;
544 | try {
545 | psh.start();
546 | if (async) {
547 | if (asyncDestroyOnShutdown) {
548 | exec.setProcessDestroyer(getProcessDestroyer());
549 | }
550 |
551 | exec.execute(commandLine, enviro, new ExecuteResultHandler() {
552 | public void onProcessFailed(ExecuteException e) {
553 | getLog().error("Async process failed for: " + commandLine, e);
554 | }
555 |
556 | public void onProcessComplete(int exitValue) {
557 | getLog().debug("Async process complete, exit value = " + exitValue + " for: " + commandLine);
558 | try {
559 | psh.stop();
560 | } catch (IOException e) {
561 | getLog().error("Error stopping async process stream handler for: " + commandLine, e);
562 | }
563 | }
564 | });
565 | result = 0;
566 | } else {
567 | result = exec.execute(commandLine, enviro);
568 | }
569 | } finally {
570 | if (!async) {
571 | psh.stop();
572 | }
573 | }
574 | return result;
575 | }
576 |
577 | private ProcessDestroyer getProcessDestroyer() {
578 | if (processDestroyer == null) {
579 | processDestroyer = new ShutdownHookProcessDestroyer();
580 | }
581 | return processDestroyer;
582 | }
583 |
584 | private boolean doesExtendFXApplication(String mainClass) {
585 | boolean fxApplication = false;
586 | try {
587 | List pathUrls = new ArrayList<>();
588 | for (String path : project.getCompileClasspathElements()) {
589 | pathUrls.add(new File(path).toURI().toURL());
590 | }
591 | URL[] urls = pathUrls.toArray(new URL[0]);
592 | URLClassLoader classLoader = new URLClassLoader(urls, JavaFXBaseMojo.class.getClassLoader());
593 | Class> clazz = Class.forName(mainClass, false, classLoader);
594 | fxApplication = doesExtendFXApplication(clazz);
595 | getLog().debug("Main Class " + clazz.toString() + " extends Application: " + fxApplication);
596 | } catch (NoClassDefFoundError | ClassNotFoundException | DependencyResolutionRequiredException | MalformedURLException e) {
597 | getLog().debug(e);
598 | }
599 | return fxApplication;
600 | }
601 |
602 | private static boolean doesExtendFXApplication(Class> mainClass) {
603 | for (Class> sc = mainClass.getSuperclass(); sc != null;
604 | sc = sc.getSuperclass()) {
605 | if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) {
606 | return true;
607 | }
608 | }
609 | return false;
610 | }
611 | }
612 |
--------------------------------------------------------------------------------