Tutorial : Build and test Java applications

 This page explains how to use Cloud Build to build and test Java-based applications, store built artifacts in a Maven repository in Artifact Registry, and generate build provenance information.

You can configure Cloud Build to build Java applications using the maven image from Docker Hub.

To execute your tasks in the maven 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 uses the latest image by default. Cloud Build starts the image specified in name by using the image's default entrypoint. To override the default entrypoint and to invoke maven as an entrypoint, specify mvn in the entrypoint field.

The following build config file specifies the entrypoint for the maven image and prints the build tool version:

          steps:
         
- name: maven:3.3-jdk-8
            entrypoint
: mvn
            args
: ['--version']
  1. In your project root directory, create a build config file named cloudbuild.yaml.

  2. Run testsmaven provides maven test, which downloads dependencies, builds the applications, and runs any tests specified in your source code. The args field of a build step takes a list of arguments and passes them to the image referenced by the name field.

    In your build config file, add test to the args field to invoke test within maven:

         steps:
         
    - name: maven:3.3-jdk-8
           entrypoint
    : mvn
           args
    : ['test']
  3. Package application: To package your application into a JAR file for your maven image, specify the package command in the args field. The package command builds a JAR file in /workspace/target/.

    The following build step packages your Java application:

         steps:
         
    - name: maven:3.3-jdk-8
           entrypoint
    : mvn
           args
    : ['package','-Dmaven.test.skip=true']
  4. Upload to Artifact Registry:

    Cloud Build generates Supply chain Levels for Software Artifacts (SLSA) build provenance information for standalone Maven packages when you upload artifacts to Artifact Registry using the mavenArtifacts field in your Cloud Build config file.

    In your build config file, use the mavenArtifacts field to specify your application file path and your Maven repository in Artifact Registry:

    artifacts:
      mavenArtifacts
    :
     
    - repository: 'https://location-maven.pkg.dev/project-id/repository-name'
        path
    : 'app-path'
        artifactId
    : 'build-artifact'
        groupId
    : 'group-id'
        version
    : 'version'

    Replace the following values:

    • location: the location for your repository in Artifact Registry.
    • project-id: the ID of the Google Cloud project that contains your Artifact Registry repository.
    • repository-name: the name of your Maven repository in Artifact Registry.
    • app-path: the path to your packaged application.
    • build-artifact: the name of your package file created from your build step.
    • group-id: uniquely identifies your project across all Maven projects, in the format com.mycompany.app. For more information, see the Maven guide to naming conventions.
    • version: the version number for your application, formatted in numbers and dots like 1.0.1.
  5. Optional: Enable provenance for regional builds

    If you are using a regional build, add the requestedVerifyOption field in the options in your build config file. Set the value to VERIFIED to enable provenance metadata generation. If you don't add requestedVerifyOption: VERIFIED, Cloud Build generates provenance for global builds only.

    options:
      requestedVerifyOption
    : VERIFIED
  6. Start your buildmanually or using build triggers.

    Once your build completes, you can view repository details in Artifact Registry.

    You can also view build provenance metadata and validate provenance to help protect your software supply chain.


Build, test, and containerize Java applications :

This page explains how to use Cloud Build to build, test, and containerize Java-based applications, upload your container images to Artifact Registry, generate build provenance.

You can configure Cloud Build to build Java applications using the maven image or the gradle image from Docker Hub.

To execute your tasks in the maven or 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 uses the latest image by default. Cloud Build starts the image specified in name by using the image's default entrypoint. To override the default entrypoint and to invoke maven or gradle as an entrypoint, specify mvn or gradle in the entrypoint field.

The following build config file specifies the entrypoint for the maven or gradle image and prints the build tool version:

  steps:
 
- name: maven:3.3-jdk-8
    entrypoint
: mvn
    args
: ['--version']
  1. In your project root directory, create a build config file named cloudbuild.yaml.

  2. Run testsmaven and gradle provide maven test and gradle test, which downloads dependencies, builds the applications, and runs any tests specified in your source code. The args field of a build step takes a list of arguments and passes them to the image referenced by the name field.

    In your build config file, add test to the args field to invoke test within maven and gradle:

     steps:
     
    - name: maven:3.3-jdk-8
       entrypoint
    : mvn
       args
    : ['test']
  3. Package application: To package your application into a JAR file for your maven image, specify the package command in the args field. The package command builds a JAR file in /workspace/target/.

    To package your application into a JAR file for your gradle image, specify the assemble command in the args field. The assemble command builds a JAR file in workspace/build/libs.

    The following build step packages your Java application:

     steps:
     
    - name: maven:3.3-jdk-8
       entrypoint
    : mvn
       args
    : ['package','-Dmaven.test.skip=true']
  4. Containerize application: Cloud Build provides a pre-built Docker image that you can use to containerize your Java application. To containerize your Java application, in your build config file:

    • Add a name field and specify the pre-built Docker image at gcr.io/cloud-builders/docker.
    • Add an args field and specify the build arguments, including the name of the container image to build, and the path to your build artifact.
    • Add an images field to push the built container image to Artifact Registry and generate build provenance information. If you do not use the images field, Cloud Build will not record provenance information. For example, if you use a docker push build step to upload images to Artifact Registry, your build will not have associated provenance information.

    The following build step containerizes your application and pushes your container image to Artifact Registry, and generates build provenance information.

    steps:
    - name: gcr.io/cloud-builders/docker
      args
    : ['build', '-t', 'location-docker.pkg.dev/project-id/repository/image', '--build-arg=JAR_FILE=target/build-artifact', '.']
    images
    : ['location-docker.pkg.dev/project-id/repository/image']

    Where:

    • location: the regional or multi-regional location for your repository.
    • project-id: the id of your Google Cloud project.
    • repository: the name of your repository Artifact Registry.
    • image: the name of your container image.
    • build-artifact: the name of your JAR file created from your build step.
  5. Optional: Enable provenance for regional builds

    If you are using a regional build, add the requestedVerifyOption field in the options in your build config file. Set the value to VERIFIED to enable provenance metadata generation. If you don't add requestedVerifyOption: VERIFIED, Cloud Build generates provenance for global builds only.

    options:
      requestedVerifyOption
    : VERIFIED
  6. 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 --region=REGION --config config-file-path source-directory

    Where:

    • config-file-path: the path to your build config file. In this example, the build config file is named cloudbuild.yaml.
    • source-directory: the path or URL to your source code.
    • REGION: one of the supported build regions.

    If you don't specify a config-file-path and source-directory in the gcloud builds submit command, Cloud Build assumes that the config file and the source code are in the current working directory.

    Once your build completes, you can view repository details in Artifact Registry.

    You can also view build provenance metadata and validate provenance to help protect your software supply chain.

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 mvn.
  • gradle-example: A Java app and an example build config file to build and test the app with gradle.

Comments

Popular posts from this blog

Terraform

Scrum Master Interview help - Bootcamp

Kubernetes