Skip to content
Snippets Groups Projects
README.md 32.78 KiB

GitLab CI template for Maven

This project implements a GitLab CI/CD template to build, test and analyse your Maven-based projects.

Usage

This template can be used both as a CI/CD component or using the legacy include:project syntax.

Use as a CI/CD component

Add the following to your gitlab-ci.yml:

include:
  # 1: include the component
  - component: gitlab.com/to-be-continuous/maven/gitlab-ci-maven@3.9.0
    # 2: set/override component inputs
    inputs:
      # ⚠ this is only an example
      image: registry.hub.docker.com/library/maven:3.8-openjdk-18
      deploy-enabled: true

Use as a CI/CD template (legacy)

Add the following to your gitlab-ci.yml:

include:
  # 1: include the template
  - project: 'to-be-continuous/maven'
    ref: '3.9.0'
    file: '/templates/gitlab-ci-maven.yml'

variables:
  # 2: set/override template variables
  # ⚠ this is only an example
  MAVEN_IMAGE: registry.hub.docker.com/library/maven:3.8-openjdk-18
  MAVEN_DEPLOY_ENABLED: "true"

Global configuration

The Maven template uses some global configuration throughout all jobs.

Input / Variable Description Default value
image / MAVEN_IMAGE The Docker image used to run Maven
⚠️ set the version required by your project
registry.hub.docker.com/library/maven:latest
project-dir / MAVEN_PROJECT_DIR Maven projet root directory .
cfg-dir / MAVEN_CFG_DIR The Maven configuration directory .m2
settings-file / MAVEN_SETTINGS_FILE The Maven settings.xml file path ${MAVEN_CFG_DIR}/settings.xml
opts / MAVEN_OPTS Global Maven options -Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=${MAVEN_CFG_DIR}/repository -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true
cli-opts / MAVEN_CLI_OPTS Additional Maven options used on the command line --no-transfer-progress --batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true

About $MAVEN_CFG_DIR

This variable is used to define the Maven configuration directory. It is used to declare the cache policy and marked the ${MAVEN_CFG_DIR}/repository directory as cached (not to download Maven dependencies over and over again).

If you have a good reason to do differently, you'll have to override the MAVEN_CLI_OPTS variable as well as the cache policy.

About $MAVEN_SETTINGS_FILE

If a file is found at the $MAVEN_SETTINGS_FILE location, the template automatically uses it as a settings.xml (using the --settings option on command line).

Note that with this design you are free to either:

  1. inline the settings.xml file into your repository source (⚠️ make sure not to inline secrets but use the ${env.MY_PASSWORD} replacement pattern instead and define the MY_PASSWORD variable as secret project variable),
  2. or define the settings.xml content as a file type project variable.