Find Java options used by Maven - java

How can I find which Java options (Xmx, Xms, Xss, etc) are being used by Maven?
I've found out that a way to set them is via the environment MAVEN_OPTS. Now I want a way to be assured it's getting the right settings.
EDIT: I believe it´s different to this question as I don´t want to see the value of an environment variable. I rather want to see which settings are actually being used by Maven, whether it comes from a env var, settings.xml or other means.
EDIT2: I'm also interested in other ways of setting Java options for a Maven build

You can set Java options for Maven in different points and level (globally or via plugins configuration):
Plugin configuration: just for Compilation
Using the Maven Compiler Plugin configuration for compiling application code and test code, you can set the required Xmx, Xms, Xss options via the compileArgs configuration entry, available for both compile and testCompile goals. An official example is available here and on other SO answers like this one.
An example is also shown below on the third point.
Plugin configuration: just for Tests execution
Using the Maven Surefire Plugin configuration for tests executions, you can set the required Java options to be used at runtime via the argLine configuration entry of the test goal. An official example is available here.
An example is also shown below on the third point.
Plugin configuration: via Properties (and profiles)
You can combine the two options above (in case of common Java options) as a property value to pass to both compileArgs and argLine configuration entry or have different properties per configuration (according to your needs).
<property>
<jvm.options>-Xmx256M</jvm.options>
</property>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<compilerArgs>
<arg>${jvm.options}</arg>
</compilerArgs>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${jvm.options}</argLine>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
Using properties gives you also an two extra advantages (on top of centralization): you can use profiles then to personalize it based on different desired behaviours (and example in this SO answer) and you can override them via command line as well, like:
mvn clean install -Djvm.options=-Xmx512
Global/Project configuration: Options file
Since Maven 3.3.1, you can specify your options in a .mvn/jvm.config file per project. It's an alternative to MAVEN_OPTS and a more narrowed scope (per project). However, since it sis a new Maven feature, people should be aware of it, otherwise troubleshooting and maintenance may be impacted.
Global/Env configuration: MAVEN_OPTS
Maven well-known environment variable to set global execution options, however applied to all Maven builds sharing that environment (i.e. per logged user).
When running Maven using the -X option (debug enabled), you will have the following output as part of your build:
[DEBUG] properties used {java.vendor=Oracle Corporation, ... , env.MAVEN_OPTS=-Xmx256M, ...
Update
After all, the executed mvn command is an OS script. Having a look at it in Windows, I found the possibility of using the MAVEN_BATCH_ECHO option which, if enabled (value set to on), will echo any command executed by the script and as such also the invocation of the java command, where you can see if your options (the MAVEN_OPTS) are picked up correctly together with the full list of parameters passed to it.
Here is an execution I tested on Windows:
set MAVEN_BATCH_ECHO=on
set MAVEN_OPTS=-Xmx256M
mvn compile > output.txt
NOTE: the output.txt will contain quite a lot of text, providing build output and additional echos executions. As part of it, it provided:
>"path_to_\jdk1.7\bin\java.exe" -Xmx256M -classpath "path_to\apache-maven-3.1.1\bin\..\boot\plexus-classworlds-2.5.1.jar" "-Dclassworlds.conf=path_to\apache-maven-3.1.1\bin\..\bin\m2.conf" "-Dmaven.home=path_to\apache-maven-3.1.1\bin\.." org.codehaus.plexus.classworlds.launcher.Launcher compile
As you can see, the -Xmx256M option was picked up correctly. If the maven script for other OS doesn't provide this option, then you can simply add it to the script (a simple echo before the java execution, showing the command, would also be enough).
You can find the maven script in your maven installation folder, under the bin subfolder.
Update2
Furthermore, since Maven is a Java tool after all and as you can see from its script it invokes the java command, you could see all the available options as suggested in this SO answer by slightly changing the Maven script and use the jinfo command which should really give you the answer according to this other SO answer.

Maybe it helps to start Maven with verbose debug output (-debug, I think?). Otherwise just do a ps aux | grep java and check the arguments of the process (assuming *nix).

Related

How can I set Dfile.encoding=utf-8 when building maven project?

System.out.println(Charset.defaultCharset().displayName()); prints ISO-8859-1, so I want to set the defaultCharset to UTF-8.
When tried with java -Dfile.encoding=utf-8 -jar XXX.jar, I checked that default charset is set to UTF-8.
But is there a way to set Dfile.encoding=utf-8 option at maven packaging level, so that I can just run java -jar XXX.jar?
What I tried:
mvn clean package -Dfile.encoding=utf-8
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>-Dfile.encoding=utf-8</argLine>
</configuration>
</plugin>
MAVEN_OPTS="-Dfile.encoding=utf-8" mvn clean package
I prefer controlling with pom.xml file.
You can define JDK_JAVA_OPTIONS environment variable as -Dfile.encoding=utf-8 to avoid typing it every time. See the java command documentation for details.
In OpenJDK 18 UTF-8 will be default for all operating systems: JEP 400.
The question is how to pass JVM args for Maven build before runtime.
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, create .mvn/jvm.config in your project root with content:
-Dfile.encoding=utf-8
See the reference: https://maven.apache.org/configure.html#mvn-jvm-config-file

How to run Maven Checkstyle plugin on modified files only

I am running Maven Checkstyle plugin through pre-commit githook written in python (I think that the same question applies to running it directly from terminal).
The command is:
mvn checkstyle:checkstyle
However, I would like to run Maven Checkstyle only on files modified by git. For instance, I could run it once for each file. If I want to run it on a specific file, I may define the following pattern (I am not sure why do I need the pattern of stars and dashes in front):
mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/*File.java
However, I am not able to pass file path and file name, for instance:
mvn checkstyle:checkstyle -Dcheckstyle.includes=src/main/java/File.java
Or, following the above mentioned pattern:
mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/*src/main/java/File.java
I have tried many other combinations as well, but nothing works. I found this issue which is about the same thing, so I am wondering if someone has found a solution to this.
I have also read:
How to run maven checkstyle plugin on incremental code only
Is there a maven-git-checkstyle plugin that runs checkstyle goal on git staged files alone?
How to check style arbitrary list of java files from command line?
but they do not solve my problem.
There is an example here: github project of maven-checkstyle-plugin
Put a placeholder in pom and pass the parameter by maven command line.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>${checkstyle.includes}</includes>
</configuration>
Command line: mvn checkstyle:check "-Dcheckstyle.includes=**/File.java"

Arquillian test with custom jre (using Maven dependency)

I have a project which use a specific JRE : that contains some additionnal lib and custo java.security entries... and i want to run test in other machine which i don't have access to update JRE with my specific configuration.
What I want to do :
get JRE distribution and put it in target folder : using Maven dependency
with Maven plugin : update this JRE with my specific configuration
set $JAVA_HOME with my custom JRE in target using arquillian.xml configuration file
It is possible to do that with Arquillian ?
Have you any other suggestion ?
Thank you !
For the first both steps you can use the Maven dependency plugin.
For the integration test you can use the failsafe plugin, there is a parameter called jvm. There is no need to override the jvm with the arquillian.xml.
Option to specify the jvm (or path to the java executable) to use with the forking options. For the default, the jvm will be a new instance of the same VM as the one used to run Maven. JVM settings are not inherited from MAVEN_OPTS.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<jvm>path to jvm or java executable</jvm>
</configuration>
</plugin>

Maven m2e enforces its own compiler settings - disable

After starting Eclipse, Mven seems to set the compiler settings to 1.5 and forget all the other global code style settings to ensure a higher code quality.
Is there some way to disable this feature? Or can I specify all compiler and code style checks in my POM?
It is very annoying because Ecplise can't run the app because of not allowed override annotations for interfaces. The tick in Java compiler -> Enable project specific settings is always set after a restart.
You can set the compiler source and target (byte-code) versions in your pom.
See http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
Code style checks can be configured in the pom as part of the maven reports, see http://maven.apache.org/plugins/maven-checkstyle-plugin/
but I'm not sure whether the integration will pick these up.
The simplest way is to add to your POM
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
See default maven compiler setting for another solutions.
If you don't want the m2e eclipse plugin actively messing with your project settings, use the maven-eclipse-plugin's eclipse goal to generate your eclipse settings.
It'll generate your eclipse settings based off of what you have in your pom, so you'll still need to set the maven compiler settings in your pom if you don't want to set them every time you regenerate your eclipse project files when you update your pom.
If you take a look at the detailed configuration for that plugin, there are instructions for how to generate various pieces of eclipse metadata.

Maven Doesn't Seem to Pass Arguments to JVM

I'm trying to compile an Android application that depends on OrmLite and whatever I do I still receive the OutOfMemory error.
The reason why I think Maven doesn't pass any arguments regarding the heap size is I can only see the following in its output:
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar /Users/mikhail.borozdin/android-sdk-macosx/platform-tools/lib/dx.jar --dex --output=/Users/mikhail.borozdin/Documents/workspace/AndroidOrmLiteTest/target/classes.dex
How did I try to pass arguments to JVM?
export MAVEN_OPTS=-Xmx2048m
mvn -X install -DargLine="-Xmx2048m"
Setting for my plug-in in pom.xml
Setting for my plug-in in pom.xml
Neither of these worked.
If the project you're building is using the android-maven-plugin, you want to set the dex mojo configuration in the maven pom.xml file, eg:
<dex>
<jvmArguments>
<jvmArgument>-Xms256m</jvmArgument>
<jvmArgument>-Xmx512m</jvmArgument>
</jvmArguments>
</dex>
Here's an example from a complete pom.xml file:
https://github.com/rtyley/agit/blob/a014c970/pom.xml#L116-121
Note that the specific xml for dex configuration evolved a fair bit in the run-up to release of android-maven-plugin version 3.0.0 (which has lots of other crucial improvements) - I would recommend ensuring that you are using this version and that the configuration is definitely using the <dex> node.
The android-maven-plugin will use a 1GB heap by default in versions post-v3.0.0 (not yet released at the time of writing).

Categories

Resources