Build java artifact with Maven and publish to Nexus artifact repository

Build java artifact with Maven and publish to Nexus artifact repository

When there is only one developer, a central repository on internet and a local repository on developer’s machine can be developed using Maven

However, when a Java application is to be divided into multiple sub-projects and development is to be carried out in a team, library dependency resolution becomes complex; hence this dependency resolution needs to be automated. For this, availability of package repository server is essential.

For some languages, the source code needs to be compiled first in order for it to be executed by the machine. This process generates files and these files generally are related to a version. Therefore, having multiple versions without a minimum management can be quite dangerous

Maven and Nexus come into play. Together they help build and manage Java dependencies in a simple and reliable way. In this article, we will be showing how to deploy Java artifacts using both technologies, but first, let’s have a brief explanation of what they are.

Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages.

Nexus is a repository manager that stores “artifacts”, which allows you to proxy collect and manage your dependencies, making it easy to distribute your software. When talking about “artifacts”, we mean external libraries like, for example, JARs files for Java libraries and packages for Node. This way, we can have access to multiple external libraries and different versions of them.

Before getting started,

I deploy my infrastructure on Digital Ocean using an ubuntu-s-4vcpu-8gb-intel-fra1-01 / 8 GB Memory / 4 Intel vCPUs / 160 GB Disk / - Ubuntu 20.04 (LTS) x64 server

Created nexus user, download unzip and start nexus server using the command

?/opt/nexus-3.38.0-01/bin/nexus start        

1. Nexus Requirements in the pom.xml

In order for Maven to be able to deploy the artifacts it creates in the package phase of the build, it needs to define the repository information where the packaged artifacts will be deployed, via the distributionManagement element:

No alt text provided for this image

2. Plugins

By default, Maven handles the deployment mechanism via the maven-deploy-plugin – this mapped to the deployment phase of the default Maven lifecycle:

No alt text provided for this image

By default, the deploy goal includes the staging workflow, which is recommended for release builds. The deploy goal of the plugin is mapped to the deploy phase of the Maven build.

3. The Global settings.xml

Deployment to Nexus is a secured operation and a deployment user exists for this purpose out of the box on any Nexus instance.

Configuring Maven with the credentials of this deployment user, so that it can interact correctly with Nexus, cannot be done in the pom.xml of the project. This is because the syntax of the pom doesn't allow it, not to mention the fact that the pom may be a public artifact, so not well suited to hold credential information.

The credentials of the server have to be defined in the global Maven setting.xml:

No alt text provided for this image

The server can also be configured to use key based security instead of raw and plaintext credentials.

Once credentials correctly configure we us "mvn package" command to build before deploying.

No alt text provided for this image

4. The Deployment Process

Performing the deployment process is a simple task:

With these configurations defined, we can run our “mvn deploy” command (in this case, i ran it under IntelliJ’s maven tab):

No alt text provided for this image
No alt text provided for this image

We can also check the uploaded files on our Nexus:

No alt text provided for this image

Our artifacts are now hosted beautifully and securely in our Nexus repository!

要查看或添加评论,请登录

Antoine CHOULA的更多文章

社区洞察

其他会员也浏览了