For the complete documentation index, see llms.txt. This page is also available as Markdown.

Java

Use a CI pipeline to build and test a Java application.

You can build and test a Java application using a Linux platform on Harness Cloud or a self-managed Kubernetes cluster build infrastructure.

This guide assumes you've created a Harness CI pipeline.

Install dependencies

Use Run steps to install dependencies in the build environment.

              - step:
                   type: Run
                   name: build
                   identifier: build
                   spec:
                     connectorRef: YOUR_IMAGE_REGISTRY_CONNECTOR
                     image: maven:3.8-jdk-11
                     shell: Sh
                     command: |-
                       mvn clean package dependency:copy-dependencies
                   - step:
                       type: Run
                       name: check dependencies
                       identifier: check_dependencies
                       spec:
                         connectorRef: YOUR_IMAGE_REGISTRY_CONNECTOR
                         image: maven:3.8-jdk-11
                         shell: Sh
                         command: |-
                           mvn dependency-check:check -U -DskipTests
                         reports:
                           type: JUnit
                           spec:
                             paths:
                               - /harness/target/*.xml

In addition to Run steps, Plugin steps are also useful for installing dependencies.

You can use Background steps to run dependent services that are needed by multiple steps in the same stage.

Cache dependencies

Cache your Java dependencies with Cache Intelligence.

Add caching.enabled.true to your stage.spec:

You can use built-in steps to:

MAVEN CACHE KEY AND PATH REQUIREMENTS

If you're using Maven, you must reference pom.xml in the key value for your Save Cache and Restore Cache steps, for example:

Additionally, you must include /root/.m2 in the sourcePaths for your Save Cache step, for example:

Here's an example of a pipeline with Save Cache to S3 and Restore Cache from S3 steps.

Build and run tests

You can use Run or Test steps to run tests in CI pipelines.

This example uses two Run steps to build and test with Maven.

You must use the Test step for your unit tests if you want to leverage Harness' Test Intelligence feature.

Visualize test results

If you want to view test results in Harness, make sure your test commands produce reports in JUnit XML format and that your steps include the reports specification.

Test splitting

Harness CI supports test splitting (parallelism) for both Run and Test steps.

Specify version

Java is pre-installed on Hosted Cloud runners. For details about all available tools and versions, go to Platforms and image specifications.

If your application requires a specific Java version, add a Run step to install it.

Install a specific version of Java
Install multiple Java versions
  1. Add the matrix looping strategy configuration to your stage.

  1. Reference the matrix variable in your steps.

Specify the desired Java Docker image tag in your steps. There is no need for a separate install step when using Docker.

Use a specific Java version
Use multiple Java versions
  1. Add the matrix looping strategy configuration to your stage.

  1. Reference the matrix variable in the image field of your steps.

Full pipeline examples

Here's a YAML example of a pipeline that:

  1. Tests a Java code repo.

  2. Builds and pushes an image to Docker Hub.

This pipeline uses Harness Cloud build infrastructure, Cache Intelligence, and Test Intelligence.

If you copy this example, replace the placeholder values with appropriate values for your Harness project, connector IDs, account/user names, and repo names.

Pipeline YAML

Next steps

Now that you have created a pipeline that builds and tests a Java app, you could:

Last updated

Was this helpful?