• 极狐gitlab maven packages 仓库使用


    极狐gitlab maven packages 仓库使用

    官方文档:Maven packages in the Package Repository | GitLab

    极狐gitlab版本:gitlab-jh v15.0.2 旗舰版

    1. 创建 maven 项目

    1.1 创建 p1 项目

    创建 leffss/p1 项目,添加以下文件:

    src/main/java/com/mycompany/app/App.java

    package com.mycompany.app;
    
    /**
     * Hello world!
     */
    public class App
    {
    
        private final String message = "Hello World!";
    
        public App() {}
    
        public static void main(String[] args) {
            System.out.println(new App().getMessage());
        }
    
        private final String getMessage() {
            return message;
        }
    
    }
    

    src/test/java/com/mycompany/app/AppTest.java

    package com.mycompany.app;
    
    import java.io.ByteArrayOutputStream;
    import java.io.PrintStream;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.After;
    import static org.junit.Assert.*;
    
    /**
     * Unit test for simple App.
     */
    public class AppTest
    {
    
        private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    
        @Before
        public void setUpStreams() {
            System.setOut(new PrintStream(outContent));
        }
    
        @Test
        public void testAppConstructor() {
            try {
                new App();
            } catch (Exception e) {
                fail("Construction failed.");
            }
        }
    
        @Test
        public void testAppMain()
        {
            App.main(null);
            try {
                assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString());
            } catch (AssertionError e) {
                fail("\"message\" is not \"Hello World!\"");
            }
        }
    
        @After
        public void cleanUpStreams() {
            System.setOut(null);
        }
    
    }
    

    pom.xml

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>my-app</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13.2</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.1</version>
            </plugin>
          </plugins>
        </pluginManagement>
        <plugins>
          <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
                  <mainClass>com.mycompany.app.App</mainClass>
                </manifest>
              </archive>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>3.0.0-M3</version>
            <executions>
              <execution>
                <id>enforce-maven</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <requireMavenVersion>
                      <version>[3.5.4,)</version>
                    </requireMavenVersion>
                  </rules>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    1.2 创建 p2 项目

    创建 leffss/p2 项目,添加以下文件:

    src/main/java/com/mycompany/app2/App.java

    package com.mycompany.app2;
    
    /**
     * Hello world!
     */
    public class App
    {
    
        private final String message = "Hello World!";
    
        public App() {}
    
        public static void main(String[] args) {
            System.out.println(new App().getMessage());
        }
    
        private final String getMessage() {
            return message;
        }
    
    }
    

    src/test/java/com/mycompany/app2/AppTest.java

    package com.mycompany.app2;
    
    import java.io.ByteArrayOutputStream;
    import java.io.PrintStream;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.After;
    import static org.junit.Assert.*;
    
    /**
     * Unit test for simple App.
     */
    public class AppTest
    {
    
        private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    
        @Before
        public void setUpStreams() {
            System.setOut(new PrintStream(outContent));
        }
    
        @Test
        public void testAppConstructor() {
            try {
                new App();
            } catch (Exception e) {
                fail("Construction failed.");
            }
        }
    
        @Test
        public void testAppMain()
        {
            App.main(null);
            try {
                assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString());
            } catch (AssertionError e) {
                fail("\"message\" is not \"Hello World!\"");
            }
        }
    
        @After
        public void cleanUpStreams() {
            System.setOut(null);
        }
    
    }
    

    pom.xml

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app2</groupId>
      <artifactId>my-app2</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>my-app2</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13.2</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.1</version>
            </plugin>
          </plugins>
        </pluginManagement>
        <plugins>
          <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
                  <mainClass>com.mycompany.app2.App</mainClass>
                </manifest>
              </archive>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>3.0.0-M3</version>
            <executions>
              <execution>
                <id>enforce-maven</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <requireMavenVersion>
                      <version>[3.5.4,)</version>
                    </requireMavenVersion>
                  </rules>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    2. 上传到 package 仓库

    leffss/p1 项目中操作

    2.1 上传 package

    项目根目录创建 maven 配置文件 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>${CI_JOB_TOKEN}</value>
              </property>
            </httpHeaders>
          </configuration>
        </server>
      </servers>
    </settings>
    

    pom.xml 新增 repositoriesdistributionManagement 配置:

    ...
      <url>http://maven.apache.org</url>
      <repositories>
        <repository>
          <id>gitlab-maven</id>
          <url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
        </repository>
      </repositories>
      <distributionManagement>
        <repository>
          <id>gitlab-maven</id>
          <url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
        </repository>
        <snapshotRepository>
          <id>gitlab-maven</id>
          <url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
        </snapshotRepository>
      </distributionManagement>
      <dependencies>
    ...
    

    项目根目录创建新增 .gitlab-ci.yml

    stages:
      - deploy
    
    deploy:
      stage: deploy
      image: maven:3.8.5-jdk-11
      script:
        - 'mvn deploy -s ci_settings.xml'
      artifacts:
        paths:
          - target/
      only:
        - tags
    

    给项目添加一个 tag 就会触发 pipeline:

    打包并上传成功:

    2.2 查看 package

    项目 > Packages & Registries > Package Registry

    • 多次上传相同版本不会覆盖,因为 gitlab 会为每次提交的 pom 和 jar 文件添加一个时间与序号

    • 其中 maven-metadata.xml 记录了最新的提交信息

      <?xml version="1.0" encoding="UTF-8"?>
      <metadata modelVersion="1.1.0">
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <versioning>
          <lastUpdated>20220609085206</lastUpdated>
          <snapshot>
            <timestamp>20220609.085206</timestamp>
            <buildNumber>2</buildNumber>
          </snapshot>
          <snapshotVersions>
            <snapshotVersion>
              <extension>jar</extension>
              <value>1.0-20220609.085206-2</value>
              <updated>20220609085206</updated>
            </snapshotVersion>
            <snapshotVersion>
              <extension>pom</extension>
              <value>1.0-20220609.085206-2</value>
              <updated>20220609085206</updated>
            </snapshotVersion>
          </snapshotVersions>
        </versioning>
        <version>1.0-SNAPSHOT</version>
      </metadata>
      
      

    注意:上传到项目仓库时,组仓库也是可以看到并使用的

    提交多版本时:

    3. 使用 package

    leffss/p2 项目中操作,方法是使用 mvn dependency:get 测试是否能够正常下载刚才上传的 maven 包。

    项目根目录创建 maven 配置文件 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>${CI_JOB_TOKEN}</value>
              </property>
            </httpHeaders>
          </configuration>
        </server>
      </servers>
    </settings>
    

    pom.xml 新增 repositoriesdistributionManagementdependency 配置:

    ...
      <url>http://maven.apache.org</url>
      <repositories>
        <repository>
          <id>gitlab-maven</id>
          <url>http://gitlab.example.com/api/v4/projects/3/packages/maven</url>
        </repository>
      </repositories>
      <distributionManagement>
        <repository>
          <id>gitlab-maven</id>
          <url>http://gitlab.example.com/api/v4/projects/3/packages/maven</url>
        </repository>
        <snapshotRepository>
          <id>gitlab-maven</id>
          <url>http://gitlab.example.com/api/v4/projects/3/packages/maven</url>
        </snapshotRepository>
      </distributionManagement>
      <dependencies>
        <dependency>
          <groupId>com.mycompany.app</groupId>
          <artifactId>my-app</artifactId>
          <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
    ...
    

    项目根目录创建新增 .gitlab-ci.yml

    stages:
      - test
    
    test:
      stage: test
      image: maven:3.8.5-jdk-11
      script:
        - 'mvn -s ci_settings.xml dependency:get -Dartifact=com.mycompany.app:my-app:1.0-SNAPSHOT'
    

    运行 pipeline 发现报错:

    ...
    Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-9-stable-1/plexus-container-default-1.0-alpha-9-stable-1.jar (194 kB at 23 kB/s)
    [INFO] Resolving com.mycompany.app:my-app:jar:1.0-SNAPSHOT with transitive dependencies
    Downloading from maven-default-http-blocker: http://0.0.0.0/com/mycompany/app/my-app/1.0-SNAPSHOT/maven-metadata.xml
    [WARNING] Could not transfer metadata com.mycompany.app:my-app:1.0-SNAPSHOT/maven-metadata.xml from/to maven-default-http-blocker (http://0.0.0.0/): transfer failed for http://0.0.0.0/com/mycompany/app/my-app/1.0-SNAPSHOT/maven-metadata.xml
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  02:31 min
    [INFO] Finished at: 2022-06-09T12:17:38Z
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.8:get (default-cli) on project my-app2: Couldn't download artifact: Unable to get dependency information for com.mycompany.app:my-app:jar:1.0-SNAPSHOT: Failed to retrieve POM for com.mycompany.app:my-app:jar:1.0-SNAPSHOT: Could not transfer artifact com.mycompany.app:my-app:pom:1.0-SNAPSHOT from/to maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories: [gitlab-maven (http://gitlab.example.com/api/v4/projects/3/packages/maven, default, releases+snapshots)]
    [ERROR]   com.mycompany.app:my-app:jar:1.0-SNAPSHOT
    [ERROR] 
    [ERROR] from the specified remote repositories:
    [ERROR]   maven-default-http-blocker (http://0.0.0.0/, releases=true, snapshots=true),
    [ERROR]   central (https://repo.maven.apache.org/maven2, releases=true, snapshots=false)
    [ERROR] Path to dependency: 
    [ERROR] 	1) org.apache.maven.plugins:maven-downloader-plugin:jar:1.0
    [ERROR] 
    [ERROR] 
    [ERROR] -> [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/MojoExecutionException
    ERROR: Job failed: command terminated with exit code 1
    

    原因是 maven 从3.8.1版本开始,禁止了从 http 协议的仓库地址下载依赖

    解决方法一:使用低版本 maven

    stages:
      - test
    
    test:
      stage: test
      image: maven:3.6.3-jdk-11
      script:
        - 'mvn -s ci_settings.xml dependency:get -Dartifact=com.mycompany.app:my-app:1.0-SNAPSHOT'
    

    运行 pipeline 成功:

    ...
    Downloaded from central: https://repo.maven.apache.org/maven2/classworlds/classworlds/1.1/classworlds-1.1.jar (38 kB at 4.3 kB/s)
    Progress (2): 98/284 kB | 98/575 kB
    Progress (2): 163/284 kB | 163/575 kB
    Progress (2): 212/284 kB | 229/575 kB
    Progress (2): 278/284 kB | 311/575 kB
    Downloaded from central: https://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar (284 kB at 26 kB/s)
    Progress (1): 360/575 kB
    Progress (1): 442/575 kB
    Progress (1): 507/575 kB
    Progress (1): 575 kB    
    Downloaded from central: https://repo.maven.apache.org/maven2/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar (575 kB at 45 kB/s)
    [INFO] Resolving com.mycompany.app:my-app:jar:1.0-SNAPSHOT with transitive dependencies
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/maven-metadata.xml
    Downloaded from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/maven-metadata.xml (767 B at 7.0 kB/s)
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-20220609.085206-2.pom
    Downloaded from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-20220609.085206-2.pom (2.7 kB at 30 kB/s)
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/junit/junit/4.13.2/junit-4.13.2.pom
    Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.2/junit-4.13.2.pom
    Progress (1): 16/27 kB
    Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.2/junit-4.13.2.pom (27 kB at 44 kB/s)
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom
    Downloading from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom
    Progress (1): 766 B
    Downloaded from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom (766 B at 1.9 kB/s)
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom
    Downloading from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom
    Progress (1): 2.0 kB
    Downloaded from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom (2.0 kB at 4.9 kB/s)
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/junit/junit/4.13.2/junit-4.13.2.jar
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-20220609.085206-2.jar
    Downloading from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
    Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
    Downloaded from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-20220609.085206-2.jar (2.8 kB at 29 kB/s)
    Progress (2): 16/385 kB | 16/45 kB
    Downloaded from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar (45 kB at 56 kB/s)
    Progress (1): 163/385 kB
    Progress (1): 245/385 kB
    Progress (1): 311/385 kB
    Progress (1): 376/385 kB
    Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar (385 kB at 122 kB/s)
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  02:30 min
    [INFO] Finished at: 2022-06-09T13:03:06Z
    [INFO] ------------------------------------------------------------------------
    Job succeeded
    

    解决方法二:参考 How to disable maven blocking external HTTP repositories? - Stack Overflow

    ci_settings.xml 新增 mirrors 配置:

    <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>${CI_JOB_TOKEN}</value>
              </property>
            </httpHeaders>
          </configuration>
        </server>
      </servers>
      <mirrors>
        <mirror>
          <id>maven-default-http-blocker</id>
          <mirrorOf>external:http:*</mirrorOf>
          <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
          <url>http://gitlab.example.com/api/v4/projects/3/packages/maven/</url>
        </mirror>
      </mirrors>
    </settings>
    

    经过测试,pipeline 还是失败:

    ...
    Progress (1): 575 kB    
    Downloaded from central: https://repo.maven.apache.org/maven2/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar (575 kB at 21 kB/s)
    [INFO] Resolving com.mycompany.app:my-app:jar:1.0-SNAPSHOT with transitive dependencies
    Downloading from maven-default-http-blocker: http://0.0.0.0/com/mycompany/app/my-app/1.0-SNAPSHOT/maven-metadata.xml
    [WARNING] Could not transfer metadata com.mycompany.app:my-app:1.0-SNAPSHOT/maven-metadata.xml from/to maven-default-http-blocker (http://0.0.0.0/): transfer failed for http://0.0.0.0/com/mycompany/app/my-app/1.0-SNAPSHOT/maven-metadata.xml
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  03:04 min
    [INFO] Finished at: 2022-06-09T13:57:11Z
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.8:get (default-cli) on project my-app2: Couldn't download artifact: Unable to get dependency information for com.mycompany.app:my-app:jar:1.0-SNAPSHOT: Failed to retrieve POM for com.mycompany.app:my-app:jar:1.0-SNAPSHOT: Could not transfer artifact com.mycompany.app:my-app:pom:1.0-SNAPSHOT from/to maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories: [gitlab-maven (http://gitlab.example.com/api/v4/projects/3/packages/maven, default, releases+snapshots)]
    [ERROR]   com.mycompany.app:my-app:jar:1.0-SNAPSHOT
    [ERROR] 
    [ERROR] from the specified remote repositories:
    [ERROR]   maven-default-http-blocker (http://0.0.0.0/, releases=true, snapshots=true),
    [ERROR]   central (https://repo.maven.apache.org/maven2, releases=true, snapshots=false)
    [ERROR] Path to dependency: 
    [ERROR] 	1) org.apache.maven.plugins:maven-downloader-plugin:jar:1.0
    [ERROR] 
    [ERROR] 
    [ERROR] -> [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/MojoExecutionException
    ERROR: Job failed: command terminated with exit code 1
    

    对比了一下正常情况下,下载请求为:

    http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/maven-metadata.xml
    http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-20220609.085206-2.pom
    http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-20220609.085206-2.jar
    
    • 首先获取 maven-metadata.xml ,然后再从中获取到后面 pom 和 jar 的正确名称

    但是使用 mirror 方式下,下载请求为:

    http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/maven-metadata.xml
    http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.pom
    http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0.jar
    

    这样子就无法下载到 pom 与 jar 了。由于对 maven 不是太熟悉,原因未知。

    解决方法三:搭建 gitlab 时开启 https(强烈建议使用有效证书)

    4. 使用 api 直接上传 package

    参考:Maven API | GitLab

    4.1 添加项目 Deploy tokens

    项目 > settings > Repository > Deploy tokens,权限选择:read_package_registrywrite_package_registry

    4.2 api 上传 package

    上传命令:

    curl --request PUT \
         --upload-file my-app-3.0.pom \
         --header "Deploy-Token: t42PhRv6ighShpChNZSD" \
         "http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/3.0-SNAPSHOT/my-app-3.0-SNAPSHOT.pom"
    
    curl --request PUT \
         --upload-file my-app-3.0.jar \
         --header "Deploy-Token: t42PhRv6ighShpChNZSD" \
         "http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/3.0-SNAPSHOT/my-app-3.0-SNAPSHOT.jar"
    
    • 如果 token 是个人token,则将 Deploy-Token 改为 Private-Token 即可

    4.3 查看 api 上传的 package

    结果:

    • 可以看到上传的文件名称和在 ci/cd 任务上传有区别的(未添加时间和序号),然后也没有 maven-metadata.xml 文件

    4.4 测试 api 上传的 package 可用性

    leffss/p2 中测试 3.0 版本:

    修改 pom.xml 文件:

    ...
      <dependencies>
        <dependency>
          <groupId>com.mycompany.app</groupId>
          <artifactId>my-app</artifactId>
          <version>3.0-SNAPSHOT</version>
        </dependency>
        <dependency>
    ...
    

    修改 .gitlab-ci.yml 文件:

    stages:
      - test
    
    test:
      stage: test
      image: maven:3.6.3-jdk-11
      script:
        - 'mvn -s ci_settings.xml dependency:get -Dartifact=com.mycompany.app:my-app:3.0-SNAPSHOT'
    

    pipeline 运行成功:

    ...
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/3.0-SNAPSHOT/my-app-3.0-SNAPSHOT.jar
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/junit/junit/4.13.2/junit-4.13.2.jar
    Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
    Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
    Downloading from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
    Downloaded from gitlab-maven: http://gitlab.example.com/api/v4/projects/3/packages/maven/com/mycompany/app/my-app/3.0-SNAPSHOT/my-app-3.0-SNAPSHOT.jar (2.8 kB at 29 kB/s)
    Progress (1): 45 kB   
    Downloaded from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar (45 kB at 113 kB/s)
    Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar (385 kB at 811 kB/s)
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  01:55 min
    [INFO] Finished at: 2022-06-10T00:42:03Z
    [INFO] ------------------------------------------------------------------------
    Job succeeded
    
  • 相关阅读:
    js(四) 全选/全不选和反选
    js(三) ajax异步局部刷新技术底层代码实现
    js(二) 实现省市联动(json)
    接口
    内部类
    封装
    Static关键字
    this关键字
    带参数的方法
    abstract关键字
  • 原文地址:https://www.cnblogs.com/leffss/p/16476298.html
Copyright © 2020-2023  润新知