Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -280,17 +281,63 @@ protected List<String> getBuildArgs() throws MojoExecutionException {
}

static List<String> processBuildArgs(List<String> buildArgs) {
String osName = normalizeOs(System.getProperty("os.name"));
return processPlatformBuildArgs(osName, buildArgs);
}

static List<String> processPlatformBuildArgs(String osName, List<String> buildArgs) {
var result = new ArrayList<String>();
for (String buildArg : buildArgs) {
if (buildArg.startsWith("\\Q") || buildArg.startsWith("-H:ConfigurationFileDirectories")) {
result.add(buildArg);
} else {
result.addAll(Arrays.asList(buildArg.split("\\s+", 2)));
buildArgForPlatform(osName, buildArg)
.ifPresent(arg ->
result.addAll(Arrays.asList(arg.split("\\s+", 2))));
}
}
return result;
}

static Optional<String> buildArgForPlatform(String osName, String buildArg) {
if (!buildArg.startsWith("$") || !buildArg.contains(":")) {
return Optional.of(buildArg);
}
String argPlatform = buildArg.substring(1, buildArg.indexOf(':'));
if (!matchPlatform(osName, argPlatform)) {
// suppress this build argument as it is for another platform
return Optional.empty();
}
// trim off the platform prefix
String trimmedArg = buildArg.substring(buildArg.indexOf(':') + 1);
return Optional.of(trimmedArg);
}

private static boolean matchPlatform(String osName, String argPlatform) {
return osName.equals(normalize(argPlatform));
}

private static String normalize(String value) {
if (value == null) {
return "";
}
return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");
}

static String normalizeOs(String value) {
value = normalize(value);
if (value.startsWith("linux")) {
return "linux";
}
if (value.startsWith("mac") || value.startsWith("osx")) {
return "mac";
}
if (value.startsWith("windows")) {
return "windows";
}
return value;
}

protected Path processSupportedArtifacts(Artifact artifact) throws MojoExecutionException {
return processArtifact(artifact, "jar", "test-jar", "war");
}
Expand Down Expand Up @@ -545,8 +592,8 @@ protected void maybeAddGeneratedResourcesConfig(List<String> into) {
}

protected void maybeAddDynamicAccessMetadataToClasspath() {
if (Files.exists(Path.of(outputDirectory.getPath() ,"dynamic-access-metadata.json"))) {
imageClasspath.add(Path.of(outputDirectory.getPath() ,"dynamic-access-metadata.json"));
if (Files.exists(Path.of(outputDirectory.getPath(), "dynamic-access-metadata.json"))) {
imageClasspath.add(Path.of(outputDirectory.getPath(), "dynamic-access-metadata.json"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,62 @@ class AbstractNativeImageMojoTest extends Specification {
"-H:ConfigurationFileDirectories=C:\\Users\\Lahoucine EL ADDALI\\Downloads\\4.5.0.0_kubernetes_kubernetes-demo-java-maven\\api\\target\\native\\generated\\generateResourceConfig"
]
}

void "platform specific build args - leading"() {
given:
def buildArgs = [
'$linux:--GC=G1',
'$linux:-R:MaxGCPauseMillis=50',
'--foo',
'$macos:--myMacOption'
]

when:
def processedArgs = AbstractNativeImageMojo.processPlatformBuildArgs('linux', buildArgs)

then:
processedArgs == [
'--GC=G1',
'-R:MaxGCPauseMillis=50',
'--foo'
]
}

void "platform specific build args - trailing"() {
given:
def buildArgs = [
'$linux:--GC=G1',
'$linux:-R:MaxGCPauseMillis=50',
'--foo',
'$mac:--myMacOption'
]

when:
def processedArgs = AbstractNativeImageMojo.processPlatformBuildArgs('mac', buildArgs)

then:
processedArgs == [
'--foo',
'--myMacOption'
]
}

void "normalizeOs"() {
when:
def resultOs = AbstractNativeImageMojo.normalizeOs(inputOs)

then:
resultOs == expectedOs

where:
inputOs || expectedOs
'Some@Unknown' || 'someunknown'
'Some@_|Unknown019' || 'someunknown019'
'mac' || 'mac'
'MacOs' || 'mac'
'linux' || 'linux'
'LinuxAnyFoo' || 'linux'
'windows' || 'windows'
'WindowsAnyFoo' || 'windows'
}
}