[MJLINK-41] add support for --add-options argument (JDK14+).
- IT "cli-options/add-options" will now look for parameters in build.log.
- removed a superfluous %n.
- A MojoFailureException is now being thrown if the user does not meet the JDK14 requirements (neither toolchain nor JAVA_HOME).
- warn user if toolchain could not be checked.
- added IT MJLINK-41_addoptions-11 which checks for the error message being thrown if java requirements were not met.
diff --git a/src/it/projects/MJLINK-41_addoptions-11/invoker.properties b/src/it/projects/MJLINK-41_addoptions-11/invoker.properties
new file mode 100644
index 0000000..72a5045
--- /dev/null
+++ b/src/it/projects/MJLINK-41_addoptions-11/invoker.properties
@@ -0,0 +1,21 @@
+# suppress inspection "UnusedProperty" for whole file
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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.
+invoker.java.version = 9-13
+invoker.goals = clean package
+invoker.buildResult = failure
+# must not use toolchain
diff --git a/src/it/projects/MJLINK-41_addoptions-11/pom.xml b/src/it/projects/MJLINK-41_addoptions-11/pom.xml
new file mode 100644
index 0000000..39987a6
--- /dev/null
+++ b/src/it/projects/MJLINK-41_addoptions-11/pom.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.plugins.it</groupId>
+ <artifactId>MJLINK-41.not.14</artifactId>
+ <version>101.0</version>
+ <packaging>jlink</packaging>
+ <name>Maven</name>
+ <url>http://maven.apache.org</url>
+ <description>Test JLink CLI Options --add-options using java 11 (which must fail without toolchain).</description>
+ <properties>
+ <maven.compiler.release>11</maven.compiler.release>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>localhost</groupId>
+ <artifactId>first-jar-module-info</artifactId>
+ <version>99.0</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.8.0</version>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jlink-plugin</artifactId>
+ <version>@project.version@</version>
+ <extensions>true</extensions>
+ <configuration>
+ <addOptions>
+ <addOption>-Xmx128m</addOption>
+ <addOption>--enable-preview</addOption>
+ <addOption>-Dvar=value</addOption>
+ </addOptions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/src/it/projects/MJLINK-41_addoptions-11/src/main/configs/config.test b/src/it/projects/MJLINK-41_addoptions-11/src/main/configs/config.test
new file mode 100644
index 0000000..405149a
--- /dev/null
+++ b/src/it/projects/MJLINK-41_addoptions-11/src/main/configs/config.test
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+Test configuration file which should be located in config/config.test in
+resulting jmod file.
\ No newline at end of file
diff --git a/src/it/projects/MJLINK-41_addoptions-11/verify.groovy b/src/it/projects/MJLINK-41_addoptions-11/verify.groovy
new file mode 100644
index 0000000..b545e93
--- /dev/null
+++ b/src/it/projects/MJLINK-41_addoptions-11/verify.groovy
@@ -0,0 +1,30 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+import java.io.*
+import java.util.*
+import java.util.jar.*
+import org.codehaus.plexus.util.*
+
+String buildlog = new File( basedir, "build.log").text
+assert buildlog.contains( "org.apache.maven.plugin.MojoFailureException" )
+assert buildlog.contains( "needs at least a Java 14" )
+
+
diff --git a/src/it/projects/cli-options/add-options/invoker.properties b/src/it/projects/cli-options/add-options/invoker.properties
new file mode 100644
index 0000000..03b85d3
--- /dev/null
+++ b/src/it/projects/cli-options/add-options/invoker.properties
@@ -0,0 +1,19 @@
+# suppress inspection "UnusedProperty" for whole file
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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.
+invoker.java.version = 14+
+invoker.goals = clean package
diff --git a/src/it/projects/cli-options/add-options/pom.xml b/src/it/projects/cli-options/add-options/pom.xml
new file mode 100644
index 0000000..84d6851
--- /dev/null
+++ b/src/it/projects/cli-options/add-options/pom.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jlink-plugin-cli-options-add-options</artifactId>
+ <version>101.0</version>
+ <packaging>jlink</packaging>
+ <name>Maven</name>
+ <url>http://maven.apache.org</url>
+ <description>Test JLink CLI Options --add-options.</description>
+ <properties>
+ <maven.compiler.release>14</maven.compiler.release>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>localhost</groupId>
+ <artifactId>first-jar-module-info</artifactId>
+ <version>99.0</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.8.0</version>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jlink-plugin</artifactId>
+ <version>@project.version@</version>
+ <extensions>true</extensions>
+ <configuration>
+ <addOptions>
+ <addOption>-Xmx128m</addOption>
+ <addOption>--enable-preview</addOption>
+ <addOption>-Dvar=value</addOption>
+ </addOptions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/src/it/projects/cli-options/add-options/src/main/configs/config.test b/src/it/projects/cli-options/add-options/src/main/configs/config.test
new file mode 100644
index 0000000..405149a
--- /dev/null
+++ b/src/it/projects/cli-options/add-options/src/main/configs/config.test
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+Test configuration file which should be located in config/config.test in
+resulting jmod file.
\ No newline at end of file
diff --git a/src/it/projects/cli-options/add-options/verify.groovy b/src/it/projects/cli-options/add-options/verify.groovy
new file mode 100644
index 0000000..8bd9385
--- /dev/null
+++ b/src/it/projects/cli-options/add-options/verify.groovy
@@ -0,0 +1,35 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+import java.io.*
+import java.util.*
+import java.util.jar.*
+import org.codehaus.plexus.util.*
+
+String buildlog = new File( basedir, "build.log").text
+assert buildlog.contains( "--add-options" )
+assert buildlog.contains( "\"-Xmx128m --enable-preview -Dvar=value\"" )
+
+File target = new File( basedir, "target" )
+assert target.isDirectory()
+
+File artifact = new File( target, "maven-jlink-plugin-cli-options-add-options-101.0.zip" )
+assert artifact.isFile()
+
diff --git a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
index 69fab8e..ae4710f 100644
--- a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
@@ -43,6 +43,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.toolchain.Toolchain;
+import org.apache.maven.toolchain.ToolchainPrivate;
import org.apache.maven.toolchain.java.DefaultJavaToolChain;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
@@ -51,6 +52,9 @@
import org.codehaus.plexus.languages.java.jpms.LocationManager;
import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;
import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
+import org.codehaus.plexus.languages.java.version.JavaVersion;
+
+import static java.util.Collections.singletonMap;
/**
* The JLink goal is intended to create a Java Run Time Image file based on
@@ -99,6 +103,27 @@
private String launcher;
/**
+ * These JVM arguments will be appended to the {@code lib/modules} file.<br>
+ * <strong>This parameter requires at least JDK 14.<br></strong>
+ *
+ * <p>The command line equivalent is: {@code jlink --add-options="…"}.</p>
+ *
+ * <p>Example:</p>
+ *
+ * <pre>
+ * <addOptions>
+ * <addOption>-Xmx256m</addOption>
+ * <addOption>--enable-preview</addOption>
+ * <addOption>-Dvar=value</addOption>
+ * </addOptions>
+ * </pre>
+ *
+ * <p>Above example will result in {@code jlink --add-options="-Xmx256m" --enable-preview -Dvar=value"}.</p>
+ */
+ @Parameter
+ private List<String> addOptions;
+
+ /**
* Limit the universe of observable modules. The following gives an example of the configuration which can be used
* in the <code>pom.xml</code> file.
*
@@ -519,6 +544,37 @@
getLog().error( message );
throw new MojoFailureException( message );
}
+
+ if ( addOptions != null && !addOptions.isEmpty() )
+ {
+ requireJdk14();
+ }
+ }
+
+ private void requireJdk14() throws MojoFailureException
+ {
+ // needs JDK 14+
+ Optional<Toolchain> optToolchain = getToolchain();
+ String java14reqMsg = "parameter 'addOptions' needs at least a Java 14 runtime or a Java 14 toolchain.";
+
+ if ( optToolchain.isPresent() )
+ {
+ Toolchain toolchain = optToolchain.orElseThrow( NoSuchElementException::new );
+ if ( !( toolchain instanceof ToolchainPrivate ) )
+ {
+ getLog().warn( "Unable to check toolchain java version." );
+ return;
+ }
+ ToolchainPrivate toolchainPrivate = (ToolchainPrivate) toolchain;
+ if ( !toolchainPrivate.matchesRequirements( singletonMap( "jdk", "14" ) ) )
+ {
+ throw new MojoFailureException( java14reqMsg );
+ }
+ }
+ else if ( !JavaVersion.JAVA_VERSION.isAtLeast( "14" ) )
+ {
+ throw new MojoFailureException( java14reqMsg );
+ }
}
/**
@@ -595,6 +651,11 @@
jlinkArgs.add( "--launcher" );
jlinkArgs.add( launcher );
}
+ if ( addOptions != null && !addOptions.isEmpty() )
+ {
+ jlinkArgs.add( "--add-options" );
+ jlinkArgs.add( String.format( "\"%s\"", String.join( " ", addOptions ) ) );
+ }
if ( disablePlugin != null )
{