无法从 GitLab 包注册表下载 Maven 依赖项

时间:2021-02-26 12:11:02

标签: java spring gitlab-ci

我有一个 java-spring 应用程序,在不同的存储库中有 2 个 maven 依赖项,但在同一组中。我使用 gitlab-ci 在 2 个项目包注册表中部署了 jar,但我无法检索它们(我只能使用一个依赖项,在 spring 应用程序 pom 中设置其项目 repo) . 对于 2 个依赖项,我编写了相同的 ci-settings.xml:

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<servers>
    <server>
        <id>gitlab-maven</id>
        <configuration>
            <httpHeaders>
                <property>
                    <name>Job-Token</name>
                    <value>${env.CI_JOB_TOKEN}</value>
                </property>
            </httpHeaders>
        </configuration>
    </server>
</servers>

具有相同的 gitlab-ci.yml

deploy:
 image: maven:3.6-jdk-11
 script:
  - 'mvn deploy -s ci_settings.xml'
 only:
  - master

在第一个依赖项 (sp1) 的 pom.xml 中,我有:

<groupId>ch.myCompany.dep</groupId>
<artifactId>sp_1</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<repositories>
    <repository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/groups/GROUP_ID/-/packages/maven</url>
    </repository>
</repositories>
<distributionManagement>
    <repository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/projects/SP1_REPO_ID/packages/maven</url>
    </repository>
    <snapshotRepository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/projects/SP1_REPO_ID/packages/maven</url>
    </snapshotRepository>
</distributionManagement>

在第二个依赖项 (sp2) 的 pom.xml 中,我有:

<groupId>ch.myCompany.dep</groupId>
<artifactId>sp_2</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<repositories>
    <repository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/groups/GROUP_ID/-/packages/maven</url>
    </repository>
</repositories>
<distributionManagement>
    <repository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/projects/SP2_REPO_ID/packages/maven</url>
    </repository>
    <snapshotRepository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/projects/SP2_REPO_ID/packages/maven</url>
    </snapshotRepository>
</distributionManagement>

在 spring 应用程序中,我将 2 个工件设置为依赖项,但无法检索它们。这是 pom.xml 的一部分:

    <modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ch.myCompany.dep</groupId>
<artifactId>sp_service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp_service</name>
<description>Service Layer for Spring Boot</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>ch.myCompany.dep</groupId>
        <artifactId>sp_1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
 <dependency>
        <groupId>ch.myCompany.dep</groupId>
        <artifactId>sp_2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/projects/GROUP_ID/packages/maven</url>
    </repository>
</repositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

1 个答案:

答案 0 :(得分:1)

在依赖项的 pom.xml distributionManagement 部分,您使用了正确的 URL:

<url>https://gitlab.com/api/v4/groups/GROUP_ID/-/packages/maven</url>

但是,在您的 spring 应用程序 pom.xml repositories 部分中,url 不正确:

<url>https://gitlab.com/api/v4/projects/GROUP_ID/packages/maven</url>

您在 /-/GROUP_ID 之间缺少 packages