Install Nexus Repository Manager in Linux Centos7, Deploy and Use 'artifacts
Tags: codenewbie, webdev, java, nexus
Published: 2022-08-01 10:58:00


Step 1: Login to your Linux server and update the yum packages. Also install required utilities.

```shell
yum update -y
sudo yum install wget -y
java -version
sudo yum install java-1.8.0-openjdk.x86_64 -y
```

Step 2: Download the latest nexus. You can get the latest download links fo for nexus from here.

```shell
cd /opt
sudo wget -O latest-unix.tar.gz https://download.sonatype.com/nexus/3/latest-unix.tar.gz
tar -xvzf latest-unix.tar.gz
sudo mv nexus-3* nexus
mv sonatype-work nexusdata
ls -lh
```

Step 3: Set User/Permissions and Configurations


```shell
useradd --system --no-create-home nexus
chown -R nexus:nexus /opt/nexus
chown -R nexus:nexus /opt/nexusdata
```

Edit `/opt/nexus/bin/nexus.vmoptions` file

    vi /opt/nexus/bin/nexus.vmoptions

```text
-Xms2703m
-Xmx2703m
-XX:MaxDirectMemorySize=2703m
-XX:+UnlockDiagnosticVMOptions
-XX:+LogVMOutput
-XX:LogFile=../nexusdata/nexus3/log/jvm.log
-XX:-OmitStackTraceInFastThrow
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc/karaf
-Djava.util.logging.config.file=etc/karaf/java.util.logging.properties
-Dkaraf.data=../nexusdata/nexus3
-Dkaraf.log=../nexusdata/nexus3/log
-Djava.io.tmpdir=../nexusdata/nexus3/tmp
-Dkaraf.startLocalConsole=false
```

Edit `nexus.rc` file.

    vi /opt/nexus/bin/nexus.rc

Uncomment `run_as_user` parameter and add new value.

    run_as_user="nexus"

We need to modify the `nexus-default.properties` file.

    vi /opt/nexus/etc/nexus-default.properties

Change `application-host=0.0.0.0` and port `application-host=9081`


Configure the open file limit of the nexus user.

    vi /etc/security/limits.conf

Add the below values to the file.

    nexus - nofile 65536


Step 4: Set Nexus as a System Service

Create the Systemd service file in `/etc/systemd/system/`.

    sudo vi /etc/systemd/system/nexus.service

Add the following contents to the unit file.

```text
[Unit]
Description=Nexus Service
After=syslog.target network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=nexus
Group=nexus
Restart=on-failure

[Install]
WantedBy=multi-user.target
```

Manage Nexus Service, Execute the following command to add nexus service to boot.

    systemctl daemon-reload
    systemctl enable nexus.service
    systemctl start nexus.service

Monitor the log file.

    tail -f /opt/nexusdata/nexus3/log/nexus.log

Check the running service port.

    netstat -tunlp | grep 9081

Show default login password.

    cat /opt/nexusdata/nexus3/admin.password




## Deploy and Use Artifacts in Spring Boot Project

Create or Edit this `~/.m2/settings.xml` file:

    vi ~/.m2/settings.xml

Update contents of the file.

```xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">

  <!--  unblock so that we can download artifacts from nexus repo  -->
  <mirrors>
    <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>dummy</mirrorOf>
      <name>Dummy mirror to override default blocking mirror that blocks http</name>
      <url>http://0.0.0.0/</url>
    </mirror>
  </mirrors>


  <!--nexus server configuration-->
  <servers>
    <server>
      <id>nexus</id>
      <username>admin</username>
      <password>19e36q29-1e09-42ce-af29-4f74563a7467</password>
    </server>
  </servers>

</settings>
```

### Deploy jar or artifact to nexus server

1. Now create a maven project. Open `pom.xml` file, and add this content inside `project` tag.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  
  <!-- ...other  -->
  
  <!--  deploy libs to nexus repository -->
  <distributionManagement>
    <repository>
      <id>nexus</id>
      <url>http://119.168.0.0:9081/repository/sadhan</url>
      <!--<name>Nexus Releases</name>-->
    </repository>
    <snapshotRepository>
      <id>nexus</id>
      <url>http://119.168.0.0:9081/repository/sadhan</url>
      <!--<name>Nexus Snapshot</name>-->
    </snapshotRepository>
  </distributionManagement>
  
</project>
```

Now hit the following command to deploy jar to nexus repository.

```shell
mvn deploy        
mvn deploy -e        
mvn deploy -e release

mvn deploy -DaltDeploymentRepository=nexus::default::http://119.168.0.0:9081/repository/sadhan
```


### Use and download jar/artifact from nexus server

2. Now pull the jar file from nexus repository.

Makes sure `~/.m2/setting.xml` file copied properly.

Now open `pom.xml` file and add this upload dependency inside `dependencies` tag.

```xml
<dependency>
  <groupId>com.revesoft</groupId>
  <artifactId>olm</artifactId>
  <version>0.0.1</version>
</dependency>
```

Then add this below content inside `project` tag.

```xml
  <!--to use nexus repositories we need that -->
  <repositories>
    <repository>
      <id>nexus</id>
      <url>http://119.168.0.0:9081/repository/sadhan/</url>
      <!--<name>Nexus Releases</name>-->
    </repository>
  </repositories>
```

Using CLI, we can download the jar file from nexus repository.

```shell
mvn dependency:resolve -DremoteRepositories=nexus::default::http://119.168.0.0:9081/repository/sadhan 
```


> Note: Here id nexus is the same as in `~/.m2/settings.xml` file. so use same it to download the jar file.


# Optional 

Upload blob file to nexus repository.

```
curl -v --user 'admin:admin123' --upload-file ./test.png http://localhost:9081/repository/documentation/test.png
```

## Congratulations

I hope we learn something exciting about Nexus. Thanks for time & passion. Feel free to ask me anything.


Say Hi to me on [Twitter](https://twitter.com/eng_sadhan), [Linkedin](https://www.linkedin.com/in/csesadhan/), and [Medium](https://medium.com/@csesadhan) where I keep on sharing interesting updates.


References
- https://help.sonatype.com/repomanager3/nexus-repository-administration/formats/maven-repositories