Silencing Maven in AWS CodeBuild

Home, Bangkok, Thailand, 2019-11-18 19:11 +0700

#devops #cloud #aws

 

One of the things you will probably want to deal with early on when running Maven builds through CodeBuild (or any other CI tool) is stopping Maven from displaying download progress for dependencies. These log lines provide little to no value and just clog up the log file making it hard to read through.

As of Maven 3.6.1 you can do this by adding the new --no-transfer-progress switch to all commands.

However the CodeBuild standard build image may have an older version of Maven installed which doesn’t support that switch. Therefore in the install phase of your buildspec.yaml remove the provided version of Maven and download the latest version.

To make this easy for myself I’ve created a drop-in Bash script called codebuild-install-maven-3.6.2.sh (3.6.2 is the latest version at the time of writing):

# Install Maven
MAVEN_VERSION=3.6.2

# Replace the default Maven install with Maven 3.6.2
rm -Rf /opt/maven
wget -q http://us.mirrors.quenda.co/apache/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz
tar xvzf apache-maven-${MAVEN_VERSION}-bin.tar.gz -C /opt
rm apache-maven-${MAVEN_VERSION}-bin.tar.gz

export MAVEN_HOME=/opt/apache-maven-${MAVEN_VERSION}
export PATH=${MAVEN_HOME}/bin:${PATH}

This script is then invoked from my buildspec.yaml:

version: 0.2

phases:
  install:
    runtime-versions:
      java: "corretto8"

    commands:
      # Install Maven
      - . ./build/codebuild-install-maven-3.6.2.sh

Note it’s sourced (the first “.”) because it defines some environment variables which I want to be present later.

Finally in the build phase of my buildspec.yaml the Maven calls can use –no-transfer-progress to suppress the “downloading” messages:

      # Build
      - mvn versions:set -DnewVersion="${VERSION_FULL}" --no-transfer-progress
      - mvn clean package --no-transfer-progress

Note ${VERSION_FULL} holds the modver version number - see my previous post for details on that .