Desplegar Artefactos Java Maven en Nexus con OpenShift Pipelines (Tekton)

OpenShiftNexusTektonCI/CDMavenKubernetes
Publicado el 2/16/2025
Fecha de ultima actualización: 2/16/2025

En esta guía, aprenderás a configurar y ejecutar un pipeline en OpenShift Pipelines (Tekton) para compilar y desplegar artefactos Java en un repositorio Nexus.

Pasos de configuración

1. Configurar el pom.xml

Agrega la siguiente configuración en el pom.xml de tu aplicación Java:

pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>artefacto</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>artefacto</name>
<distributionManagement>
<repository>
<id>nexus</id>
<name>Releases</name>
<url>http://nexusrepo-sonatype-nexus-service.cicd.svc.cluster.local:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Snapshot</name>
<url>http://nexusrepo-sonatype-nexus-service.cicd.svc.cluster.local:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<properties>
<maven-deploy-plugin.version>2.8.1</maven-deploy-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<configuration>
<nexusUrl>http://nexusrepo-sonatype-nexus-service.cicd.svc.cluster.local:8081</nexusUrl>
<serverId>nexus</serverId>
<skipStaging>true</skipStaging>
</configuration>
</plugin>
</plugins>
</build>
</project>

2. Subir el proyecto a un repositorio Git

El código debe estar almacenado en un repositorio Git para que Tekton pueda acceder a él.

3. Crear el pipeline en OpenShift Pipelines (Tekton)

pipeline.yaml
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: artefacto
spec:
params:
- name: GIT_REPO
type: string
- name: APP_NAME
type: string
tasks:
- name: git-clone
taskRef:
kind: ClusterTask
name: git-clone
params:
- name: url
value: $(params.GIT_REPO)
- name: revision
value: main
- name: maven-package
taskRef:
kind: ClusterTask
name: maven
params:
- name: MAVEN_IMAGE
value: registry.redhat.io/ubi8/openjdk-21
- name: GOALS
value:
- package
- deploy
- name: CONTEXT_DIR
value: $(params.APP_NAME)
runAfter:
- git-clone

El goal deploy es el que permite subir el artefacto al repositorio Nexus.

4. Crear el archivo de configuración de Maven

Crea un ConfigMap con la configuración de Maven (settings.xml):

settings.xml
<settings>
<localRepository>/workspace/source/maven/.m2</localRepository>
<mirrors>
<mirror>
<id>nexus</id>
<name>Default mirror</name>
<url>http://nexusrepo-sonatype-nexus-service.cicd.svc.cluster.local:8081/repository/maven-public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>...</password>
</server>
</servers>
</settings>

Aplica el ConfigMap en OpenShift:

cmd
oc create configmap maven-settings --from-file=settings.xml -n cicd

5. Ejecutar el pipeline

Para ejecutar el pipeline hacer click en el botón de Start en la interfaz de OpenShift Pipelines.

Consideraciones

  • El pipeline usa un mirror predeterminado en Nexus para resolver dependencias de Maven.
  • La configuración del servidor en settings.xml permite la autenticación con Nexus.
  • Asegúrate de reemplazar la contraseña en el settings.xml antes de ejecutar el pipeline.

Con estos pasos, puedes desplegar artefactos Maven en Nexus utilizando OpenShift Pipelines (Tekton).

Conclusiones

Implementar un pipeline en OpenShift Pipelines (Tekton) para desplegar artefactos en Nexus permite automatizar la entrega de artefactos de manera eficiente y confiable. Además, la configuración de Maven con un settings.xml adecuado garantiza la autenticación y el uso correcto del repositorio de artefactos. Esta solución es escalable y puede adaptarse a múltiples proyectos dentro de un clúster de OpenShift.