Java
This guide will help build Java projects on Semaphore.
Overview
Java and related tools are pre-installed in Linux machines. You can switch the active compiler using sem-version.
How to change Java versions
Java is available on Linux Ubuntu machines. To change active Java versions use sem-version
. For example to switch to v11:
sem-version java 11
See artifacts to learn how to save and persist the built JARs.
Using Docker containers
Semaphore does not distribute pre-build images for Java. You, can, however, build your own images and use them with Docker Environments.
See the semaphoreci/docker-images repository for examples of Dockerfiles.
How to cache dependencies in Java
The cache tool automatically detects the presence of Maven directories and cache dependencies.
The first job in the pipeline must cache the dependencies:
export MAVEN_OPTS=-Dmaven.repo.local=.m2
checkout
cache restore
mvn -q dependency:go-offline test-compile
cache store
The rest of the jobs can use the cache directly to retrieve dependencies and compiled code:
export MAVEN_OPTS=-Dmaven.repo.local=.m2
checkout
cache restore
How to set up test reports
This section explains how to set up test reports (and flaky tests) for Java and Maven.
-
Add the Surefire plugin to the
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<reportFormat>xml</reportFormat>
<outputDirectory>/tmp</outputDirectory>
</configuration>
</plugin>
</plugins>
</build> -
Run the tests
mvn test
-
Create an after_pipeline job with the following command:
test-results publish /tmp/junit.xml