This page explains how to use Cloud Build to build and test Java-based applications. If you are new to Cloud Build, read the Cloud Build quickstarts.
Before you begin
- Be familiar with creating Java-based applications.
- Have your Java project ready.
- Be familiar with how to write a Cloud Build configuration file.
- To run the
gcloudcommands in this page, install the
To execute your tasks in the
gradle image, specify the URL of the
image and the tagged version of the image in the
name field of your build
config file. If you don't specify the image tag, Cloud Build
latest image by default. Cloud Build starts the image
name by using the image's default entrypoint. To override the
default entrypoint and to invoke
gradle as an entrypoint, specify
gradle in the
The following build config file specifies the
entrypoint for the
gradle image and prints the build tool version:
steps: - name: maven:3-jdk-8 entrypoint: mvn args: ['--version']
steps: - name: gradle:5.6.2-jdk8 entrypoint: gradle args: ['--version']
In your project root directory, create a build config file named
gradle test, which downloads dependencies, builds the applications, and runs any tests specified in your source code. The
argsfield of a build step takes a list of arguments and passes them to the image referenced by the
In your build config file, add
argsfield to invoke
steps: - name: maven:3-jdk-8 entrypoint: mvn args: ['test']
steps: - name: gradle:5.6.2-jdk8 entrypoint: gradle args: ['test']
Package application: To package your application into a JAR file for your
mavenimage, specify the
packagecommand in the
packagecommand builds a JAR file in
To package your application into a JAR file for your
gradleimage, specify the
assemblecommand in the
assemblecommand builds a JAR file in
The following build step packages your Java application:
steps: - name: maven:3-jdk-8 entrypoint: mvn args: ['package','-Dmaven.test.skip=true']
steps: - name: gradle:5.6.2-jdk8 entrypoint: gradle args: ['assemble']
Containerize application: Cloud Build provides a pre-built Docker image that you can use to containzerize your Java application. To containerize your Java application, in your build config file:
- Add a
namefield and specify the pre-built Docker image, which is stored in Container Registry at
- Add an
argsfield and specify the
buildargument, the name of the container image to build, and the path to your build artifact in
- Add an
imagesfield to push the built image to Container Registry.
The following build step containerizes your application and pushes your image to Container Registry:
steps: - name: gcr.io/cloud-builders/docker args: ['build', '-t', 'gcr.io/project-id/image-name', '--build-arg=JAR_FILE=target/build-artifact'] images: ['gcr.io/project-id/image-name']
steps: - name: gcr.io/cloud-builders/docker args: ['build', '-t', 'gcr.io/project-id/image-name', '--build-arg=JAR_FILE=build/libs/build-artifact'] images: ['gcr.io/project-id/image-name']
- project-id: the id of your Cloud project.
- image-name: the name of your container image.
- build-artifact: the name of your JAR file created from your build step.
- Add a
Start your build: When you have your build config file ready, start your build by entering the following command in your terminal:
gcloud builds submit --config config-file-path source-directory
- config-file-path: the path to your build config file. In this example,
the build config file is named
- source-directory: the path or URL to your source code.
If you don't specify a config-file-path and source-directory in the
gcloud builds submitcommand, Cloud Build assumes that the config file and the source code are in the current working directory.
- config-file-path: the path to your build config file. In this example, the build config file is named
Here are some example repositories you can use to build Java apps, each of which contain a sample application and a build config file to build and test that application:
- maven-example: A Java app and an example build config file to build and test
the app with
- gradle-example: A Java app and an example build config file to build and test
the app with