Automatical artifact deployment to Maven Central using Gitlab-CI

in maven •  4 years ago 

Original creation date: 18th September 2019
Note: Originally I posted this on my Wordpress Blog (https://1337codersblog.wordpress.com), but I decided to switch to steemit, so I copied it from there.

Automatical artifact deployment to Maven Central using Gitlab-CI

For a project I’m currently working on I need a Maven artifact for abstracting database connections so that I can change the implementations without modifying the actual source code, for example if I want to change from H2 database to MySQL.

So I had to write my own artifact and deploy it to Maven Central. Because I’m using Gitlab.com as my version control system I wanted the Gitlab CI-Runner to automatically deploy commits to Maven Central that have been push to the master branch.

Initial Setup

First register a group ID for Maven Central. The procedure is described in the [OSSRH Guide](https://central.sonatype.org/pages/ossrh-guide.html9.

Now add groupID, artifactID, version number and packaging to your pom.xml:

<groupId>com.gitlab.marvinh</groupId>
<artifactId>database-connector</artifactId>
<version>0.3</version>
<packaging>jar</packaging>

You have to decide which plugin you want to use for deployment. The Maven Deploy Plugin or the Nexus Staging Maven Plugin.

Maven Deploy Plugin

If you plan to deploy to Maven Central using another provider than Sonatype, you should use the Maven Deploy Plugin. Add the Maven Deploy Plugin to you pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
        </plugin>
    </plugins>
</build>

Add distribution management to your pom.xml:

<distributionManagement>
    <repository>
        <id>central</id>
        <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
    </repository>
    <snapshotRepository>
        <id>snapshot</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </snapshotRepository>
</distributionManagement>

Create file .m2/settings.xml with following content:

<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <servers>
        <server>
            <id>central</id>
            <username>${env.MAVEN_REPO_USER}</username>
            <password>${env.MAVEN_REPO_PASS}</password>
        </server>
        <server>
            <id>snapshot</id>
            <username>${env.MAVEN_REPO_USER}</username>
            <password>${env.MAVEN_REPO_PASS}</password>
        </server>
    </servers>
</settings>

Continue with step “Setting up Gitlab Runner”.

Nexus Staging Maven Plugin

If you want to use Sonatype to deploy to Maven Central you should use the Nexus Staging Maven Plugin. It will deploy to Sonatype and automatically closes and releases your artifact. So you don’t have to login to your Sonatype account and do the close and release manually like it would be necessary when using the Maven Deploy Plugin.

Add the Nexus Staging Maven Plugin to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.sonatype.plugins</groupId>
            <artifactId>nexus-staging-maven-plugin</artifactId>
            <version>1.6.8</version>
            <extensions>true</extensions>
            <executions>
                <execution>
                    <id>default-deploy</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <serverId>ossrh</serverId>
                <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                <autoReleaseAfterClose>true</autoReleaseAfterClose>
            </configuration>
        </plugin>
    </plugins>
</build>

Add distribution management to your pom.xml:

<distributionManagement>
    <repository>
        <id>central</id>
        <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
    </repository>
</distributionManagement>

Create file .m2/settings.xml with following content:

<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <servers>
        <server>
            <id>central</id>
            <username>${env.MAVEN_REPO_USER}</username>
            <password>${env.MAVEN_REPO_PASS}</password>
        </server>
        <server>
            <id>ossrh</id>
            <username>${env.MAVEN_REPO_USER}</username>
            <password>${env.MAVEN_REPO_PASS}</password>
        </server>
    </servers>
</settings>

Continue with step “Setting up Gitlab Runner”.

Setting up Gitlab Runner

Create file .gitlab-ci.yml:

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/

stages:
  - build
  - test
  - package
  - deploy

build:
  stage: build
  script:
    - mvn $MAVEN_CLI_OPTS compile

test:
  stage: test
  script:
    - mvn $MAVEN_CLI_OPTS test

package:
  stage: package
  script:
    - mvn $MAVEN_CLI_OPTS package -Dmaven.test.skip=true
  after_script:
    - mv target/*.jar .
  artifacts:
    paths:
    - ./*.jar


deploy:
  stage: deploy
  script:
    - mvn $MAVEN_CLI_OPTS clean deploy -Dmaven.test.skip=true
  only:
    - master

Create an user access token for the oss.sonatype.org. To do this, login to oss.sonatype.org. Click on your name in the right top corner and select “Profile”. Now you see a drop-down box where “Summary” is selected. Click on it and select “User Token”. On the displayed page click “Access User Token”. Now you see the user token: The left part is the user name and the right part is the passphrase.

You have to define CI environment Variables MAVEN_REPO_USER and MAVEN_REPO_PASS. To do this, login to your account on gitlab.com and go to the project. On the left panel click “Settings” and then “CI / CD”. In the “Variables” section click “Expand”. Here you provide in the “Key” field the name of the variable and in the “Value” field the value. For security reasons you should activate “Masked” for MAVEN_REPO_PASS. Use the user name part of the generated User Token as value for MAVEN_REPO_USER and the passphrase part of the User Token as value for MAVEN_REPO_PASS.

Now when the Gitlab-CI runs the deploy goal it will able to login to Sonatype and upload the artifact. But it is not yet able to close and release the uploaded artifact because your artifact still violates some rules of Sonatype. In the second part I will describe how to solve this.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!