Skip to content

Commit

Permalink
Add jlinkOptions configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinayagarwal committed Oct 16, 2020
1 parent 5e92071 commit 87f080b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ The same command line options for `jlink` can be set:
- `bindServices`: Adds the option to bind services. Values: false (default) or true
- `ignoreSigningInformation`: Adds the option to ignore signing information. Values: false (default) or true
- `jlinkVerbose`: Adds the verbose option. Values: false (default) or true
- `jlinkOptions`: A list of options passed to the jlink executable.
- `launcher`: Adds a launcher script with the given name.
- If `options` are defined, these will be passed to the launcher script as vm options.
- If `commandLineArgs` are defined, these will be passed to the launcher script as command line arguments.
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/openjfx/JavaFXBaseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -456,6 +457,42 @@ String createMainClassString(String mainClass, JavaModuleDescriptor moduleDescri
return mainClass;
}

List<String> splitComplexArgumentString(String argumentString) {
char[] strArr = argumentString.trim().toCharArray();

List<String> splitedArgs = new ArrayList<>();
StringBuilder sb = new StringBuilder();

char expectedSeparator = ' ';
for (int i = 0; i < strArr.length; i++) {
char item = strArr[i];

if (item == expectedSeparator
|| (expectedSeparator == ' ' && Pattern.matches("\\s", String.valueOf(item))) ) {

if (expectedSeparator == '"' || expectedSeparator == '\'') {
sb.append(item);
expectedSeparator = ' ';
} else if (expectedSeparator == ' ' && sb.length() > 0) {
splitedArgs.add(sb.toString());
sb.delete(0, sb.length());
}
} else {
if (expectedSeparator == ' ' && (item == '"' || item == '\'')) {
expectedSeparator = item;
}

sb.append(item);
}

if (i == strArr.length - 1 && sb.length() > 0) {
splitedArgs.add(sb.toString());
}
}

return splitedArgs;
}

private static String findExecutable(final String executable, final List<String> paths) {
File f = null;
search: for (final String path : paths) {
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/org/openjfx/JavaFXJLinkMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Gluon
* Copyright 2019, 2020, Gluon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,6 +43,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -151,6 +152,12 @@ public class JavaFXJLinkMojo extends JavaFXBaseMojo {
@Component(role = Archiver.class, hint = "zip")
private ZipArchiver zipArchiver;

/**
* A list of options passed to the jlink {@code executable}.
*/
@Parameter
List<?> jlinkOptions;

public void execute() throws MojoExecutionException {
if (skip) {
getLog().info( "skipping execute as per configuration" );
Expand All @@ -166,7 +173,7 @@ public void execute() throws MojoExecutionException {
}

handleWorkingDirectory();

Map<String, String> enviro = handleSystemEnvVariables();
CommandLine commandLine = getExecutablePath(jlinkExecutable, enviro, workingDirectory);

Expand All @@ -182,7 +189,6 @@ public void execute() throws MojoExecutionException {
}

try {

List<String> commandArguments = createCommandArguments();
String[] args = commandArguments.toArray(new String[commandArguments.size()]);
commandLine.addArguments(args, false);
Expand Down Expand Up @@ -290,6 +296,17 @@ private void patchLauncherScript(String launcherFilename) throws IOException {
private List<String> createCommandArguments() throws MojoExecutionException, MojoFailureException {
List<String> commandArguments = new ArrayList<>();
preparePaths(getParent(Paths.get(jlinkExecutable), 2));

if (jlinkOptions != null) {
jlinkOptions.stream()
.filter(Objects::nonNull)
.filter(String.class::isInstance)
.map(String.class::cast)
.map(this::splitComplexArgumentString)
.flatMap(Collection::stream)
.forEach(commandArguments::add);
}

if (modulepathElements != null && !modulepathElements.isEmpty()) {
commandArguments.add(" --module-path");
String modulePath = StringUtils.join(modulepathElements.iterator(), File.pathSeparator);
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/org/openjfx/JavaFXRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,42 +181,6 @@ private String createAddModulesString(JavaModuleDescriptor moduleDescriptor, Map
return moduleDescriptor.name();
}

private List<String> splitComplexArgumentString(String argumentString) {
char[] strArr = argumentString.trim().toCharArray();

List<String> splitedArgs = new ArrayList<>();
StringBuilder sb = new StringBuilder();

char expectedSeparator = ' ';
for (int i = 0; i < strArr.length; i++) {
char item = strArr[i];

if (item == expectedSeparator
|| (expectedSeparator == ' ' && Pattern.matches("\\s", String.valueOf(item))) ) {

if (expectedSeparator == '"' || expectedSeparator == '\'') {
sb.append(item);
expectedSeparator = ' ';
} else if (expectedSeparator == ' ' && sb.length() > 0) {
splitedArgs.add(sb.toString());
sb.delete(0, sb.length());
}
} else {
if (expectedSeparator == ' ' && (item == '"' || item == '\'')) {
expectedSeparator = item;
}

sb.append(item);
}

if (i == strArr.length - 1 && sb.length() > 0) {
splitedArgs.add(sb.toString());
}
}

return splitedArgs;
}

// for tests

void setExecutable(String executable) {
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/org/openjfx/JavaFXBaseMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2019, 2020, Gluon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openjfx;

import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
Expand Down Expand Up @@ -97,6 +112,37 @@ public void invalidPathWithDepthTest() {
Assert.assertNull(JavaFXBaseMojo.getParent(Paths.get("/some-invalid-path"), 2));
}

@Test
public void testSplitComplexArgumentString() {
String option = "param1 " +
"param2 \n " +
"param3\n" +
"param4=\"/path/to/my file.log\" " +
"'var\"foo var\"foo' " +
"'var\"foo' " +
"'var\"foo' " +
"\"foo'var foo'var\" " +
"\"foo'var\" " +
"\"foo'var\"";

String expected = "START," +
"param1," +
"param2," +
"param3," +
"param4=\"/path/to/my file.log\"," +
"'var\"foo var\"foo'," +
"'var\"foo'," +
"'var\"foo'," +
"\"foo'var foo'var\"," +
"\"foo'var\"," +
"\"foo'var\"";

String splitedOption = new JavaFXRunMojo().splitComplexArgumentStringAdapter(option)
.stream().reduce("START", (s1, s2) -> s1 + "," + s2);

Assert.assertEquals(expected, splitedOption);
}

@AfterClass
public static void destroy() throws IOException {
Files.walk(path.getParent())
Expand Down

0 comments on commit 87f080b

Please sign in to comment.