Help Center/ CodeArts Build/ Best Practices/ Running a Multi-Task Maven Build Project (Built-in Executors, Code)
Updated on 2024-11-21 GMT+08:00

Running a Multi-Task Maven Build Project (Built-in Executors, Code)

Scenario

A build task is the smallest unit that a build project can be broken down into for simple service scenarios. However, for more complex requirements, you may need to set up multiple build tasks in a modular and fine-grained way, and run them in a specific order. This means that each task depends on the successful completion of its dependency task.

To handle such complex builds, CodeArts Build offers a task model called BuildFlow, which organizes multiple build tasks in a directed acyclic graph (DAG) and runs them in parallel based on their dependencies.

In this practice, your build project involves three build tasks (Job1, Job2, and Job3). Job3 depends on Job1 and Job2. Your code is stored in CodeArts Repo repositories.

Requirements

  • You can only use the code hosted in CodeArts Repo for a BuildFlow task.
  • You have permissions for CodeArts Repo.

Procedure

Table 1 Steps

Step

Description

Creating a Project

Create a project.

Creating a CodeArts Repo Repository

Create a CodeArts Repo repository to store code files.

Creating a build.yml File

Define the entire build process in build.yml.

Creating Task Scripts Used in build.yml

Create a script for the whole build.

Creating and Running a Build Task

Create and run a build flow task.

Viewing the Build Results

View the build results.

Creating a Project

  1. Log in to the Huawei Cloud console with your Huawei Cloud account.
  2. Click in the upper left corner and choose Developer Services > CodeArts from the service list.
  3. Click Access Service. The homepage of CodeArts is displayed.
  4. Click Create Project, and select the Scrum template.
  5. Set the project name to build-bestpractice, and leave the other parameters as default.
  6. Click OK to access the project.

Creating a CodeArts Repo Repository

  1. In the navigation pane, choose Code > Repo.
  2. On the displayed page, click New Repository. Select Template, and click Next.
  3. On the template selection page, select the Java Maven Demo template and click Next.
  4. On the displayed page, set Repository Name to Repo01 and keep the default values for other parameters.
  5. Click OK.

Creating a build.yml File

  1. On the code repository details page, click Create and select Create Directory from the drop-down list.
  2. Name the directory as .cloudbuild and describe the files it contains. Click OK.
  3. In the .cloudbuild directory, click Create and select Create File from the drop-down list. Name the file as build.yml and add the following code to the file:

    version: 2.0 # The version number is a mandatory and unique parameter that must be set to 2.0.
    params: # Parameters that can be referenced by builds.
      - name: condition_param
        value: 1
    # The envs configuration is optional.
    envs: 
      - condition: condition_param == 0 # The following host specification and type are not used if this condition is not met.
        resource:
          type: docker 
          arch: ARM
      - condition: condition_param == 1 # The following host specification and type are used if this condition is met.
        resource:
          type: docker
          arch: X86
    
    buildflow:
      jobs: # Build tasks
        - job: Job3 # Define the task name.
          depends_on: # Define the task dependency. In this practice, the configuration indicates that Job3 depends on Job1 and Job2.
            - Job1
            - Job2
          build_ref: .cloudbuild/build_job3.yml # Define the YAML build script to run for Job3.
        - job: Job1
          build_ref: .cloudbuild/build_job1.yml # Define the YAML build script to run for Job1.
        - job: Job2
          build_ref: .cloudbuild/build_job2.yml # Define the YAML build script to run for Job2.

    build.yml defines the entire build process, with three build tasks currently specified. Job3 depends on Job1 and Job2 and will be run only after they are completed. Job1 and Job2 have equal priority and will be triggered at the same time. build_ref defines the build script to be run for each build task.

Creating Task Scripts Used in build.yml

  1. In the .cloudbuild directory, click Create and select Create File from the drop-down list. Name the file as build_job1.yml and add the following code to the file:

    version: 2.0
    steps:
      BUILD:
      - maven:
          image: cloudbuild@maven3.5.3-jdk8-open # The image is used for your build and can be customized.
          inputs:
            settings:
              public_repos:
                - https://mirrors.huawei.com/maven # Configure the dependency repository.
            cache: true # Determine whether to enable caching.
            command: mvn package -Dmaven.test.failure.ignore=true -U -e -X -B # The command to be run.

  2. Create build_job2.yml and build_job3.yml with the same code samples by following the instructions in 1.
  3. The following figure shows the directory that stores files of the code repository.

    Figure 1 Directory

Creating and Running a Build Task

  1. In the navigation pane, choose CICD > Build.
  2. Click Create Task. On the displayed Basic Information page, set parameters according to Table 2.

    Table 2 Basic information

    Parameter

    Description

    Name

    Assign a custom name to the build task, for example, BuildFlow.

    Code Source

    Select Repo.

    Repository

    Select Repo01, the code repository created in Creating a CodeArts Repo Repository.

    Default Branch

    Keep the default value master.

    Description

    Enter additional information to describe the build task.

  3. Click Next and select the Maven template. Click OK. The Build Actions page is displayed.
  4. Click the Code tab. The build scripts in the Repo01 code repository will be loaded automatically.
  5. Click Save and Run. In the displayed dialog box, click Confirm. The build task run page is displayed.

Viewing the Build Results

The Build Processes tab page shows the complete flowchart of the current build tasks. While the build tasks are still running, you can see that Job1 and Job2 are running in parallel, and Job3 only runs after Job1 and Job2 are finished.

  1. On the Build Processes tab page, click either the Job1 node in the left-hand menu or the green Job1 rectangle in the right pane. The run details page of Job1 is displayed, where you can view the build logs of Job1, as shown in Figure 2.

    Figure 2 Viewing the build results

    The details page includes the following tabs:

    • The Actions tab shows the resource scheduling information and sequential position of this task within the whole build process.
    • The Build Parameters tab shows the global parameters of the current build task.
    • The Build Scripts tab shows the script run for the current build task.

  2. You can view the run details of Job2 and Job3 by following instructions in 1.