karvai
karvai

Reputation: 1757

How to pass VM Options in Maven

I need the following vm options to be passed into maven.

-Dvertx.disableFileCPResolving=true  
-Dlog4j.configurationFile=log4j2.xml 
-Dlogger-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory 
-DAPP_ID=9757632

At present I am passing them via the IDE (Intellij) and everything is working fine. (See screenshot)

VM Options

But how can I add them as part of the Maven project, possibly via POM file or a resource file?

Tried the following in my POM file based on the suggestion in this page but it doesn't work.

https://stackoverflow.com/a/39751176/9401029

<properties>
    <vertx.disableFileCPResolving>true</vertx.disableFileCPResolving>
    <log4j.configurationFile>log4j2.xml </log4j.configurationFile>
    <vertx.logger-delegate-factory-class-name>io.vertx.core.logging.Log4j2LogDelegateFactory</vertx.logger-delegate-factory-class-name>
    <APP_ID>9757632</APP_ID>

</properties>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArgs>
                    <arg>${vertx.disableFileCPResolving}</arg>
                    <arg>${log4j.configurationFile}</arg>
                    <arg>${vertx.logger-delegate-factory-class-name}</arg>
                    <arg>${APP_ID}</arg>
                </compilerArgs>
            </configuration>
        </plugin>

Saw another example using the file JVMARGS (no extension) which holds following values.

-Dvertx.disableFileCPResolving=true  
-Dlog4j.configurationFile=log4j2.xml 
-Dlogger-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory 
-DAPP_ID=9757632

Placed this file at the root level and within resource folder. That doesn't work either.

Please advice how to pass these values into my maven project, preferably via POM file. Thank you.

Upvotes: 3

Views: 12949

Answers (1)

Kevin Boone
Kevin Boone

Reputation: 4307

Maven has a gazillion ways to run Java code, through various plug-ins -- and they don't all use the same methods to pass JVM command-line switches to the running program.

For example, the exec plugin [1] respects the environment variable MAVEN_OPTS. This is a system environment variable, not a Java environment variable.

So on Linux, to run a program under Maven with a specific -Xmx setting I might do

$ MAVEN_OPTS="-Xmx2000m" mvn exec:java 

But note that you'd need to set MAVEN_OPTS in a way that is appropriate for your platform. For an IDE on Windows, you might need to use the Control Panel to do this. Or your IDE might provide a way to set the environment variable when it runs Maven -- that depends on the IDE.

So the short answer, I guess, is that you need to find exactly which execution plug-in you're using in Maven, and look at the documentation for that plug-in to see how to configure it to use the appropriate JVM settings.

In the end, though, you're probably not going to be running your application under Maven in the long term. You'll need to find a way to specific JVM settings independently of Maven -- in a script, or batch file, for example.

[1] https://www.mojohaus.org/exec-maven-plugin/usage.html

Upvotes: 4

Related Questions