通过gitlab cicd将maven项目部署到nexus

oogrdqng  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(515)

所以我的问题是,我有一个新安装的nexus3示例,在portainer示例中的docker容器中运行。目前我正在开发一个用maven构建的java库。我想通过gitlab ci将构建工件上传到nexus以存储和重用它们。在下面,您可以找到我的java项目配置、gitlab ci脚本以及nexus的外观。
关系:

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/设置.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(selfhostnexus)到存储库的错误,或者我错过了一些nexus配置。我以前在我的工作场所使用过nexus,但是我从来没有配置过nexus示例,所以我可能错过了什么?
先谢谢你

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题