Upload Maven Project to Nexus Command Line
- Get link
- X
- Other Apps
Deploying Java artifacts on a Nexus Repository with Maven
Every bit a software project grows, the number of dependencies increases, requiring the need of managing them in a scalable way. Different forms of management exist and they were created based on multiple projects' natures and technologies.
For some languages, the source code needs to exist compiled commencement in order for it to be executed past the auto. This procedure generates files and these files by and large are related to a version. Therefore, having multiple versions without a minimum direction can exist quite unsafe.
This is where 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 start, permit's have a brief explanation of what they are.
Maven is a build automation tool used primarily for Java projects that addresses how software is built and its dependencies. Information technology uses a XML file describing the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins. Maven dynamically downloads Java libraries and Maven plug-ins by accessing one or more repositories such as the Maven 2 Central Repository, and stores them in a local enshroud.
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", nosotros mean external libraries like, for example, JARs files for Java libraries and packages for Node. This way, we tin can take access to multiple external libraries and different versions of them.
In this case, both Maven and Nexus were chosen because they were currently being used by our team. An alternative to Maven for artifacts deployment could be Gradle and the reason of using an internal Nexus repository is to accept efficient and centralized collaboration between developers and teams in a visitor, besides involving issues such every bit company'southward compliance and information security.
Getting into action!
To make this as straightforward every bit possible, we will be basically using the following maven commands:
- mvn deploy - Used in near project builds, it is invoked when you call the deploy stage of the default build lifecycle of a maven project.
- mvn deploy:deploy-file - Used to deploy artifacts, which are non built using Maven, to any remote repository.
To run both commands properly, nosotros need to make some basic configurations first in lodge to have access to our internal Nexus repository, assuming it requires authentication (in our case it does).
Preparing the basis
Start of all, we need to create a file named "settings.xml" and place it under a folder named ".m2", which represents the local repository of Maven (in whatever operational system). This file will contain the "id", "username" and "password" required to access our internal Nexus, as shown bellow:
<settings>
<servers>
<server>
<id>my-repo-id</id>
<username>my-username</username>
<password>my-countersign</password>
</server>
</servers>
</settings>
This step is required to run both commands!
Running mvn:deploy
Add the post-obit code snippet inside the "pom.xml" file of our maven project that we desire to deploy:
<project>
...
<distributionManagement>
<repository>
<id>my-repo-id</id>
<url>https://my-nexus-repo-accost.com</url>
</repository>
</distributionManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>two.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
With these configurations defined, we tin run our "mvn deploy" command (in this instance, i ran it nether IntelliJ'due south maven tab):
mvn deploy -DskipTests -Dmaven.install.skip=truthful
It builds our project and uploads it to the repository we divers in our "pom.xml" file. With "-DskipTests" nosotros skip running application's tests and "-Dmaven.install.skip=true" bypasses local artifact installation. After running it successfully, we can meet the following message on IDE's console:
We tin can besides check the uploaded files on our Nexus:
Running mvn deploy:deploy-file
This command enables us to deploy any installed dependency on our motorcar to our Nexus, without the need of having its source code! The only thing we will demand is the JAR file itself. This means that this can deploy whatever artifact, even if it was not built with Maven.
With "settings.xml" configured and placed correctly, before running the command, we demand to pay attention to the proper noun of the file we want to deploy as information technology must lucifer the "-Dfile" argument value we will input. Then, we can run information technology from a Linux terminal:
mvn deploy:deploy-file -Durl=https://my-nexus-repo-address.com -Dfile=my-java-artifact.jar -DgroupId=com.case -DartifactId=my-java-artifact -Dpackaging=jar -Dversion=one.0.0 -DrepositoryId=my-repo-id
Subsequently running information technology successfully, we should see the post-obit bulletin on our final:
We tin besides check the uploaded files on our Nexus:
Aaaand there you go. Our artifacts are now hosted beautifully and deeply in our Nexus repository!
Determination
By using both Maven commands and Nexus, nosotros have an easy and fast way of deploying java dependencies to a repository. With them, nosotros can even "transfer"artifacts from one repository to some other in instance we don't accept admission to their respective maven projects, which really saves a lot of possible headaches (trust me, it actually is a pain).
References
Source: https://rodolfombc.medium.com/deploying-java-artifacts-on-a-nexus-repository-with-maven-939a80406acf
- Get link
- X
- Other Apps
Comments
Post a Comment