windowsbashmavenenvironment-variablesparameter-passing

How can I specify Maven command line arguments system wide on Windows?


How can I alter the default arguments passed to the mvn executables executed on a Windows system without modifying each installation or manually creating alternative startup scripts (or otherwise "hard-coding" my preferences in a non-portable way)? To be clear, I am not open to external methods like a console alias or using a shortcut or filesystem link. I am asking for a Maven-specific portable way to describe the default startup arguments for all Maven executions on my Windows machine.


Solution

  • Much like MAVEN_OPTS allows you to pass command line arguments to the JVM used to run Maven, MAVEN_CMD_LINE_ARGS is an environmental variable that allows you to pass command line arguments directly to Maven itself (but only on Windows). Unlike modifying files and settings at the level of an individual Maven installation or project (via settings.xml or maven.config for instance), MAVEN_CMD_LINE_ARGS allows you to pass command line arguments to any Maven installation spun up using the provided startup scripts (on Windows the scripts are "mvn.cmd" and "mvnDebug.cmd").

    As an example, on my Windows development computer I have set MVN_CMD_LINE_ARGS to

    --show-version --global-settings %M2_CONF%\settings.xml --global-toolchains %M2_CONF%\toolchains.xml --define settings.security=%M2_CONF%\security-settings.xml --fail-fast --update-snapshots --strict-checksums --define maven.artifact.threads=8.

    Instead of having to place an alias or create my own startup script I simply can use this builtin method to achieve my goal of standardized cross-installation configurations. With MVN_CMD_LINE_ARGS set to that value, executing

    mvn

    on the command line is actually like executing

    mvn --show-version --global-settings %M2_CONF%\settings.xml --global-toolchains %M2_CONF%\toolchains.xml --define settings.security=%M2_CONF%\security-settings.xml --fail-fast --update-snapshots --strict-checksums --define maven.artifact.threads=8.

    Now, notice I said that I believe this only functions on Windows. While the Linux Bash scripts include an export of a variable called MVN_CMD_LINE_ARGS they do not then pass it to the mvn executable as an argument. The result of this is that on both Windows and Linux you can use MVN_CMD_LINE_ARGS from after the execution to determine what arguments were used to call mvn, but only on Windows can you use MVN_CMD_LINE_ARGS to instruct which arguments will be used to call mvn. From what I can tell though, this behavior may not be intended, so I would not recommend using this in a critical manner. It seems there is a project specific way to configure the mvn execution arguements via placing them into a "./.mvn/maven.config" file within the projects directory structure.