How to publish Mule artifacts to Nexus Repository


In a previous post, we’ve seen
How to install a Nexus Repository in a Docker container. Now it’s the time to play with it and learn how we can use Nexus as a Private repository for our Mule artifacts.
In this post, we will see how to create a new repository and use it from a Mule project to deploy our Mule artifacts to it.

Prerequisites

To follow this tutorial we need an instance of Nexus. If you don’t have one, you can quickly spin up one with Docker. Check our the post How to install Nexus on Docker to see how to do it.

Set up our Nexus repository

Once we’ve got our Nexus instance we will create a new repo for our Mule artifacts and a local user in Nexus to deploy to this repo.

Create a repo

Go to your Nexus web management console and sign in as Admin. Go to Server and Administration (1) (The gear icon at the top) and click on Repositories (2). Then click on Create repository (3)



In the Select recipe page, select a Maven 2 (hosted) type of repository


Next, provide a name for your repo and leave the rest of the options as default:
  • Version Policy = Release (this would be a repo for releases, not for Snapshots)
  • Layout Policy = Strict
  • Content Disposition = Inline
  • Blob store = default
  • Strict Content Type Validation checked
  • Deployment Policy = Disable redeploy
  • No Cleanup policies


Click Create repository. Now you should see your new repository in the list of repositories.
In the URL column, click on the Copy button of your new repo to get the URL. We will use it in our Mule project.


Create a Role and a User in Nexus

In this step, we will create a specific user with write permission only to our new repo to be used in our Mule project. We should not use the Admin user, that good be a security risk and could compromise all of our repositories. For that, first we will create a Role in Nexus.
Go to the Server and Administration and click on Roles in the left panel. From there, click on Create Role on top right corner:



Next, for the role creation, provide the following info:
  • Type - Nexus Role
  • Role ID - Specify an ID for the new Role
  • Role Name - Choose a name for your new Role
  • Role Description - Provide a meaningful description of what this role allows to do

Then, click on Modify Applied Privileges, and in the Privileges Selection window, search for the name of your repository (this way we will filtered in only the permissions related to your new repo). 

In Nexus, the general rule for permissions is nx-repository-admin privileges are for administering the repositories and their details; nx-repository-view privileges are for use of the repositories once set up.
So, in our case we need to select the All privileges for nx-repository-view-maven2-[YOUR_REPO] repository views and click Confirm



Click on Save to create your new role. Now you should see your new role in the list of Roles


Next, we need to create a user and assign it this new role.
Go to Security and Administration > Users and click on Create Local User



Then, provide an ID for your new user and contact details (First name, Last name...). Make sure you select Active as status and that you select the new role we’ve just created and include it as Granted.


Click on Create local user. Now you should see your new user in the list of users. 
With that, our Nexus Repository is ready to receive content from our Mule apps.

Create a Mule Project

Head over to Anypoint Studio and create a New Mule Project. Then, open the pom.xml file. We need to add the following items:
  • Maven coordinates - These will be the coordinates that will identify this artifact in our Nexus repository:
    • groupId - Typically we use the reverse internal domain name (com.mycompany)
    • artifactId - The name of the mule app
    • version - We will use 1.0.0 (remove the SNAPSHOT, our Nexus repository is only for Releases)
    • packaging - In this example we will use a mule-application
<groupId>com.demos.gon</groupId>
<artifactId>hello-gon</artifactId>
<version>1.0.0</version>
<packaging>mule-application</packaging>


Add the Nexus Repository to our Project

Remember, mule projects are mavenized, that is, they follow the build lifecycle of Maven and the Maven rules. This means that Maven will first package our mule project in a jar file, then it will install it in the local repository (the local machine) and in the last step il will deploy the artifact (the jar) to a repository.
For that, we need to tell Maven where our Nexus repository is located and how to get access to it.

The first step is to include the distributionManagement element in the pom of our mule project. This element contains the repository to which Maven will deploy the jar. Within the repository you will have to specify:
  • id - Any ID that helps you identify your repository. The recommendation is to use the same name as your Nexus repo
  • name - A descriptive name of this repository (you can put anything in here, it’s just informative)
  • url - In here we need to paste the URL of the repository we created in the precedent step, when we created the repo
In our example:
<project>
...
<distributionManagement>
<repository>
<id>gon-nexus-mule</id>
<name>Gon Nexus Repo</name>
<url>http://localhost:8081/repository/gon-nexus-mule/</url>
</repository>
</distributionManagement>
...
</project>

You might be wondering, what about the credentials? How is Maven going to access the Nexus repo? The answer is the settings.xml file. We don’t include credentials in a pom file.

For that, go to your local maven directory, normally located at ~/.m2 and open the settings.xml file. If there’s no file created one with that name.
In here we need to add two elements:
  • repository element (with the details of our repo) - add it within the repositories element
<settings>
<repositories>
...
<repository>
<id>gon-nexus-mule</id>
<name>Gon Nexus Repo</name>
<url>http://localhost:8081/repository/gon-nexus-mule/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
</repository>
...
</repositories>
...
</settings>
  • Server - Create a Servers element if it does not exist and provide the credentials of the user we created to write into our new repo in Nexus. Like this:
<settings>
<servers>
...
<server>
<id>gon-nexus-mule</id>
<username>mule-dev</username>
<password>Mule1234</password>
</server>
...
</servers>
...
</settings>

Make sure the id within the server element matches exactly the value of the id in the repository element. This is how Maven links both.

If you’re wondering if it is recommended to put the credentials in plain text in the settings file the answer is NO. Here we would normally parametrize the settings file and use a couple of properties for username and password that would be injected at deploy time. But, here, for simplicity we will put them in plain.

Save your settings file

Package and Deploy the app

Time to test if everything works. To deploy our app to Nexus, we need to use maven commands. Open a terminal from the root folder of your project (in Anypoint Studio, right-click on the name of the project > Show In > System Explorer, and open the terminal from there). Run the command:

mvn clean deploy



If everything went well you should see the success message. Now, let’s get back to our Nexus repository and see if our new mule artifact has been uploaded.
In Nexus, go to Browse server contents > Browse and double click on our Mule repository. And voila! Here’s our test project with the maven coordinates we specified:

Previous Post Next Post