Maven: Attempting to create memory dump for unit tests

maven-2

Solution

Maven (`surefire-plugin` to be precise) by default creates new JVM for running tests. `MAVEN_OPTS` variable is used by the JVM running maven itself, but not forked test JVM. To change settings of that JVM, use the following code snippet:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <!-- ... -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:\Temp</argLine>
                </configuration>
            </plugin>
            <!-- ... -->
        </plugins>
    </build>

</project>

Problem

I have difficulty creating a memory dump for my unit tests which produce out of memory errors. My MAVEN_OPTS contains the following: ``` -Xmx1024m -XX:-HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:\Temp ``` However, when I run my unit tests from the command line using "mvn install", I do not get any such memory dump when the OutOfMemoryError occurs. How do I get the memory dump? Thanks

Original source