微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过 Gitlab CICD 将 Maven 项目部署到 Nexus

如何解决通过 Gitlab CICD 将 Maven 项目部署到 Nexus

所以我的问题如下,我有一个新安装的 Nexus 3 实例在我的 Portainer 实例内的 Docker 容器中运行。目前我正在开发一个使用 Maven 构建的 Java 库。我想通过 Gitlab CI 将我的构建工件上传到 Nexus 以存储和重用它们。在下面,您可以找到我的 Java 项目配置、Gitlab CI 脚本以及我的 Nexus 的外观。

Nexus

Nexus Setup

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.kryofex</groupId>
    <artifactId>PluginAPI</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

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

    <distributionManagement>
        <!--<snapshotRepository>
            <id>nexus-snapshots</id>
            <url>${nexus.url}/repository/maven-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>nexus-releases</id>
            <url>${nexus.url}/repository/maven-releases/</url>
        </repository>-->
        <repository>
            <id>central</id>
            <name>Central</name>
            <url>${env.NEXUS_REPO_URL}central/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>Snapshots</name>
            <url>${env.NEXUS_REPO_URL}snapshots/</url>
        </snapshotRepository>
    </distributionManagement>


    <build>
        <sourceDirectory>src</sourceDirectory>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                    <minimizeJar>true</minimizeJar>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
    </dependencies>

.m2/settings.xml

<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">
    <servers>
        <server>
            <id>central</id>
            <username>${env.NEXUS_REPO_USER}</username>
            <password>${env.NEXUS_REPO_PASS}</password>
        </server>
        <server>
            <id>snapshots</id>
            <username>${env.NEXUS_REPO_USER}</username>
            <password>${env.NEXUS_REPO_PASS}</password>
        </server>
    </servers>
</settings>

.gitlab-ci.yml

image: maven

variables:
  SNAPSHOT_DEPLOYMENT_REPOSITORY: ${NEXUS_REPO_URL}/repository/maven-snapshots/
  RELEASE_DEPLOYMENT_REPOSITORY: ${NEXUS_REPO_URL}/repository/maven-releases/
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode --errors --fail-at-end --show-version"
  MAVEN_CLI_POST_OPTS: "-Dnexus.url=${NEXUS_REPO_URL} -Dmaven.repo.local=.m2"

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

stages:
  - build
  - test
  - package
  - deploy

codebuild:
  stage: build
  script:
    - mvn install
    - mvn compile

codetest:
  stage: test
  script:
    - mvn $MAVEN_CLI_OPTS test
    - echo "The code has been tested"

codepackage:
  stage: package
  script:
    - mvn $MAVEN_CLI_OPTS package -Dmaven.test.skip=true
    - echo "Packaging the code"
  artifacts:
    paths:
      - target/*.jar
  only:
    - master

codedeploy:
  stage: deploy
  script:
    - mvn -v
    - mvn $MAVEN_CLI_OPTS source:jar deploy -Dnexus_user=${NEXUS_REPO_USER} -Dnexus_pwd=${NEXUS_REPO_PWD} -DsnapshotDeploymentRepository=$SNAPSHOT_DEPLOYMENT_REPOSITORY -DreleaseDeploymentRepository=$RELEASE_DEPLOYMENT_REPOSITORY -DskipTests ${MAVEN_CLI_POST_OPTS}
    - echo "installing the package in local repository"
  only:
    - master

当我使用此配置运行我的管道时,除了部署之外,一切正常。在这个阶段 Gitlab 抛出一个错误

[INFO] Uploading to central: http://78.46.71.23:49155/central/de/kryofex/PluginAPI/1.0.0/PluginAPI-1.0.0.jar
[INFO] Uploading to central: http://78.46.71.23:49155/central/de/kryofex/PluginAPI/1.0.0/PluginAPI-1.0.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:08 min
[INFO] Finished at: 2020-12-27T14:58:42Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project PluginAPI: Failed to deploy artifacts: Could not transfer artifact de.kryofex:PluginAPI:jar:1.0.0 from/to central (http://78.46.71.23:49155/central/): Transfer Failed for http://78.46.71.23:49155/central/de/kryofex/PluginAPI/1.0.0/PluginAPI-1.0.0.jar: Connect to 78.46.71.23:49155 [/78.46.71.23] Failed: Connection timed out -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project PluginAPI: Failed to deploy artifacts: Could not transfer artifact de.kryofex:PluginAPI:jar:1.0.0 from/to central (http://78.46.71.23:49155/central/): Transfer Failed for http://78.46.71.23:49155/central/de/kryofex/PluginAPI/1.0.0/PluginAPI-1.0.0.jar

我该怎么做才能解决这个错误?我想可能是我的存储库 URL(自托管 Nexus)错误,或者我错过了一些 Nexus 配置。我之前在工作场所使用过 Nexus,但我从未配置过 Nexus 实例,所以我可能错过了什么?

先谢谢你

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。