Skip to content
Snippets Groups Projects
Commit 7775c358 authored by Xavier FRANCOIS's avatar Xavier FRANCOIS Committed by Pierre Smeyers
Browse files

feat: support settings.xml to be passed as file-type variable

parent 5ced64d3
No related branches found
No related tags found
No related merge requests found
...@@ -18,22 +18,29 @@ include: ...@@ -18,22 +18,29 @@ include:
The Maven template uses some global configuration throughout all jobs. The Maven template uses some global configuration throughout all jobs.
| Name | description | default value | | Name | description | default value |
| --------------------- | -------------------------------------- | ----------------- | | --------------------- |--------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| `MAVEN_IMAGE` | The Docker image used to run Maven <br/>:warning: **set the version required by your project** | `registry.hub.docker.com/library/maven:latest` | | `MAVEN_IMAGE` | The Docker image used to run Maven <br/>:warning: **set the version required by your project** | `registry.hub.docker.com/library/maven:latest` |
| `MAVEN_PROJECT_DIR` | Maven projet root directory | `.` | | `MAVEN_PROJECT_DIR` | Maven projet root directory | `.` |
| `MAVEN_CFG_DIR` | The Maven configuration directory | `.m2` | | `MAVEN_CFG_DIR` | The Maven configuration directory | `.m2` |
| `MAVEN_SETTINGS_FILE` | The Maven `settings.xml` file path | `${MAVEN_CFG_DIR}/settings.xml` |
| `MAVEN_OPTS` | [Global Maven options](http://maven.apache.org/configure.html#maven_opts-environment-variable) | `-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=${MAVEN_CFG_DIR}/repository -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true` | | `MAVEN_OPTS` | [Global Maven options](http://maven.apache.org/configure.html#maven_opts-environment-variable) | `-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=${MAVEN_CFG_DIR}/repository -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true` |
| `MAVEN_CLI_OPTS` | Additional [Maven options](https://maven.apache.org/ref/3-LATEST/maven-embedder/cli.html) used on the command line | `--no-transfer-progress --batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true` | | `MAVEN_CLI_OPTS` | Additional [Maven options](https://maven.apache.org/ref/3-LATEST/maven-embedder/cli.html) used on the command line | `--no-transfer-progress --batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true` |
### About `$MAVEN_CFG_DIR` ### About `$MAVEN_CFG_DIR`
This variable is used to define the Maven configuration directory. It is used for two (2) purposes: 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).
* in case a Maven settings file (`settings.xml`) is found, the template automatically uses it (using the `-s` option on command line),
* the cache policy declares 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`](https://docs.gitlab.com/ee/ci/yaml/README.html#cache) policy. If you have a good reason to do differently, you'll have to override the `MAVEN_CLI_OPTS` variable as well as the [`cache`](https://docs.gitlab.com/ee/ci/yaml/README.html#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](https://maven.apache.org/ref/current/maven-embedder/cli.html) on command line).
Note that with this design you are free to either:
1. inline the `settings.xml` file into your repository source (:warning: 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](https://docs.gitlab.com/ee/ci/variables/#use-file-type-cicd-variables).
## Jobs ## Jobs
### `mvn-build` job ### `mvn-build` job
...@@ -236,7 +243,7 @@ You may handle them in the following ways: ...@@ -236,7 +243,7 @@ You may handle them in the following ways:
<!-- ... --> <!-- ... -->
``` ```
`${MAVEN_CFG_DIR}/settings.xml`: `${MAVEN_SETTINGS_FILE}`:
```xml ```xml
<settings> <settings>
......
...@@ -21,6 +21,12 @@ ...@@ -21,6 +21,12 @@
"default": ".m2", "default": ".m2",
"advanced": true "advanced": true
}, },
{
"name": "MAVEN_SETTINGS_FILE",
"description": "The Maven `settings.xml` file path",
"default": "${MAVEN_CFG_DIR}/settings.xml",
"advanced": true
},
{ {
"name": "MAVEN_OPTS", "name": "MAVEN_OPTS",
"description": "[Global Maven options](http://maven.apache.org/configure.html#maven_opts-environment-variable)", "description": "[Global Maven options](http://maven.apache.org/configure.html#maven_opts-environment-variable)",
......
...@@ -57,6 +57,8 @@ variables: ...@@ -57,6 +57,8 @@ variables:
# default configuration directory # default configuration directory
MAVEN_CFG_DIR: ".m2" MAVEN_CFG_DIR: ".m2"
# default settings.xml file path
MAVEN_SETTINGS_FILE: "$MAVEN_CFG_DIR/settings.xml"
# `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work. # `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
MAVEN_OPTS: >- MAVEN_OPTS: >-
...@@ -344,12 +346,12 @@ stages: ...@@ -344,12 +346,12 @@ stages:
fi fi
} }
# autodetects any Maven settings file in $MAVEN_CFG_DIR and builds the Java CLI option accordingly # autodetects any Maven settings file in and builds the Java CLI option accordingly
function eval_mvn_settings_opt() { function eval_mvn_settings_opt() {
if [[ -f "$MAVEN_CFG_DIR/settings.xml" ]] if [[ -f "$MAVEN_SETTINGS_FILE" ]]
then then
log_info "Maven settings file found: \\e[33;1m$MAVEN_CFG_DIR/settings.xml\\e[0m" log_info "Maven settings file found: \\e[33;1m$MAVEN_SETTINGS_FILE\\e[0m"
mvn_settings_opt="-s $MAVEN_CFG_DIR/settings.xml" mvn_settings_opt="-s $MAVEN_SETTINGS_FILE"
fi fi
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment