This is the part of my Devops learning journey.
Setup Nexus Server in DigitalOcean
We use Digitalocean for the demo experiments.
Environment: Ubuntu 22.10
Size: 8GB / 4 CPUs
SSH for connection
apt update
install java 8 and tools
apt install openjdk-8-jre-headless
apt install net-tools
Once we parepare the server, we install the Nexus with curl
cd /opt
wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz
tar -zxvf latest-unix.tar.gz
root@ubuntu-s-4vcpu-8gb-fra1-01:/opt# ls
digitalocean latest-unix.tar.gz nexus-3.43.0-01 sonatype-work
After this we get two folders,
nexus-* : the runtime folder
sonatype-work: own configuration for nexus and data
We should never run the service or application with root user, instead the best practice is to create the service user. And then we change the owners and groups of the nexus folder to nexus.
adduser nexus
chown -R nexus:nexus nexus-3.43.0-01
chown -R nexus:nexus sonatype-work
root@ubuntu-s-4vcpu-8gb-fra1-01:/opt# ls -l
total 205432
drwxr-xr-x 4 root root 4096 Nov 26 06:13 digitalocean
-rw-r--r-- 1 root root 210342986 Nov 7 14:44 latest-unix.tar.gz
drwxr-xr-x 10 nexus nexus 4096 Nov 26 06:23 nexus-3.43.0-01
drwxr-xr-x 3 nexus nexus 4096 Nov 26 06:23 sonatype-work
Then we shift to the nexus user and run the nexus service
su - nexus
/opt/nexus-3.43.0-01/bin/nexus start
Vlidate the service is running with "ps aux | grep nexus" and "netstat -lnpt"
nexus@ubuntu-s-4vcpu-8gb-fra1-01:~$ ps aux | grep nexus
root 3067 0.0 0.0 9744 3924 pts/3 S 06:47 0:00 su - nexus
nexus 3068 0.1 0.0 9084 5360 pts/3 S 06:47 0:00 -bash
nexus 3283 238 14.9 6368260 1219048 pts/3 Sl 06:47 0:54 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -server -Dinstall4j.jvmDir=/usr/lib/jvm/java-8-openjdk-amd64/jre -Dexe4j.moduleName=/opt/nexus-3.43.0-01/bin/nexus -XX:+UnlockDiagnosticVMOptions -Dinstall4j.launcherId=245 -Dinstall4j.swt=false -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m -XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=../sonatype-work/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=../sonatype-work/nexus3 -Dkaraf.log=../sonatype-work/nexus3/log -Djava.io.tmpdir=../sonatype-work/nexus3/tmp -Dkaraf.startLocalConsole=false -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=lib/endorsed -Di4j.vpt=true -classpath /opt/nexus-3.43.0-01/.install4j/i4jruntime.jar:/opt/nexus-3.43.0-01/lib/boot/nexus-main.jar:/opt/nexus-3.43.0-01/lib/boot/activation-1.1.1.jar:/opt/nexus-3.43.0-01/lib/boot/jakarta.xml.bind-api-2.3.3.jar:/opt/nexus-3.43.0-01/lib/boot/jaxb-runtime-2.3.3.jar:/opt/nexus-3.43.0-01/lib/boot/txw2-2.3.3.jar:/opt/nexus-3.43.0-01/lib/boot/istack-commons-runtime-3.0.10.jar:/opt/nexus-3.43.0-01/lib/boot/org.apache.karaf.main-4.3.6.jar:/opt/nexus-3.43.0-01/lib/boot/osgi.core-7.0.0.jar:/opt/nexus-3.43.0-01/lib/boot/org.apache.karaf.specs.activator-4.3.6.jar:/opt/nexus-3.43.0-01/lib/boot/org.apache.karaf.diagnostic.boot-4.3.6.jar:/opt/nexus-3.43.0-01/lib/boot/org.apache.karaf.jaas.boot-4.3.6.jar com.install4j.runtime.launcher.UnixLauncher start 9d17dc87 0 0 org.sonatype.nexus.karaf.NexusMain
nexus 3578 0.0 0.0 10264 3208 pts/3 R+ 06:47 0:00 ps aux
nexus 3579 0.0 0.0 6852 2060 pts/3 R+ 06:47 0:00 grep --color=auto nexus
nexus@ubuntu-s-4vcpu-8gb-fra1-01:~$ netstat -lnpt
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.54:53 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 3283/java
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:40569 0.0.0.0:* LISTEN 3283/java
tcp6 0 0 :::22 :::* LISTEN -
The nexus is running at port 8081 and we can reach the application with IP and Port.
The admin user password is at /opt/sonatype-work/nexus3/admin.password
Publish Artifact to Nexus Repository
Create Nexus Local User
Give the nx-anonymous for the first time.
Create and Assign Roles
There are two types of roles in Nexus
nx-repository-view-* : upload the artifacts. nx-repository-admin-* : administrators of Nexus who take cares of backups, plugins and etc..
In our case, we assign the nx-repository-view-* role to the local developer user. More specifically, we assign the role of repo maven2-maven-snapshots to the local user: nx-repository-view-maven2-maven-snapshots-*
Configure Gradle & Maven to connect to Nexus
Once we setup the local user and assigned the proper roles. We are going to deploy the Java Applications to the repository. We will try Gradle and Maven both.
Gradle
Add the following codes to the build.gradle and add the gradle.properties file for the variables repoUser and repoPassword
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication){
artifact("build/libs/my-app-$version"+".jar"){
extension 'jar'
}
} }
repositories {
maven{
name 'nexus'
url "http://165.232.32.170:8081/repository/maven-snapshots/"
allowInsecureProtocol = true
credentials {
username project.repoUser
password project.repoPassword
}
} }}
Go to setting.gradle file and set "rootProject.name = 'my-app'."
Go to project folder and run "./gradlew build" to compile it. And then run "./gradlew publish" which will publish the code to our configured Nexus repository.
Maven
Go to pom.xml and add the following parts.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
</plugins>
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://165.232.32.170:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
Add the server config in the maven.
cd ~/.m2
vim settings.xml to create the file settings.xml
Here the id(nexus-snapshots) is defined in the pom.xml.
Compile the maven project.
go to project folder
mvn package, it creates the build folder
mvn deploy, this deploys the maven artifact to the folder
Check in Nexus Repository Manager
Summary
In this post, we setup the Nexus server in the cloud(Digitalocean), configured the local dev user. Then we published the Java application with both Maven and Gradle versions to the repository. Ideally as part of the CICD, we should use the docker image other than deploy it manually in the server. In the coming posts, we will try to achieve that part.