Maven OutOfMemoryError and MAVEN_OPTS


Running out of memory is something that happens frequently during a Maven build. Here is some sample output from running mvn package:

[INFO] Processing PDFs
[INFO] Processing PDF: /Users/tom/Books/jenkins-the-definitive-guide-book/hudsonbook-pdf/src/main/resources/hudson-book-cover.pdf
[INFO] Processing PDF: /Users/tom/Books/jenkins-the-definitive-guide-book/hudsonbook-pdf/target/book.pdf
[ERROR] Java heap space -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/OutOfMemoryError
mbp:jenkins-the-definitive-guide-book tom$


There is a big fat Java heap space error at the top of the error message.
To fix this, simply increase the available memory in the MAVEN_OPTS environment variable. In other words, before you run the mvn package command, set or modify MAVEN_OPTS with the -Xmx... switch. The following code snippet shows how to do this in Linux/Unix and Windows.

# Unix
export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=128m"


# Windows
set MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=128m