在 Gitlab 的 maven 中处理 CI/CD 中的依赖项

时间:2021-03-20 19:45:03

标签: maven gitlab gitlab-ci

我们正在迁移到 Gitlab,我们希望使用 Gitlab 包管理来代替 Nexus Sonatype。在我们使用 Nexus Sonatype 之前。 当您在 Nexus 中创建 Maven 存储库时,您可以上传依赖项 A 并下载并在应用程序(Jar)B 中使用该依赖项 A。 这就是我们正在寻找的行为,但经过 2 天的努力在 gitlab 中实现这一点后,它仍然无法正常工作。我们在 2 个工作和 2 个不同的 gitlab 项目中使用 gitlab.com 云。两个项目都在一个 gitlab 组中。 依赖是mq-resources.jar,由mq-copy.jar实现。

我们有一个用于应用程序 mq-resources 和应用程序 mq-copy 的 settings.xml:


  <servers>
    <server>
            <id>gitlab-maven</id>
            <configuration>
                <httpHeaders>
                <property>
                    <name>Deploy-Token</name>
                    <value>secrettoken</value>
                </property>
                </httpHeaders>
            </configuration>
        </server>

 <profiles>

    <profile>
      <id>gitlab-maven</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <repositories>
            <repository>
                <id>gitlab-maven</id>
        <!--    <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url> 
        <url>${env.CI_SERVER_URL}/api/v4/groups/1302855/-/packages/maven</url>  
                <url>${env.CI_SERVER_URL}/api/v4/packages/maven</url>  -->
            <url>${env.CI_SERVER_URL}/api/v4/groups/1302855/-/packages/maven</url>  
            </repository> 
        </repositories>
    </profile>
  </profiles>

我有一个用于 mq-resources.jar 应用程序的 gitlab-ci.yml 文件


image: maven:3.6.3

variables:
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
  paths:
    - .m2/repository/
    - target/

stages:
  - build

build-code-job:
  stage: build
  script:
    - export
    - mvn $MAVEN_CLI_OPTS clean deploy

我有一个用于 mq-copy.jar 应用程序的 gitlab-ci.yml 文件:

image: maven:3.6.3

variables:
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
  paths:
    - .m2/repository/
    - target/

stages:
  - build

build-code-job:
  stage: build
  script:
    - export
    - mvn $MAVEN_CLI_OPTS dependency:get -Dartifact=nl.example.mq:mq-resources:3.0.10-SNAPSHOT 
    - mvn $MAVEN_CLI_OPTS clean deploy

用于实现 mq-copy.jar 的 maven 文件如下所示。

<dependencies>
        <dependency>
            <groupId>nl.example.mq</groupId>
            <artifactId>mq-resources</artifactId>
            <version>3.0.10-SNAPSHOT</version>
        </dependency>
    </dependencies>
    
    <distributionManagement>
        <repository>
            <id>gitlab-maven</id>
            <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url>
        </repository>
        <snapshotRepository>
            <id>gitlab-maven</id>
            <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url>
        </snapshotRepository>
    </distributionManagement> 

mq-copy.jar 文件的 pom 是相同的,当然没有 mq-resource 依赖项。

但是在 gitlab 中运行 mq-copy 管道时会出现此故障:

[ERROR] Failed to execute goal on project mq-copy: Could not resolve dependencies for project nl.bpittens.mq:mq-copy:jar:3.0.10-SNAPSHOT: Could not find artifact nl.bpittens.mq:mq-resources:jar:3.0.10-SNAPSHOT in gitlab-maven (https://gitlab.com/api/v4/groups/1302855/-/packages/maven) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

我们在云中使用共享的 gitlab 运行器。 也许这是一个问题,但我应该期望工件的 gitlab 存储库是跨项目的?

0 个答案:

没有答案