6

I need to use maven with a settings file in a specific location, normally you can give MAVEN_OPTS env variable but they are passed to JVM so the following will yield:

$ MAVEN_OPTS="-s /settings.xml"
$ mvn clean
Unrecognized option: -s
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

I searched a lot but found two keys, org.apache.maven.user-settings and org.apache.maven.global-settings which is explained here but it seemed it was working with Maven 2 only. Aliasing mvn to mvn -s /settings.xml would probably work but I do not like it.

Mustafa
  • 10,013
  • 10
  • 70
  • 116
  • https://stackoverflow.com/a/27503199/2970947 – Elliott Frisch Dec 31 '18 at 17:55
  • 1
    Can you provide details on why you need to use a separate settings.xml? – Thorbjørn Ravn Andersen Jan 01 '19 at 00:53
  • First it would be useful to know why you need a separate settings.xml as already @ThorbjørnRavnAndersen asked. Furthermore you might can use `.mvn/maven.config`? See release notes https://maven.apache.org/docs/3.3.1/release-notes.html – khmarbaise Jan 01 '19 at 11:04
  • @khmarbaise I have used Maven for very long and I have missed the announcement of this functionality (which is a major step forward - I hail the new architects). Is it well described in the official documentation too? – Thorbjørn Ravn Andersen Jan 01 '19 at 12:15
  • @ThorbjørnRavnAndersen Can you explain what you define as the official documentation? Do you have an expectation where it should be documented apart from that release notes? – khmarbaise Jan 01 '19 at 15:00
  • @khmarbaise I had a closer look. My intuitive expectation would be that the page that explains the command line flags thoroughly had a section of how to do this. Apparently such a page is not online. The closest is https://maven.apache.org/configure.html which has a single line explaining the exisiance of .mvn and two XML-files but without any further links. Also https://maven.apache.org/guides/mini/guide-configuring-maven.html has a section on optional configuration which only mentions settings.xml. Have I overlooked anything? – Thorbjørn Ravn Andersen Jan 01 '19 at 15:36
  • @khmarbaise I too have used maven for a long time and wanted the .mvn/maven.config functionality and I was not aware. If you have any influence in the maven doc I would suggest making this more obvious. I would also suggest that a ~/.mvn (%USERPROFILE%\.mvn for windows) would also be extremely useful. – mario Sep 15 '20 at 15:03

2 Answers2

6

From the mvn shell script:

# -----------------------------------------------------------------------------
# Apache Maven Startup Script
#
# Environment Variable Prerequisites
#
#   JAVA_HOME       Must point at your Java Development Kit installation.
#   MAVEN_OPTS      (Optional) Java runtime options used when Maven is executed.
#   MAVEN_SKIP_RC   (Optional) Flag to disable loading of mavenrc files.
# -----------------------------------------------------------------------------

so MAVEN_OPTS contains JVM arguments, not Maven arguments (which is consistent with the error message indicating the JVM doesn't like your arguments).

The actual invocation is

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

so there is nowhere to put it. I would therefore suggest that you write your own ? mvn script which in turn calls the real maven command with the arguments you like (in my experience scripts are more robust than aliases). Additionally I have recently found myself that the Java versions later than 8 have ... interesting issues... so I really need to have mvn8, mvn11 (and perhaps more) commands anyway.

Another approach that I only started using recently is the Maven wrapper (https://github.com/takari/maven-wrapper) where a ./mvnw command is placed in your project which then downloads Maven when needed. This is very useful. To get started use

mvn -N io.takari:maven:wrapper

after which ./mvnw should be directly usable instead of mvn. The interesting part here is that the generated Maven command looks like

exec "$JAVACMD" \
  $MAVEN_OPTS \
  -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
  "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
  ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"

and MAVEN_CONFIG is not set earlier in the script. So for mvnw you can set MAVEN_CONFIG to your "-s /settings.xml" string.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
5

Maven 4

The MAVEN_ARGS environment variable is supported and can be used.

Maven 3.9.0

The MAVEN_ARGS environment variable is implemented in Maven 3, starting with version 3.9.0 and can be used if you have sufficiently new version of maven.

Older Maven 3 versions

There was a feature request MNG-5824: Support MAVEN_ARGS environment variable as a way of supplying default command line arguments. This was closed unimplemented with a suggestion to use the .mvn/maven.config in project directory

user7610
  • 25,267
  • 15
  • 124
  • 150