Search code examples
javamavenjvmjavac

Maven and JVM relation


Though not a direct programming question, it is related to Maven and JVM

While I was going thru this link - http://maven.apache.org/archives/maven-1.x/reference/command-line.html

Update of links: https://wiki.openstack.org/wiki/Documentation/Troubleshooting

https://maven.apache.org/docs/3.0/release-notes.html

it says about MAVEN_OPTS

"Specify additional options using the MAVEN_OPTS environment variable. It is for passing parameters to the Java VM when running Maven. For example, to increase the amount of memory to 1024 Meg for the entire run of Maven, use: MAVEN_OPTS=-Xmx1024m "

Questions: How Maven is able to set the JVM properties? Why does Maven need JVM? It just build the war and JVM is needed only when u deploy the WAR to appserver.right? Does Maven starts the JVM and stop it after use? IF it stops it after use, what's the point in setting more heap size?

My understanding after research:

(1) How/Why Maven is able to set the JVM properties?

  • Maven will be able to set the JVM properties using MAVEN_OPTS. It is to pass the parameter to JVM for that run of Maven

(2) Why does Maven need JVM? Maven is just a build tool..

  • Maven has to build the WAR file, right? To build the WAR file, you need to compile the code, and sometimes run the test cases. To compile the code, you need JVM. To run test cases you need JVM. Don't know where else we need JVM when we do a mvn clean deploy. [eg of javac passing JVM parameters otherwise: javac -d build/classes -classpath ... -J-Xms256m -J-Xmx1024m java-source-files]

(3) Does Maven starts the JVM and stop it after use? IF it stops it after use, what's the point in setting more heap size?

  • Yes , Maven starts the JVM and stop after its use. JVM is needed during its run. like if maven build needs lot of memory [like if you do XML processing] then setting the heap memory using MAVEN_OPTS will be helpful.

Please clarify on my understanding especially on the second question


Solution

    1. mvn is a shell script/batch file, and it uses MAVEN_OPTS to do what you'd expect.
    2. Maven is Java. Maven tasks are written in Java.
    3. ...? Maven does a lot, and some of those things use memory. It's just a Java program. When it ends the JVM that was running it also ends.

    To further clarify point 1, this is what happens at the end of the script:

    exec "$JAVACMD" \
      $MAVEN_OPTS \
      -classpath "${M2_HOME}"/boot/plexus-classworlds-*.jar \
      "-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
      "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
      ${CLASSWORLDS_LAUNCHER} "$@"
    

    Note: why read Maven 1 docs?