This guide will help you get started with a Java project on Semaphore. If you’re new to Semaphore please read our Guided tour first.
Table of contents:
# .semaphore/semaphore.yml version: v1.0 name: Java example agent: machine: type: e1-standard-2 os_image: ubuntu1804 blocks: - name: Hello world task: jobs: - name: Run java commands: - java --version - mvn -v
Example project with Spring Boot and Docker
Semaphore provides a tutorial and demo application with a working CI/CD pipeline that you can use to get started quickly:
Supported Java versions
Semaphore supports all versions of Java. You have the following options:
- Linux: Java and related tools are available out-of-the-box in the Ubuntu 18.04 VM image.
- Docker: use semaphoreci/openjdk or your own Docker image with the version of Java and other packages that you need.
Follow the links above for details on currently available language versions and additional tools.
Selecting a Java version on Linux
For example, in your
sem-version java 11
If the version of Java that you need is not currently available in the Linux VM, we recommend running your jobs in a custom Docker image.
Here's an example of caching dependencies using Maven. You may also cache compiled code and tests as well. The exact implementation varies depending on your tools. In the following configuration example, we download dependencies, compile code and warm the cache in the first block, then use the cache in subsequent blocks.
version: v1.0 name: Java & Maven Example agent: machine: type: e1-standard-2 os_image: ubuntu1804 blocks: - name: Setup task: env_vars: # Set maven to use a local directory. This is required for # the cache util. It must be set in all blocks. - name: MAVEN_OPTS value: "-Dmaven.repo.local=.m2" jobs: - name: Dependencies commands: - checkout - cache restore maven-$SEMAPHORE_GIT_BRANCH-$(checksum pom.xml),maven-$SEMAPHORE_GIT_BRANCH,maven-master # Download all JARs possible and compile as much as possible # Use -q to reduce output spam - mvn -q dependency:go-offline test-compile - cache store maven-$SEMAPHORE_GIT_BRANCH-$(checksum pom.xml) .m2 - cache store build-$SEMAPHORE_GIT_SHA target - name: Tests task: env_vars: - name: MAVEN_OPTS value: "-Dmaven.repo.local=.m2" prologue: commands: - checkout - cache restore maven-$SEMAPHORE_GIT_BRANCH-$(checksum pom.xml),maven-$SEMAPHORE_GIT_BRANCH,maven-master - cache restore build-$SEMAPHORE_GIT_SHA jobs: - name: Everything commands: # Again, -q to reduce output spam. Replace with command # that executes tests - mvn -q package