Maven dependencies and publishing to a private repository

Updated at:

This guide explains how to use a Flow pipeline to build Maven artifacts and publish them to a private Maven repository. You will learn how to create an artifact repository, configure settings, and publish artifacts.

Prerequisites

Maven dependency settings

Flow automatically proxies the Alibaba Cloud public proxy repository and the Alibaba Cloud organizational private repository in Packages. To use a self-managed private Maven repository or a custom Maven configuration, choose one of the following methods.

Flow supports the following three methods to configure Maven dependencies:

  • Create a settings.xml file in the root directory of your code repository.

  • In your Flow pipeline, go to global settings > build configuration, select Custom Maven Configuration in the Organization Maven Settings section, and upload a settings.xml file.

  • Use the default settings.xml file provided by Alibaba Cloud DevOps.

Currently, Maven configurations affect only Java builds. The priority is as follows:

  1. By default, a Flow build first loads the settings.xml configuration file from the root directory of the code repository. This configuration affects only the pipelines associated with this code repository. You must also specify the settings.xml file in your build command.

    mvn -s settings.xml clean package
  2. If you go to global settings > build configuration in a Flow pipeline, select Custom Maven Configuration in Organization Maven Settings, and upload a settings.xml file, Flow uses this file for all Java builds in all Flow pipelines within the organization.

  3. If your project's code repository does not have a settings.xml file and you have not configured a Custom Maven Configuration, Flow automatically generates a settings.xml file for the build, configured to connect to the Alibaba Cloud organizational private repository and the Alibaba Cloud public proxy repository.

    Note

    When you build with a Flow pipeline, Flow first downloads dependencies from the Alibaba Cloud organizational private repository. If a dependency is not found, Flow then downloads it from the Alibaba Cloud public proxy repository.

To avoid downloading Maven dependencies again for every build, enable the build cache. On the pipeline editing page, choose Variables and Cache > Cache, add the /root/.m2 cache directory, and turn on its switch. Flow then caches the dependencies that are already downloaded to the /root/.m2 local Maven repository, and subsequent builds reuse them, which reduces the dependency download time.

Access credential configuration

  1. On the artifact repository page, click your target private Maven repository.

  2. In the left-side navigation pane, choose repository guide > Maven Configuration to view the publishing configuration information.

    The push configuration has two steps. First, set repository credentials: in the settings.xml file, add a <server> element under the <servers> node, set the <id> to private, and enter the repository's username and password. Second, configure the artifact upload: in the <profiles> section, specify the deployment repository address by using the altReleaseDeploymentRepository attribute. The format is private::default::https://packages.aliyun.com/maven/xxx/private. To upload snapshot version artifacts, change the attribute name to altSnapshotDeploymentRepository.

    Note

    You can download the settings.xml configuration file from the repository guide page and upload it to the root directory of your code repository, or you can create and configure the file directly in the root directory.

  3. On the code repository page, click your target code repository.

  4. In the root directory of your code repository, upload or create a settings.xml configuration file.

    <?xml version="1.0" encoding="UTF-8"?>
    <settings
        xmlns="http://maven.apache.org/SETTINGS/1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
        <!-- Set repository credentials -->
        <servers>
            <server>
                <id>private</id>
                <username>${MVN_USERNAME}</username>
                <password>${MVN_PASSWORD}</password>
            </server>
        </servers>
        <!-- Artifact upload configuration -->
        <profiles>
            <profile>
                <id>rdc</id>
                <properties>
                    <altReleaseDeploymentRepository>
                        private::default::https://packages.aliyun.com/xxx/maven/private
                    </altReleaseDeploymentRepository>
                </properties>
            </profile>
        </profiles>
        <!-- Add activeProfiles configuration -->
        <activeProfiles>
            <activeProfile>rdc</activeProfile>
        </activeProfiles>
        <!-- Add Alibaba Cloud public proxy configuration -->
        <mirrors>
            <mirror>
                <id>mirror</id>
                <mirrorOf>central,jcenter,!private</mirrorOf>
                <name>mirror</name>
                <url>https://maven.aliyun.com/nexus/content/groups/public</url>
            </mirror>
        </mirrors>
    </settings>
    Note
    • To avoid formatting errors from copying or manual creation, we recommend that you download the file from the repository guide > Maven Configuration page and then upload it.

    • To protect your access credentials, set them as custom environment variables in Flow.

Build and publish with Flow

In the build stage of your pipeline, under task step > Java Build, enter the build command. For more information about other build parameters, see Java Test Build. In the upper-right corner, click Save Only.

Set the JDK Version to JDK 1.8 and the Maven Version to Maven 3.5.2. For the build command, enter mvn -s settings.xml -B clean deploy -Dmaven.test.skip=true -Dautoconfig.skip.

Note

The default settings.xml file contains the configuration for the Alibaba Cloud DevOps private repository. If you want to use a custom Maven configuration for your build, you can contact your organization administrator to set a custom settings.xml file in Global Settings. Alternatively, you can upload the configuration file to a specified directory in your code repository and specify the file in the Maven build command by using the -s settings.xml  flag.

mvn -s settings.xml -B clean deploy -Dmaven.test.skip=true -Dautoconfig.skip

Build command

Description

mvn

The executable for the Maven command-line tool, used to run the Maven build lifecycle.

-s settings.xml

Specifies the path to the settings.xml file, which contains Maven configuration information, such as repository credentials and URLs.

-B clean deploy

This option specifies two Maven lifecycle phases: clean and deploy. The clean phase removes previous build artifacts, and the deploy phase publishes the project's artifact to the configured remote repository.

-Dmaven.test.skip=true

This option tells Maven to skip the test phase during the build. This is useful for rapid deployments or integration testing.

-Dautoconfig.skip

Skips Maven's automatic configuration.

View run results

  1. Check the run results in the Flow pipeline.

    After the pipeline run is complete, confirm that the Apply for Runtime Environment, Clear Workspace, Clone Source Code, Pipeline Cache, Java Build, Upload Artifact, and Upload Cache steps in the left-side panel all have a green check mark. This indicates that all steps ran successfully. Click the Upload Artifact step to view detailed logs and confirm that the pipeline successfully packaged and uploaded the build artifact to OSS.

  2. Verify that the new artifact version is listed in your private Maven artifact repository.

FAQ

What can I do if a Java build takes too long?

If the duration of a Java build increases significantly, troubleshoot as follows:

  1. Check where Maven downloads dependencies from. If the repository or mirror in settings.xml or pom.xml points to an overseas repository such as repo1.maven.org, dependencies are downloaded from that repository, and cross-border network latency significantly increases the build duration.

  2. Make sure that repository and mirror point to the Alibaba Cloud Maven mirror. The default Maven configuration provided by Alibaba Cloud DevOps is already connected to the Alibaba Cloud public proxy repository maven.aliyun.com. If you use a custom settings.xml, verify that its mirror also points to the Alibaba Cloud mirror. Otherwise, the custom configuration overrides the default one.

  3. Enable the build cache. On the pipeline editing page, choose Variables and Cache > Cache, add the /root/.m2 cache directory, and turn on its switch so that subsequent builds do not download the same dependencies again.

Related documents

Java Test Build

In the left navigation bar, click Package List and select a target artifact package, such as com.example:social-simple. The details panel on the right displays information such as the total number of versions, the pusher, and the push time. On the File Information tab, you can view and download specific files, such as maven-metadata.xml, .pom, .jar, and checksum files.