Search code examples
mavenmaven-3

How to handle space in argument in MAVEN_OPTS from the command line?


I have some issues passing arguments to my mvn command through MAVEN_OPTS when there's a space, and specifically a javaagent parameters. I have also followed this recommendation to double quote the part after the semicolon but with no luck.

The command I am using is this:

$ export MAVEN_OPTS='-javaagent:"/Users/teras/Library/Application Support/CrossMobile/Plugins/cmxray.jar"'
$ echo $MAVEN_OPTS
-javaagent:"/Users/teras/Library/Application Support/CrossMobile/Plugins/cmxray.jar"
$ mvn -P desktop,run install -B -e
Error opening zip file or JAR manifest missing : "/Users/teras/Library/Application
Error occurred during initialization of VM
agent library failed to init: instrument

Unfortunately this doesn't seem to work. I am on OSX, using bash, if this helps.

Any idea what I am missing?


Solution

  • I'm afraid, you can't do that easily. If you look at the mvn script you can see it does not put $MAVEN_OPTS in quotes.

    So unless there is a trick I'm not aware of, there is no way to escape the space.

    You can fix that locally by editing your mvn script. If you used homebrew to install Maven you can find it in /usr/local/Cellar/maven/<VERSION>/libexec/bin/mvn. Make a copy of the script (so you can restore it if something goes wrong) and then find those lines at the end of the file (may be a bit different depending on the version):

    exec "$JAVACMD" \
      $MAVEN_OPTS \
      $MAVEN_DEBUG_OPTS \
      -classpath "${CLASSWORLDS_JAR}" \
      "-Dclassworlds.conf=${MAVEN_HOME}/bin/m2.conf" \
      "-Dmaven.home=${MAVEN_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
      ${CLASSWORLDS_LAUNCHER} "$@"
    

    and add quotes around the variables so it looks like this:

    exec "$JAVACMD" \
      "$MAVEN_OPTS" \
      "$MAVEN_DEBUG_OPTS" \
      -classpath "${CLASSWORLDS_JAR}" \
      "-Dclassworlds.conf=${MAVEN_HOME}/bin/m2.conf" \
      "-Dmaven.home=${MAVEN_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
      ${CLASSWORLDS_LAUNCHER} "$@"
    

    I am not sure if this is a Maven bug or it's this way on purpose. It looks like the issue was reported already but there seems to be some pushback from the maven team.

    UPDATE

    After reading through the comments of the above issue, I found this one which explains why this may not be a good solution after all:

    If you quote $MAVEN_OPTS when used here, the java executable will see it as 1 parameter, not multiple ones, so if you have MAVEN_OPTS="-Xmx256m -Dparam=\"with space\"", then java will understand a Xmx value of 256m -Dparam="with space"...

    If you don't quote it, then every space separated token (even if it was escaped when declaring MAVEN_OPTS) will be considered a separate argument, so -Dparam="with space" will be understood as trying to launch the space" main class with the -Dparam="with system property...