1

My question is quite basic and I am a bit surprised I could not easily find an answer by googling or from maven-exec-plugin documentation.

I need to execute my java program in my maven project, providing both jvm arguments, system properties, and the program arguments from the command line at the invocation time.
I expect maven to provide the classpass and also the main class from pom.xml, so that I don't have to write them when I invoke my program.

As the jvm arguments, the system properties, and the program arguments differ from invocation to invocation, I can't specify them in my pom.xml.

I want to do something like this:
Invocation 1:
mvn exec:whatever -Dexec.systemProperties='p1=v1' -Dexec.jvmArguments='-XX:NewRatio=1' -Dexec.args='a b c'

Invocation 2:
mvn exec:whatever -Dexec.systemProperties='p2=v2 p3=v3' -Dexec.jvmArguments='-Xmx=10g -XX:PrintGCDetails' -Dexec.args='f h g'

(The 'exec:whatever', '-Dexec.systemProperties' etc. are just placeholders to clarify my intention and can be replaced by any other goals and arguments).

Partial answer:
I know that I can use exec:java goal and provide both program arguments and system properties like this:
mvn -Dp1=v1 -Dp2=v2 exec:java -exec:args="a b c"
This works fine, as exec:java does not fork a new process so system properties provided to maven are accessible by my program as well. But then I don't know how to pass the jvm flags, like '-XX:PringGCDetails', from the command line.

Of course, I always have an option to create a fat jar and then use regular java command to invoke it.
Yet I'd like to know whether there is a way to tweak maven-exec-plugin or similar to invoke my program without creating a fat jar, and still allow me to provide system properties, jvm, and program arguments at the time of the invocation.

Alexander
  • 2,761
  • 1
  • 28
  • 33
  • Here https://www.mojohaus.org/exec-maven-plugin/usage.html I read that any VM specific option that you want to pass to the executed class must be passed to the Maven VM using the MAVEN_OPTS environment variable – Jack Dec 20 '18 at 09:20
  • Not great for me, as I'll have to modify the global MAVEN_OPTS variable each time I want to invoke my program with say a different JVM flag. – Alexander Dec 21 '18 at 17:34
  • I don't know your level of expertise and this is just to tell that using a env variable does not always mean modifying global environment settings. For example in linux/bash I would do the following: `$ MAVEN_OPTS=-XX:PrintGCDetails mvn exec:whatever`. – Jack Dec 22 '18 at 18:16
  • Thanks Jack, seems like a good option. – Alexander Jan 04 '19 at 08:47

0 Answers0