Search code examples
javamavencommand-line-interfacegitlab-ci

Make maven ignore / continue after "Unable to parse command line options"


While reworking a CI pipeline script, i stumbled into

$ mvn $MAVEN_CLI_OPTS clean compile
Unable to parse command line options: Unrecognized option: --no-transfer-progress

when trying to add --no-transfer-progress to the default setup. This happens in a shell executor runner, of course due to an outdated Maven version. Although i have already found a workaround via SDKMAN! to get recent maven versions onto such runners, i would like my CI script to be as generalized as possible, i.e. not failing for project pipelines that are missing an .sdkmanrc file and simply want to rely on the default maven version on that runner (which i don't have control over).

So, is there a way to make maven ignore unrecognized options and continue execution nevertheless?


Solution

  • As all the feedback so far indicated that this is not possible, i integrated a bash script into the CI pipeline that progressively enhances the CLI options depending on the currently available maven version:

    INSTALLED_MAVEN=$(mvn -version | grep -o "Apache Maven [\.0-9]*" | cut -d" " -f 3)
    
    # --no-transfer-progress was introduced in Version 3.6.1
    if [ $(printf "${INSTALLED_MAVEN}\n3.6.1" | sort -V | head -1) = "3.6.1" ]; then
      MAVEN_CLI_OPTS="$MAVEN_CLI_OPTS --no-transfer-progress"
    fi
    
    # --color was introduced in version 3.8.2
    if [ $(printf "${INSTALLED_MAVEN}\n3.8.2" | sort -V | head -1) = "3.8.2" ]; then
      MAVEN_CLI_OPTS="$MAVEN_CLI_OPTS --color=always"
    fi
    

    Clearly has room for improvement i.e. by making the version comparison algorithm DRY, but so far this works like a charm for us.

    Nevertheless always keen to learn if there's a smarter way to achieve this.