oc new-app sonatype/nexus
While developing your application with Java and Maven, you will most likely be building many times. In order to shorten the build times of your pods, Maven dependencies can be cached in a local Nexus repository. This tutorial will guide you through creating a Nexus repository on your cluster.
This tutorial assumes that you are working with a project that is already set up for use with Maven. If you are interested in using Maven with your Java project, it is highly recommended that you look at their guide.
In addition, be sure to check your application’s image for Maven mirror
capabilities. Many images that use Maven have a MAVEN_MIRROR_URL
environment
variable that you can use to simplify this process. If it does not have this
capability, read
the Nexus documentation
to configure your build properly.
Furthermore, make sure that you give each pod enough resources to function. You may have to edit the pod template in the Nexus deployment configuration to request more resources.
Download and deploy the official Nexus container image:
oc new-app sonatype/nexus
Create a route by exposing the newly created Nexus service:
oc expose svc/nexus
Use oc get routes to find the pod’s new external address.
oc get routes
The output should resemble:
NAME HOST/PORT PATH SERVICES PORT TERMINATION nexus nexus-myproject.192.168.1.173.xip.io nexus 8081-tcp
Confirm that Nexus is running by navigating your browser to the URL under HOST/PORT. To sign in to Nexus, the default administrator username is admin, and the password is admin123.
Nexus comes pre-configured for the Central Repository, but you may need others for your application. For many Red Hat images, it is recommended to add the jboss-ga repository at Maven repository. |
This is a good time to set up readiness and liveness probes. These will periodically check to see that Nexus is running properly.
$ oc set probe dc/nexus \ --liveness \ --failure-threshold 3 \ --initial-delay-seconds 30 \ -- echo ok $ oc set probe dc/nexus \ --readiness \ --failure-threshold 3 \ --initial-delay-seconds 30 \ --get-url=http://:8081/nexus/content/groups/public
If you do not want persistent storage, continue to Connecting to Nexus. However, your cached dependencies and any configuration customization will be lost if the pod is restarted for any reason. |
Create a persistent volume claim (PVC) for Nexus, so that the cached dependencies are not lost when the pod running the server terminates. PVCs require available persistent volumes (PV) in the cluster. If there are no PVs available and you do not have administrator access on your cluster, ask your system administrator to create a Read/Write Persistent Volume for you. Otherwise, see Persistent Storage in OpenShift Container Platform for instructions on creating a persistent volume.
Add a PVC to the Nexus deployment configuration.
$ oc volumes dc/nexus --add \ --name 'nexus-volume-1' \ --type 'pvc' \ --mount-path '/sonatype-work/' \ --claim-name 'nexus-pv' \ --claim-size '1G' \ --overwrite
This removes the previous emptyDir volume for the deployment config and adds a
claim for one gigabyte of persistent storage mounted at /sonatype-work
, which
is where the dependencies will be stored. Due to the change in configuration,
the Nexus pod will be redeployed automatically.
To verify that Nexus is running, refresh the Nexus page in your browser. You can monitor the deployment’s progress using:
$ oc get pods -w
The next steps demonstrate defining a build that uses the new Nexus repository. The rest of the tutorial uses this example repository with wildfly-100-centos7 as a builder, but these changes should work for any project.
The example builder image supports
MAVEN_MIRROR_URL
as part of its environment, so we can use this to point our
builder image to our Nexus repository. If your image does not support consuming
an environment variable to configure a Maven mirror, you may need to modify the
builder image to provide the correct Maven settings to point to the Nexus
mirror.
$ oc new-build openshift/wildfly-100-centos7:latest~https://github.com/openshift/jee-ex.git \ -e MAVEN_MIRROR_URL='http://nexus.<Nexus_Project>:8081/nexus/content/groups/public' $ oc logs build/jee-ex-1 --follow
Replace <Nexus_Project>
with the project name of the Nexus repository. If it
is in the same project as the application that is using it, you can remove the
<Nexus_Project>.
.
Learn more about DNS resolution in OpenShift Container Platform.
In your web browser, navigate to http://<NexusIP>:8081/nexus/content/groups/public to confirm that it has stored your application’s dependencies. You can also check the build logs to see if Maven is using the Nexus mirror. If successful, you should see output referencing the URL http://nexus:8081.