In this article, we will show how you can leverage the power of [GitLab CI/CD](https://about.gitlab.com/features/gitlab-ci-cd/)
to build a [Maven](https://maven.apache.org/) project, deploy it to [Artifactory](https://www.jfrog.com/artifactory/), and then use it from another Maven application as a dependency.
You'll create two different projects:
-`simple-maven-dep`: the app built and deployed to Artifactory (available at https://gitlab.com/gitlab-examples/maven/simple-maven-dep)
-`simple-maven-app`: the app using the previous one as a dependency (available at https://gitlab.com/gitlab-examples/maven/simple-maven-app)
We assume that you already have a GitLab account on [GitLab.com](https://gitlab.com/), and that you know the basic usage of Git and [GitLab CI/CD](https://about.gitlab.com/features/gitlab-ci-cd/).
We also assume that an Artifactory instance is available and reachable from the internet, and that you have valid credentials to deploy on it.
## Create the simple Maven dependency
First of all, you need an application to work with: in this specific case we will
use a simple one, but it could be any Maven application. This will be the
dependency you want to package and deploy to Artifactory, in order to be
available to other projects.
### Prepare the dependency application
For this article you'll use a Maven app that can be cloned from our example
project:
1. Log in to your GitLab account
1. Create a new project by selecting **Import project from ➔ Repo by URL**
Username and password will be replaced by the correct values using secret variables.
### Configure GitLab CI/CD for `simple-maven-dep`
Now it's time we set up [GitLab CI/CD](https://about.gitlab.com/features/gitlab-ci-cd/) to automatically build, test and deploy the dependency!
GitLab CI/CD uses a file in the root of the repo, named `.gitlab-ci.yml`, to read the definitions for jobs
that will be executed by the configured GitLab Runners. You can read more about this file in the [GitLab Documentation](https://docs.gitlab.com/ee/ci/yaml/).
First of all, remember to set up secret variables for your deployment. Navigate to your project's **Settings > CI/CD** page
and add the following secret variables (replace them with your current values, of course):
Now it's time to define jobs in `.gitlab-ci.yml` and push it to the repo:
```yaml
image:maven:latest
variables:
MAVEN_CLI_OPTS:"-s.m2/settings.xml--batch-mode"
MAVEN_OPTS:"-Dmaven.repo.local=.m2/repository"
cache:
paths:
-.m2/repository/
-target/
build:
stage:build
script:
-mvn $MAVEN_CLI_OPTS compile
test:
stage:test
script:
-mvn $MAVEN_CLI_OPTS test
deploy:
stage:deploy
script:
-mvn $MAVEN_CLI_OPTS deploy
only:
-master
```
GitLab Runner will use the latest [Maven Docker image](https://hub.docker.com/_/maven/), which already contains all the tools and the dependencies you need to manage the project,
in order to run the jobs.
Environment variables are set to instruct Maven to use the `homedir` of the repo instead of the user's home when searching for configuration and dependencies.
Caching the `.m2/repository folder` (where all the Maven files are stored), and the `target` folder (where our application will be created), is useful for speeding up the process
by running all Maven phases in a sequential order, therefore, executing `mvn test` will automatically run `mvn compile` if necessary.
Both `build` and `test` jobs leverage the `mvn` command to compile the application and to test it as defined in the test suite that is part of the application.
Deploy to Artifactory is done as defined by the secret variables we have just set up.
The deployment occurs only if we're pushing or merging to `master` branch, so that the development versions are tested but not published.
Done! Now you have all the changes in the GitLab repo, and a pipeline has already been started for this commit. In the **Pipelines** tab you can see what's happening.
If the deployment has been successful, the deploy job log will output:
-[Spring boot application with GitLab CI and Kubernetes](https://about.gitlab.com/2016/12/14/continuous-delivery-of-a-spring-boot-application-with-gitlab-ci-and-kubernetes/)
-[Spring boot application with GitLab CI and Kubernetes](https://about.gitlab.com/2016/12/14/continuous-delivery-of-a-spring-boot-application-with-gitlab-ci-and-kubernetes/)
-[Setting up GitLab CI for iOS projects](https://about.gitlab.com/2016/03/10/setting-up-gitlab-ci-for-ios-projects/)
-[Setting up GitLab CI for iOS projects](https://about.gitlab.com/2016/03/10/setting-up-gitlab-ci-for-ios-projects/)
@@ -27,8 +27,7 @@ Apart from those, here is an collection of tutorials and guides on setting up yo
...
@@ -27,8 +27,7 @@ Apart from those, here is an collection of tutorials and guides on setting up yo
### Java
### Java
-**Articles:**
-[Continuous Delivery of a Spring Boot application with GitLab CI and Kubernetes](https://about.gitlab.com/2016/12/14/continuous-delivery-of-a-spring-boot-application-with-gitlab-ci-and-kubernetes/)
-[Continuous Delivery of a Spring Boot application with GitLab CI and Kubernetes](https://about.gitlab.com/2016/12/14/continuous-delivery-of-a-spring-boot-application-with-gitlab-ci-and-kubernetes/)
### Scala
### Scala
...
@@ -41,18 +40,15 @@ Apart from those, here is an collection of tutorials and guides on setting up yo
...
@@ -41,18 +40,15 @@ Apart from those, here is an collection of tutorials and guides on setting up yo
### Elixir
### Elixir
-[Test a Phoenix application](test-phoenix-application.md)
-[Test a Phoenix application](test-phoenix-application.md)
-**Articles:**
-[Building an Elixir Release into a Docker image using GitLab CI](https://about.gitlab.com/2016/08/11/building-an-elixir-release-into-docker-image-using-gitlab-ci-part-1/)
-[Building an Elixir Release into a Docker image using GitLab CI](https://about.gitlab.com/2016/08/11/building-an-elixir-release-into-docker-image-using-gitlab-ci-part-1/)
### iOS
### iOS
-**Articles:**
-[Setting up GitLab CI for iOS projects](https://about.gitlab.com/2016/03/10/setting-up-gitlab-ci-for-ios-projects/)
-[Setting up GitLab CI for iOS projects](https://about.gitlab.com/2016/03/10/setting-up-gitlab-ci-for-ios-projects/)
### Android
### Android
-**Articles:**
-[Setting up GitLab CI for Android projects](https://about.gitlab.com/2016/11/30/setting-up-gitlab-ci-for-android-projects/)
-[Setting up GitLab CI for Android projects](https://about.gitlab.com/2016/11/30/setting-up-gitlab-ci-for-android-projects/)
### Code quality analysis
### Code quality analysis
...
@@ -71,20 +67,19 @@ Apart from those, here is an collection of tutorials and guides on setting up yo
...
@@ -71,20 +67,19 @@ Apart from those, here is an collection of tutorials and guides on setting up yo
-[Using `dpl` as deployment tool](deployment/README.md)
-[Using `dpl` as deployment tool](deployment/README.md)
-[Repositories with examples for various languages](https://gitlab.com/groups/gitlab-examples)
-[Repositories with examples for various languages](https://gitlab.com/groups/gitlab-examples)
-[The .gitlab-ci.yml file for GitLab itself](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/.gitlab-ci.yml)
-[The .gitlab-ci.yml file for GitLab itself](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/.gitlab-ci.yml)
-**Articles:**
-[Continuous Deployment with GitLab: how to build and deploy a Debian Package with GitLab CI](https://about.gitlab.com/2016/10/12/automated-debian-package-build-with-gitlab-ci/)
-[Continuous Deployment with GitLab: how to build and deploy a Debian Package with GitLab CI](https://about.gitlab.com/2016/10/12/automated-debian-package-build-with-gitlab-ci/)
-[How to deploy Maven projects to Artifactory with GitLab CI/CD](artifactory_and_gitlab/index.md)
## GitLab CI for GitLab Pages
## GitLab CI/CD for GitLab Pages
-[Example projects](https://gitlab.com/pages)
-[Example projects](https://gitlab.com/pages)
-**Articles:**
-[Creating and Tweaking `.gitlab-ci.yml` for GitLab Pages](../../user/project/pages/getting_started_part_four.md)
-[Creating and Tweaking `.gitlab-ci.yml` for GitLab Pages](../../user/project/pages/getting_started_part_four.md)
-[SSGs Part 3: Build any SSG site with GitLab Pages](https://about.gitlab.com/2016/06/17/ssg-overview-gitlab-pages-part-3-examples-ci/):
-[SSGs Part 3: Build any SSG site with GitLab Pages](https://about.gitlab.com/2016/06/17/ssg-overview-gitlab-pages-part-3-examples-ci/):
examples for Ruby-, NodeJS-, Python-, and GoLang-based SSGs
examples for Ruby-, NodeJS-, Python-, and GoLang-based SSGs
-[Building a new GitLab docs site with Nanoc, GitLab CI, and GitLab Pages](https://about.gitlab.com/2016/12/07/building-a-new-gitlab-docs-site-with-nanoc-gitlab-ci-and-gitlab-pages/)
-[Building a new GitLab docs site with Nanoc, GitLab CI, and GitLab Pages](https://about.gitlab.com/2016/12/07/building-a-new-gitlab-docs-site-with-nanoc-gitlab-ci-and-gitlab-pages/)
-[Publish code coverage reports with GitLab Pages](https://about.gitlab.com/2016/11/03/publish-code-coverage-report-with-gitlab-pages/)
-[Publish code coverage reports with GitLab Pages](https://about.gitlab.com/2016/11/03/publish-code-coverage-report-with-gitlab-pages/)
See the documentation on [GitLab Pages](../../user/project/pages/index.md) for a complete overview.
See the topic [GitLab Pages](../../user/project/pages/index.md) for a complete overview.
In this article, we will show how you can leverage the power of [GitLab CI/CD](https://about.gitlab.com/features/gitlab-ci-cd/)
to build a [Maven](https://maven.apache.org/) project, deploy it to [Artifactory](https://www.jfrog.com/artifactory/), and then use it from another Maven application as a dependency.
You'll create two different projects:
-`simple-maven-dep`: the app built and deployed to Artifactory (available at https://gitlab.com/gitlab-examples/maven/simple-maven-dep)
-`simple-maven-app`: the app using the previous one as a dependency (available at https://gitlab.com/gitlab-examples/maven/simple-maven-app)
We assume that you already have a GitLab account on [GitLab.com](https://gitlab.com/), and that you know the basic usage of Git and [GitLab CI/CD](https://about.gitlab.com/features/gitlab-ci-cd/).
We also assume that an Artifactory instance is available and reachable from the internet, and that you have valid credentials to deploy on it.
## Create the simple Maven dependency
First of all, you need an application to work with: in this specific case we will
use a simple one, but it could be any Maven application. This will be the
dependency you want to package and deploy to Artifactory, in order to be
available to other projects.
### Prepare the dependency application
For this article you'll use a Maven app that can be cloned from our example
project:
1. Log in to your GitLab account
1. Create a new project by selecting **Import project from ➔ Repo by URL**
Username and password will be replaced by the correct values using secret variables.
### Configure GitLab CI/CD for `simple-maven-dep`
Now it's time we set up [GitLab CI/CD](https://about.gitlab.com/features/gitlab-ci-cd/) to automatically build, test and deploy the dependency!
GitLab CI/CD uses a file in the root of the repo, named `.gitlab-ci.yml`, to read the definitions for jobs
that will be executed by the configured GitLab Runners. You can read more about this file in the [GitLab Documentation](https://docs.gitlab.com/ee/ci/yaml/).
First of all, remember to set up secret variables for your deployment. Navigate to your project's **Settings > CI/CD** page
and add the following secret variables (replace them with your current values, of course):
Now it's time to define jobs in `.gitlab-ci.yml` and push it to the repo:
```yaml
image:maven:latest
variables:
MAVEN_CLI_OPTS:"-s.m2/settings.xml--batch-mode"
MAVEN_OPTS:"-Dmaven.repo.local=.m2/repository"
cache:
paths:
-.m2/repository/
-target/
build:
stage:build
script:
-mvn $MAVEN_CLI_OPTS compile
test:
stage:test
script:
-mvn $MAVEN_CLI_OPTS test
deploy:
stage:deploy
script:
-mvn $MAVEN_CLI_OPTS deploy
only:
-master
```
GitLab Runner will use the latest [Maven Docker image](https://hub.docker.com/_/maven/), which already contains all the tools and the dependencies you need to manage the project,
in order to run the jobs.
Environment variables are set to instruct Maven to use the `homedir` of the repo instead of the user's home when searching for configuration and dependencies.
Caching the `.m2/repository folder` (where all the Maven files are stored), and the `target` folder (where our application will be created), is useful for speeding up the process
by running all Maven phases in a sequential order, therefore, executing `mvn test` will automatically run `mvn compile` if necessary.
Both `build` and `test` jobs leverage the `mvn` command to compile the application and to test it as defined in the test suite that is part of the application.
Deploy to Artifactory is done as defined by the secret variables we have just set up.
The deployment occurs only if we're pushing or merging to `master` branch, so that the development versions are tested but not published.
Done! Now you have all the changes in the GitLab repo, and a pipeline has already been started for this commit. In the **Pipelines** tab you can see what's happening.
If the deployment has been successful, the deploy job log will output: