Build internal maven repositories

Build internal maven repositories

Installation of management tool

Choose sonatype nexus repository manager to bulid internal maven repositories.

Download link: https://help.sonatype.com/repomanager3/product-information/download

Select a tar match your system and execute following commands:

1
2
3
4
wget https://download.sonatype.com/nexus/3/nexus-3.41.1-01-unix.tar.gz
tar zxvf nexus-3.41.1-01-unix.tar.gz
cd nexus-3.41.1-01/bin/
./nexus start

Because use root to start nexus, check the log:

1
tail -f /root/sonatype-work/nexus3/log/nexus.log

application is started

1
2
3
4
5
-------------------------------------------------

Started Sonatype Nexus OSS 3.41.1-01

-------------------------------------------------

But we want this application run as service, so do more configuration for systemd, create a file /etc/systemd/system/nexus.service contains:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=nexus service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/root/nexus-3.41.1-01/bin/nexus start
ExecStop=/root/nexus-3.41.1-01/bin/nexus stop
User=root
Restart=on-abort
TimeoutSec=600

[Install]
WantedBy=multi-user.target

then enable and start nexus:

1
2
3
sudo systemctl daemon-reload
sudo systemctl enable nexus.service
sudo systemctl start nexus.service

after that management tool installation finished, let’s start to build internal maven repositories.

Create private repository

First, access sonatype nexus by 8081 port and finish the wizard.

Repository types

According to nexus docs, there are several proxy types for nexus:

  • Proxy repository: is a type linked to remote repository, which act as cache for the local request
  • Hosted repository: local stored and follow maven policy
  • Repository group: combine multi repositories as one

So our usage, we choose maven2 hosted repository to publish our own jar.

Following configurations are used for this repository:

1
2
3
4
5
6
format: maven2
type: hosted
layout policy: strict
content disposition: inline
blob store: default
deployment policy: disable redeploy

And try to upload first jar to our hosted repository

Accesshttp://your_nexus_address/#browse/upload:your_repo this page to uplaod jar with group id and artifact id.

Then, change pom.xml to enable the repository:

1
2
3
4
5
6
7
<repositories>
<repository>
<id>zstack-premium</id>
<name>zstack-premium</name>
<url>http://your_nexus_address/repository/your_repo/</url>
</repository>
</repositories>

But if use maven3 need to update settings to allow http access.

1
2
3
4
5
6
<mirror>
<id>zstack-premium-mirror</id>
<name>zstack-premium</name>
<url>http://your_nexus_address/repository/your_repo/</url>
<mirrorOf>zstack-premium</mirrorOf>
</mirror>

Test mvn install the lib is download as expected.

Deploy project to the repository

Add distribution management into your pom.xml

1
2
3
4
5
6
7
<distributionManagement>
<repository>
<id>your_repo_id</id>
<name>readable name</name>
<url>your_repo_address</url>
</repository>
</distributionManagement>

to set which repo address your deployment should go, then add server related setttings to your .m2/settting.xml

1
2
3
4
5
<server>
<id>your_repo_id</id>
<username>your_username</username>
<password>your_password</password>
</server>

Then use mvn deploy to finish upload.

There are some errors during the configuration phase.

  • Error status code 401, means authentication failure, use mvn -X to check

    [DEBUG] Reading global settings from /opt/maven/conf/settings.xml
    [DEBUG] Reading user settings from /root/.m2/settings.xml
    two configuration files are used, change the global one with the sever section fix the issue.

  • Error status code 400, nexus repository should change the deployment policy to allow redeploy