Help Center/ CodeArts Build/ Best Practices/ Code-based Build/ Uploading Software Packages with Maven
Updated on 2024-04-18 GMT+08:00

Uploading Software Packages with Maven

When to Use

CodeArts Build allows you to configure build scripts using YAML files. You can use YAML syntax to compile the build environment, parameters, commands, and steps into a build.yml file, which can be stored in a code repository with the built code. The system uses the build.yml file as the build script to execute the build task, making the build process traceable, recoverable, secure, and reliable. The build with Maven is used as an example in this section.

Prerequisites

A project is available. If no project is available, create one.

Creating a Code Repository

  1. Log in to the CodeArts homepage using the Huawei Cloud account.
  2. Click the name of the project to create a repository for it.
  3. In the navigation pane, choose Code > Repo, as shown in Figure 1.

    Figure 1 CodeArts Repo

  4. Click New Repository.
  5. Set parameters based on Table 1 and click OK.

    Table 1 Creating a code repository

    Parameter

    Description

    Repository Name

    Customize the name for the code repository, for example, maven_yml_build.

    • The name starts with a digit, letter, or underscore (_).
    • The value can contain periods (.) and hyphens (-).
    • The value cannot end with .git, .atom, or a period (.).

    Description

    Describe the code repository.

    .gitignore Programming Language

    Select .gitignore based on the programming language, for example, Java.

    Permissions

    Select all.

    • Allow project members to access the repository: A project manager is automatically set as the repository administrator, and a developer is set as a common repository member. When the two roles are added to the project, they will be automatically synchronized to existing repositories.
    • Generate README: You can edit the README file to record information such as the project architecture and compilation purpose, which is similar to a comment on the entire repository.
    • Automatically create Check task (free of charge): After the repository is created, you can view the code check task of the repository in the check task list.

    Visibility

    Set this parameter to Private.

    • Private: Only repository members can access and commit code.
    • Public: The repository is open and read-only to all guests, but is not displayed in their repository list or search results. You can select an open-source license as the remarks.

Creating a build.yml File

  1. In the navigation pane, choose Code > Repo.
  2. Click the name of the code repository you created (see Creating a Code Repository).
  3. Choose Create > Create Directory, as shown in Figure 2.

    Figure 2 Creating a directory

  4. Set parameters based on Table 2 and click OK.

    Table 2 Creating a directory

    Parameter

    Description

    Directory Name

    You can customize the value, for example, .cloudbuild.

    Commit Message

    Enter remarks of the directory. The message is used to record the description of the files in the folder.

  5. Click the name of the directory created in Step 4.
  6. Choose Create > Create File, as shown in Figure 3.

    Figure 3 Creating a file

  7. Name the file build.yml and copy the following code to the file:

    # This YAML is the default template and can be modified based on this 
    ---
    version: 2.0
    steps:
      BUILD:
      - maven:
          image: cloudbuild@maven3.5.3-jdk8-open # You can customize the image path.
          inputs:
            settings:
              public_repos:
                - https://mirrors.huawei.com/maven
            cache: true # Indicates whether to enable the cache.
            command: mvn package -Dmaven.test.failure.ignore=true -U -e -X -B
      - upload_artifact:
          inputs:
            path: "**/target/*.?ar"
      - build_image:
          inputs:
            organization: codeci_gray # Organization name
            image_name: maven_demo # Image name
            image_tag: 1.0 # Image tag
            dockerfile_path: ./Dockerfile

  8. Click OK.

Creating a Java File

  1. Create a directory named src/main/java by referring to Step 4.
  2. Create the HelloWorld.java file in the src/main/java directory by referring to Step 6 and Step 7. The code in the file is as follows:

    /**
     * Hello world
     *
     */
    
    public class HelloWorld {
    
      public static void main(String[] args) {
        System.out.println("Hello World!");
      }
    
    }

  3. Click OK.

Creating a Dockerfile

  1. Create a file named Dockerfile in the root directory by referring to Step 6 and Step 7. The code in the file is as follows:

    FROM swr.cn-north-5.myhuaweicloud.com/codeci/special_base_image:centos7-base-1.0.2-in
    MAINTAINER <devcloud@demo.com> 
    USER root 
    RUN mkdir /demo 
    COPY ./target/server-1.0.jar /demo/app.jar

    server-1.0.jar combines the values of artifactId, packaging, and version in the pom.xml file.

  2. Click OK.

Creating a pom.xml File

  1. Create a file named pom.xml in the root directory by referring to Step 6 and Step 7. The code in the file is as follows:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.huawei.demo</groupId>
      <artifactId>server</artifactId>
      <packaging>jar</packaging>
      <version>1.0</version>
      <name>server</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
      </dependencies>
    
    
      <build>
        <pluginManagement>
          <plugins>
            <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-jar-plugin</artifactId>  
              <version>2.6</version>  
              <configuration>  
                <archive>  
                  <manifest>  
                    <addClasspath>true</addClasspath>  
                  </manifest>  
                  <manifestEntries>  
                    <Main-Class>  
                      HelloWorld
                    </Main-Class>  
                  </manifestEntries>  
                </archive>  
              </configuration>  
            </plugin> 
          </plugins>
        </pluginManagement>
      </build>
    </project>

  2. Click OK.

Creating a Build Task

  1. In the navigation pane, choose CICD > Build, as shown in Figure 4.

    Figure 4 CodeArts Build homepage

  2. Click Create Task.
  3. Set the parameters based on the Table 3, as shown in Figure 5.

    Table 3 Basic information

    Parameter

    Description

    Task Name

    Enter a custom task name, for example, maven_yml_build.

    Code Source

    Select Repo.

    Source Code Repository

    Select the code repository you created (see Creating a Code Repository).

    Branch

    Select the branch created when you create the repository in Creating a Code Repository. If no branch is available, select the default master.

    Description

    Describe the build task.

    Figure 5 Creating a Build Task

  4. Click Next.
  5. Select Blank Template and click Next.
  6. Click the Code tab to view the imported build script, as shown in Figure 6.

    Figure 6 Code tab

  7. Click Create and Run in the upper right corner.

Viewing and Verifying the Build Result

  • Verifying the uploaded the software package
    1. In the navigation pane, choose Artifact > Release Repos.
    2. Go to the release repos to view the released software package. The name of the software package is the same as the task name you use when Creating a Build Task, as shown in Figure 7.
      Figure 7 Viewing the software package
  • Viewing the pushed image
    1. Go to the SWR console.
    2. In the navigation pane, choose My Images. In the organization filter box, search for the organization name you enter in the code when you create a build.yml file, for example, codeci_gray.
    3. In the filtering results, click the image name you enter in the code when you create a build.yml file, for example, maven_demo, as shown in Figure 8.
      Figure 8 Filtering images