This page describes the process of installing and using the nexus repository manager to deploy 2 projects to the repository. This page will show you how to create a simple project and deploy it to the Nexus repository. We will go further and create a second project that will depend on the first one.
Installing Nexus
The process of installing the repository manager literally takes 10 minutes. There is a great video of this at the following link.
install nexus by first downloading it. Expand it out to some directory on your machine.
navigate to the bin/jsw/your-architecture/nexus start
It should print out something like this:
Starting Sonatype Nexus Repository Manager…
Started Sonatype Nexus Repository Manager.
navigate to the following url:
The default admin username/password is: admin/admin123
Creating a Test project
Start a test project by using an archetype.
Archetypes are blank project templates.
Run the following command to generate a new project using a maven archetype.
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
Answer the questions like this:
Define value for groupId: : testproject Define value for artifactId: : testproject Define value for version: 1.0-SNAPSHOT: : Define value for package: testproject: : Confirm properties configuration: groupId: testproject artifactId: testproject version: 1.0-SNAPSHOT package: testproject Y: :
Once the project is created you can verify that its working by typing…
cd testproject
mvn test
If all the tests pass the system should print something like this.
verma@verma-desktop:~/workspace$ cd testproject verma@verma-desktop:~/workspace/testproject$ mvn test ------------------------------------------------------- T E S T S ------------------------------------------------------- Running testproject.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1 second [INFO] Finished at: Sat Nov 14 19:31:02 EST 2009 [INFO] Final Memory: 18M/248M [INFO] ------------------------------------------------------------------------
Once Nexus repository manager is installed it is ready to accept snapshot and releases from Maven when you execute the deploy task.
In order for nexus to publish your project to nexus you need to insert the following into the settings.xml file located in the ~/.m2 folder.
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>snapshots</id> <username>deployment</username> <password>deployment123</password> </server> <server> <id>releases</id> <username>deployment</username> <password>deployment123</password> </server> <server> <id>thirdparty</id> <username>deployment</username> <password>deployment123</password> </server> </servers> </settings>
In order for your project to get published you need to put the following into your pom.xml. Open up the testproject/pom.xml file and insert the following right before the “dependencies” section in the file.
<distributionManagement> <repository> <id>releases</id> <url>http://localhost:8081/nexus/content/repositories/releases</url> </repository> <snapshotRepository> <id>snapshots</id> <name>Internal Snapshots</name> <url>http://localhost:8081/nexus/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement>
Posting snapshots to nexus
After application checkout you are ready to deploy this project to the Nexus repository so that other projects can use this as a dependency.
As part of the Maven build cycle when the build is complete it will want to deploy the packages to a repository. If you specify the following section inside your pom.xml file Maven will use it to post the artefacts there.
Snapshots vs Releases
If there is a “-SNAPSHOT” at the end of the project’s version label then the items will get posted to the snapshot repository. Otherwise it will go to the releases location.
Make sure you have the servers and the username and passwords defined in settings.xml file seen above. These id’s will be used to post the files to the repository.
To deploy the project to the Nexus repository just type:
mvn deploy
Verify that the project was posted to the Nexus repository
At this moment you can go to the nexus repository to verify that the project has been deployed.
(http://localhost:8081/nexus)
Click on the Repositories item on the left navigation. You should see a number of directories. Click on the snapshots and you should be able to see the test project you just created above.
Creating a test project that depends on the project above
Once you deploy the project into the nexus repository you can refer to it by putting it into the dependency section of your pom.xml file.
Run the following command to generate a new project using a maven archetype.
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
This time name the project testproject2
Define value for groupId: : testproject2 Define value for artifactId: : testproject2 Define value for version: 1.0-SNAPSHOT: : Define value for package: testproject2: : Confirm properties configuration: groupId: testproject2 artifactId: testproject2 version: 1.0-SNAPSHOT package: testproject2 Y: :
Once the project is created we can add some code dependencies in /src/main/java/
we need to do 3 things.
- Specify that this project depends on the “testproject” we created above.
- Specify that the Nexus repository be checked first.
- Define the snapshot and release repository (if you want to publish to Nexus)
Open pom.xml and add the following dependency from above.
<dependency> <groupId>testproject</groupId> <artifactId>testproject</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
Nexus will be checked first before the maven central site.
</build>
<repositories>
<repository>
<id>Company Central Code Repository</id>
<name>Company Central Code Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
</repositories>
<dependencies>
Releases and snapshots will be sent to Nexus.
<distributionManagement> <repository> <id>releases</id> <url>http://localhost:8081/nexus/content/repositories/releases</url> </repository> <snapshotRepository> <id>snapshots</id> <name>Internal Snapshots</name> <url>http://localhost:8081/nexus/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement>
Save and close the file.
Verify that the dependencies by typing the following on the command line.
dependency:tree
[INFO] [dependency:tree] [INFO] testproject2:testproject2:jar:1.0-SNAPSHOT [INFO] +- junit:junit:jar:3.8.1:test [INFO] \- testproject:testproject:jar:1.0-SNAPSHOT:compile
The above graph shows that the testproject2 depends on the testproject and junit.
At this time you can type mvn:deploy the code to the Nexus repository and navigate to the repository to search for “testproject2″. It should be located in the snapshot repository.
Deploying to the Release Repository
In order to deploy to the release repository all you need to change is the project version. Just remove the word “-SNAPSHOT” from the end and the system will post the file to the release repository.
Try it out yourself by opening up the pom.xml file for the testproject2 and running mvn deploy again.
Navigate to the nexus repository (http://localhost:8081/nexus) and you will see the project under releases.
Lazy man’s Repository
If you find the above steps are too much work then you can still publish by simply copying the artifact from your local “.m2/repo” to your website. Then just modify the pom.xml file of the project that needs this as a dependency.
<repositories>
<repository>
<id>LazyManRepo</id>
<name>Lazy Man Code Repository</name>
<url>http://your static site hostname/directory of your repository</url>
</repository>
</repositories>
If its a plugin
<pluginRepositories>
<pluginRepository>
<id>numberformat-releases</id>
<url>http://your static site hostname/directory of your repository</url>
</pluginRepository>
</pluginRepositories>
If its a archetype
mvn archetype:generate \ -DarchetypeGroupId=test \ -DarchetypeArtifactId=test-archetype \ -DarchetypeVersion=1.0-SNAPSHOT \ -DarchetypeRepository=http://your static site hostname/directory of your repository
There are a few limitations to this approach but its a quick way to get up and running.
Hi,
Its really an excellent useful article, but I ran into one problem, the issue is that I managed to deploy the built artifact, by granting Anonymous user the need roles. Meaning that the credentials deployment/deployment123, weren’t used. If I haven’t done that, then Maven build will fail throwing 401 error.
Error deploying artifact: Failed to transfer file: http://localhost:8080/nexus/content/repositories/releases/com/test/Project_Name/1.0.0/Project_Name-1.0.0.war. Return code is: 400
Thanks,
AA
Thanks, this is one of the best tutorials on how to deploy maven artifacts into nexus.
Simple and clear instructions with some basic background info was all I needed – thanks a bunch!
Serge
A big thanks . you responded to all my questions with you tutorial.
Very simple, very clear, with enough detail to get the project set-up .
Fadhel
Company Central Code Repository
Company Central Code Repository
http://localhost:8081/nexus/content/repositories/releases
This code snip id and name doesn’t match to those defined in , so initially I got an error in dependency:tree.
Very good article, easy to understand. Thanks for sharing.
Jirong
great tutrial many thanks