Publish Jar

How to upload a JAR file to Nexus or artifactory

Even though Codefresh has great support for containers, it can still be used for traditional JAR uploads of libraries or applications that are not dockerized yet. In this example we will compile a JAR and upload it to Nexus. The process is the same for Artifactory or any other package manager.

For a Java application with Docker, see the Gradle or Maven example.

The example Java library project

You can see the example project at https://github.com/codefresh-contrib/plain-jar-sample-lib. The repository contains a simple Java library built with Maven with the following goals:

  • mvn package creates a jar file of the library. It also runs unit tests.
  • mvn upload uploads the jar to a package manager such as Nexus or Artifactory.

We use Nexus for this example. To upload the Jar manually first edit the pom.xml with the URL of the package manager. The project also includes a settings.xml with parameterized credential.

The Nexus package manager

The Nexus package manager

From your workstation you can upload the jar manually with:

mvn -s settings.xml -Dserver.password=my-nexus-user -Dserver.username=my-nexus-pass deploy

If you then visit Nexus you should see your JAR file in the snapshots repository.

Create a CI pipeline for publishing a JAR file

Create a new pipeline in Codefresh and define as parameters your Nexus credentials. You could also use shared configuration or any other credential mechanism you already use in your other pipelines.

Parameters for Nexus

Parameters for Nexus

Then copy/paste the Codefresh YAML file in the pipeline editor. Here are the full contents of the file:

codefresh.yml

version: '1.0'
steps:
  main_clone:
    title: Cloning main repository...
    type: git-clone
    repo: 'codefresh-contrib/plain-jar-sample-lib'
    revision: master
    git: github
  publish_jar:
    title: Upload to nexus
    image: 'maven:3.5.2-jdk-8-alpine'
    commands:
      - mvn -Dmaven.repo.local=/codefresh/volume/m2_repository -s settings.xml -Dserver.password=${{NEXUS_PASS}} -Dserver.username=${{NEXUS_USER}}  deploy

The pipeline starts by checking out the code using a git clone step. The next step is a freestyle one and packages the jar file. We also use the Codefresh volume for caching.

You can define the version of Maven/JDK you want to use by picking the appropriate image from Dockerhub, or using any of your own images (even from external registries).

Note the use of the two user-defined environment variables passed to server.password and server.username. You will need to define those yourself. See the documentation on User Procided Variables.

Publish JAR pipeline

Publish JAR pipeline

Once the pipeline has finished you should see the JAR file in the Nexus browser UI.

Upload finished

Upload finished

You can use the same pipeline for Artifactory or any other compliant Java package registry.

Gradle example
Spring boot example
Codefresh YAML for pipeline definitions
Steps in pipelines
Creating pipelines
How Codefresh pipelines work