Home > Net >  Passing vm arguments to exec-maven-plugin
Passing vm arguments to exec-maven-plugin

Time:07-20

Guys I have upgraded my project to Java 17 that's why I need these lines to VM to my program works without error.

=--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED

My project is working but I have another sub project which I use for testing purposes.It is run on JavaFX and it need previous arguments to work correctly. I have tried adding as CommandLineArgs and arguments but still arguments are not passing through to vm and giving errors.

    <profile>
        <id>smoke</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy jars for tests</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <includeProjectDependencies>false</includeProjectDependencies>
                                <includePluginDependencies>false</includePluginDependencies>
                                <stripVersion>true</stripVersion>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <classpathScope>test</classpathScope>
                        <includeProjectDependencies>false</includeProjectDependencies>
                        <includePluginDependencies>false</includePluginDependencies>
                        <executable>${env.HOME}/java/bin/java.exe</executable>
                        <workingDirectory>${project.build.directory}</workingDirectory>

                        <arguments>
                            <argument>-javaagent:${env.HOME}/wildfly/standalone/lib/ext/agent.jar</argument>
                            <argument>-classpath</argument>
            <argument>lib/*;classes</argument>
                            <argument>-DJBOSS_HOME=${JBOSS_HOME}</argument>
                            <argument>-DJAVA_HOME=${env.HOME}/java</argument>
                            <argument>-Duser.home=${env.USERPROFILE}</argument>
                            <argument>-Djava.library.path=${JBOSS_HOME}/bin</argument>

                            <argument>-DTESTING_FROM_COMMANDLINE=true</argument>
                            <mainClass>com.test.GenerateSmoke</mainClass>
                  <commandlineArgs>-Dexec.args=--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</commandlineArgs>

                        </arguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

If you have any idea please let me know.Thanks

CodePudding user response:

I had a similar issue when running tests with the maven-surefire-plugin. In my case I added it like this into the pom:

<configuration>
                <argLine>${argLine} --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>

Maybe it is the same in your case.

CodePudding user response:

The plugins configuration options are documented here: https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html

commandlineArgs or -Dexec.args are both an alternative option to override the entire executed command line you specify. These can not be used together with (other) arguments. So you either define the entire java command line to execute as:

  • arguments in the pom, or
  • commandlineArgs in the pom, or
  • exec.args as an environment variable (so mvn exec:exec -Dexec.args=...)

You should be able to add the arguments to your current plugin configuration like so:

<configuration>
    <arguments>
        <argument>...</argument>
        <argument>--add-opens</argument>
        <argument>java.base/java.lang=ALL-UNNAMED</argument>
        <argument>--add-opens</argument>
        <argument>java.base/java.util=ALL-UNNAMED</argument>
        <argument>...</argument>
    </arguments>
</configuration>

Depending on what you run these arguments might need to be inserted in a specific place, so the order of these arguments matters. That is where you can experiment with substituting the ... in my example with your current arguments.

As a test i've ran a java -jar with these options, and that works. I've enabled the maven argument -X for debug logging.

[DEBUG] Executing command line: [some\path\jdk-17.0.2\bin\java.exe, --add-opens, java.base/java.lang=ALL-UNNAMED, --add-opens, java.base/java.util=ALL-UNNAMED, -jar, my.jar]

And if i make an "accidental" typo in my add-opens we see that these arguments are indeed parsed by the java process:

[DEBUG] Executing command line: [some\path\jdk-17.0.2\bin\java.exe, --add-typo, java.base/java.lang=ALL-UNNAMED, --add-opens, java.base/java.util=ALL-UNNAMED, -jar, my.jar]
Unrecognized option: --add-typo
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
  • Related