How to add the common CI pipelines and pre-commit configuration to an existing Java project¶
The common CI pipeline and Java Maven pipeline templates provide a number of common jobs useful for Java based projects.
This page shows you how to add these jobs to a pipeline for an existing Java project.
Add the CI configuration¶
Update your .gitlab-ci.yml
file and add the following pipeline templates:
include:
- project: "uis/devops/continuous-delivery/ci-templates"
file: "/auto-devops/common-pipeline.yml"
ref: "{replace with the latest ci-templates repository tag}"
- project: "uis/devops/continuous-delivery/ci-templates"
file: "auto-devops-stages.yml"
ref: "{replace with the latest ci-templates repository tag}"
- project: "uis/devops/continuous-delivery/ci-templates"
file: "/auto-devops/maven.gitlab-ci.yml"
ref: "{replace with the latest ci-templates repository tag}"
variables:
# Add this variable if you don't have a Dockerfile in the repository and/or
# want to disable automatic image building.
BUILD_DISABLED: "1"
# Add this variable if your project does not need to run 'mvn deploy'
# to publish build artifacts to a GitLab Maven Repository
MAVEN_DEPLOY_DISABLED: "1"
# Add this variable if your project does not need to run 'mvn verify'
# to run tests in your pipeline
MAVEN_VERIFY_DISABLED: "1"
# Add this variable if you do not need to retrieve GitLab Access Tokens
# used to download Maven dependencies from a configured Maven Repository in GitLab
MAVEN_ACCESS_TOKENS_DISABLED: "1"
If your project needs to retrieve dependencies from a GitLab Maven Repository configured in your
Maven pom.xml, do not add the MAVEN_ACCESS_TOKENS_DISABLED
variable above, and extend the
build job in your .gitlab-ci.yml to use a provided script to retrieve GitLab access tokens:
build:
before_script: !reference [.maven.deploy_tokens]
Add the google-java-format pre-commit plugin¶
To ensure your Java source is formatted consistently, use the
google-java-format
pre-commit plugin by adding the following to your
.pre-commit-config.yaml
in the root of your project:
- repo: https://gitlab.developers.cam.ac.uk/uis/devops/continuous-delivery/precommit-google-java-format
rev: v1.0.0
hooks:
- id: google-java-format
files: ".java$"
args: ["-i"]
If you have not already done so for this project, run pre-commit install
to configure the
pre-commit hooks to run when you commit.
To manually format your source using google-java-format (and run all other pre-commit checks),
run pre-commit run --all-files
.
This plugin formats your code to match the
Google Java Style Guide on git commit
.
If your code already meets the required code format the plugin will pass and allow your commit to
continue. If your code requires formatting it will format the code automatically for you but fail
pre-commit. This allows you to review the changes and commit any updates to continue.
IDE formatter configuration¶
Plugins for the google-java-format code formattter are available for most IDEs - download and install from the following links: