59

How to set VM arguments for Jetty run from maven-jetty-plugin?

For example, I need to pass -Xmx arguments to Jetty run by the mvn jetty:run command.

3
  • 1
    I know that question is really old, but did you find a solution ? (I mean not add an argument for the whole JVM but only for jetty) I think Cargo would do the trick, but I don't feel like adding another thing to my project... Commented May 21, 2012 at 10:02
  • Yes, use -DargLine... see my answer below for full example Commented May 25, 2015 at 15:07
  • -DargLine doesn't work with jetty:run, it works with jetty:run-forked Commented Jan 1, 2018 at 4:45

11 Answers 11

64

The enviroment variable MAVEN_OPTS is the answer. The string content of MAVEN_OPTS is passed to JVM (java.exe).

  • Linux: in shell type export MAVEN_OPTS=....
  • Windows: in shell (cmd.exe) type set MAVEN_OPTS=...

For example: on Windows set MAVEN_OPTS="-Xmx1024m" sets the heap size of the Maven process to 1024mb.

Update (01.04.2013): Pass it directly to Jetty.

Matthew Farwell (please upvote his answer to give him credit) comes with the solution of using a forked JVM process to run Jetty which is a new feature of the Jetty plugin. This is a better solution as the former runs inside same JVM process as Maven (thus shares memory).

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't that set it for the entire JVM run? Is there a way to get it to just affect the jetty that it starts?
yes it is - see (and vote up) the new answer from Matthew Farwell - there is new feature of jetty plugin to run jetty in separated jvm with own arguments
49

With more recent versions of the maven-jetty-plugin, you can use mvn:run-forked. The option jvmArgs will allow you to set -Xmx etc.

For more information, see: jetty:run-forked : Running an unassembled webapp in a separate jvm.

I think the original issue was Starting Jetty in separate JVM.

1 Comment

Solutions where Jetty assembles the WAR file prior to running it, are a problem in my case. When assembling the WAR file, the plugin appears to skip the 'filtering steps' I need when copying and packaging Web resources. The jetty:deploy-war goal simply uses a pre-built WAR file, but does not support the jvmArgs parameter. :-( Setting the MAVEN_OPTS env variable works, but I have to set it back for other apps I build. It would be nice if I could set the JVM options via the plugin config, keeping that config with the app that needs it.
14

It seems like your current approach is correct - when running jetty through maven, jetty is a thread inside the maven process. So increasing maven's heap will increase jetty's heap.

How are you setting MAVEN_OPTS?

One example I found looks like this: MAVEN_OPTS='-Xmx256m -Xms10m' mvn clean jetty:run

Note that MAVEN_OPTS is an environment variable here, and not passed to the JVM (who wouldn't know what to do with it).

Comments

11

To specify vm arguments via the command line (as originally asked) you can do the following:

mvn clean install -DargLine="-Xmx1524m" 

4 Comments

Under which circumstances does this work? Is it specific to jetty? For me, it didn't work, while setting MAVEN_OPT did.
mvn -DargLine="-Xmx1524m" clean install <----- This should work fine.
@cafebabe1991 Happy for you to comment on my post, and highlight errors, if there are any. Don't think it is necessary to modify my answer, since, it is my answer!! No idea why Stackoverflow would allow modification of someones answer because you don't like the order of where the -D flag goes. To my knowledge, the order is irrelevant. Reverted back to original answer.
@ChrisRitchie If I remember correctly the -D<param> was not being picked up by the maven unless it is in the order I specified. I agree that I shouldn't have edited your answer.
4

The <jvmArgs> param mentioned here : Maven jetty plugin didn't work for me .

Maven version : Apache Maven 3.0.3

Jetty Maven plugin version : jetty-maven-plugin:8.1.10.v20130312

This worked :

MAVEN_OPTS='-Xmx4096m -Xms4096m' export MAVEN_OPTS mvn jetty:run & 

Comments

3

On Linux/Unix

export MAVEN_OPTS="-Xmx256m" && mvn clean install jetty:run 

will do the trick

1 Comment

This leaves the variable set for later executions, which might not be intended.
2

The plugin allows you to specify jvmArgs like this:

<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <jvmArgs>-Xmx1024</jvmArgs> <scanIntervalSeconds>10</scanIntervalSeconds> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8080</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <webAppConfig> <jettyEnvXml>jetty-env.xml</jettyEnvXml> </webAppConfig> </configuration> <executions> <execution> <phase>test</phase> <goals> <goal>run-exploded</goal> </goals> </execution> </executions> </plugin> 

3 Comments

did not work for me, I tried: <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.5.v20120716</version> <configuration> <jvmArgs>-Xmx2048M -XX:MaxPermSize=2048m</jvmArgs> <webAppSourceDirectory>${project.basedir}/war</webAppSourceDirectory> <webAppConfig> <descriptor>${project.basedir}/war/WEB-INF/web.xml</descriptor> </webAppConfig> </configuration> </plugin>
@davidjnelson You need to use jetty:run-forked goal. See: Matthew Farwell's answer above: stackoverflow.com/a/13388081/19501.
But how to have jetty run as jetty:run-forked when the command being typed in is mvn clean install ?
2

As referenced in Configuring Apache Maven, discussing the MAVEN_OPTS environment variable referenced in other answers, you can also control project configuration with files in the .mvn directory.

For VM arguments in particular, you can add a .mvn/jvm.config file containing the associated parameters:

Starting with Maven 3.3.1+ you can define JVM configuration via ${maven.projectBasedir}/.mvn/jvm.config file which means you can define the options for your build on a per project base. This file will become part of your project and will be checked in along with your project. So no need anymore for MAVEN_OPTS, .mavenrc files.

Comments

1

you can use to pass -Xmx argument like;

<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version></version> <configuration> <jvmArgs>-Xmx -Xms -XX:PermSize= -XX:MaxPermSize= -XX:+HeapDumpOnOutOfMemoryError</jvmArgs> <scanIntervalSeconds>1</scanIntervalSeconds> <stopKey>stop-jetty</stopKey> <stopPort>9999</stopPort> <systemProperties> <systemProperty> <name>jetty.port</name> <value>9090</value> </systemProperty> <systemProperty> <name>spring.profiles.active</name> <value></value> </systemProperty> </systemProperties> <webApp> <contextPath>/</contextPath> </webApp> </configuration> </plugin> 

Comments

0

There is no way using the commandline. But you could copy the mvn.cmd / mvn.sh to mvnhp.cmd and change the line

%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %CLASSWORLDS_JAR% "-Dclassworlds.conf=%M2_HOME%\bin\m2.conf" "-Dmaven.home=%M2_HOME%" "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS% 

To

%MAVEN_JAVA_EXE% -Xmx1024m %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %CLASSWORLDS_JAR% "-Dclassworlds.conf=%M2_HOME%\bin\m2.conf" "-Dmaven.home=%M2_HOME%" "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS% 

Comments

0

For maven jetty plugin version 11 or above, you may set the deployMode to FORK, the default is EMBED.

<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>11.0.23</version> <configuration> <webApp> <contextPath>/${project.artifactId}</contextPath> </webApp> <classesDirectory>${project.build.directory}/classes</classesDirectory> <systemProperties> <jetty.port>8082</jetty.port> <spring.profiles.active>development</spring.profiles.active> </systemProperties> <deployMode>FORK</deployMode> <jvmArgs> -Xms512m -Xmx512m -XX:+UseZGC -XX:+ZGenerational -XX:+ScavengeBeforeFullGC --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/sun.security.ssl=ALL-UNNAMED --add-exports java.base/sun.security.util=ALL-UNNAMED </jvmArgs> </configuration> </plugin> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.