将 Maven 项目发布到 Github 包

Posted

技术标签:

【中文标题】将 Maven 项目发布到 Github 包【英文标题】:Publish Maven project to Github Package 【发布时间】:2021-11-19 11:52:25 【问题描述】:

目标

将私有 maven 项目作为私有 repo 发布到 GitHub 包,以便在其他项目中使用。

出错

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project SecuritySpringBootContracts: Failed to deploy artifacts: Could not transfer artifact com.sagar:SecuritySpringBootContracts:jar:0.0.3 from/to contracts (https://maven.pkg.github.com/sagarnayak/SecuritySpringBootContracts): transfer failed for https://maven.pkg.github.com/sagarnayak/SecuritySpringBootContracts/com/sagar/SecuritySpringBootContracts/0.0.3/SecuritySpringBootContracts-0.0.3.jar, status: 422 Unprocessable Entity

我在 GitHub 中的私有存储库

https://github.com/sagarnayak/SecuritySpringBootContracts

我的代码

C:\Users\SAGAR\.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">

<activeProfiles>
    <activeProfile>github</activeProfile>
</activeProfiles>

<profiles>
    <profile>
        <id>github</id>
        <repositories>
            <repository>
                <id>central</id>
                <url>https://repo1.maven.org/maven2</url>
            </repository>
            <repository>
                <id>contracts</id>
                <url>https://maven.pkg.github.com/sagarnayak/SecuritySpringBootContracts</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    </profile>
</profiles>

<servers>
    <server>
        <id>contracts</id>
        <username>sagarnayak</username>
        <password>ghp_WRGrhj380s8vikAcZMp4j1z02</password>
    </server>
</servers>

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sagar</groupId>
    <artifactId>SecuritySpringBootContracts</artifactId>
    <version>0.0.3</version>
    <name>SecuritySpringBootContracts</name>
    <description>SecuritySpringBootContracts</description>
    <properties>
        <java.version>11</java.version>
        <kotlin.version>1.5.31</kotlin.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</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>
    </dependencies>

    <build>
        <sourceDirectory>$project.basedir/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>$project.basedir/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>$kotlin.version</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>contracts</id>
            <name>contracts package</name>
            <url>https://maven.pkg.github.com/sagarnayak/SecuritySpringBootContracts</url>
        </repository>
    </distributionManagement>
</project>

当我输入 mvnw deploy 时出现错误 -

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project SecuritySpringBootContracts: Failed to deploy artifacts: Could not transfer artifact com.sagar:SecuritySpringBootContracts:jar:0.0.3 from/to contracts (https://maven.pkg.github.com/sagarnayak/SecuritySpringBootContracts): transfer failed for https://maven.pkg.github.com/sagarnayak/SecuritySpringBootContracts/com/sagar/SecuritySpringBootContracts/0.0.3/SecuritySpringBootContracts-0.0.3.jar, status: 422 Unprocessable Entity

欢迎提出任何建议。

【问题讨论】:

确实不建议在您的问题中(或任何地方)发布您的密码 @MorBlau 非常感谢这个建议。密码不正确它被截断了,所以我认为密码错误是安全的。 【参考方案1】:

正如本期提到的Publishing GitHub Packages returns 422 error 原因似乎是您的 Maven 工件的名称,它必须是小写的。

在您的pom.xml 中,例如使用&lt;artifactId&gt;security-springboot-contracts&lt;/artifactId&gt; 而不是&lt;artifactId&gt;SecuritySpringBootContracts&lt;/artifactId&gt;

【讨论】:

【参考方案2】:

应用启动时创建的快照版本错误,应该类似于0.0.1-SNAPSHOT

例如:0.0.1-SNAPSHOT 或 0.0.3-SNAPSHOT

在您的情况下,它是使用 0.0.3 创建的

【讨论】:

以上是关于将 Maven 项目发布到 Github 包的主要内容,如果未能解决你的问题,请参考以下文章

maven 发布/部署到 github 包

002tomcat服务器——通过maven将项目打war包

Github Actions 使用 maven 对另一个存储库的私有包注册表进行身份验证

通过GitHub Action自动部署Maven项目

使用github作为maven仓库存放发布自己的jar包依赖 实现多个项目公共部分代码的集中,避免团队中多个项目之间代码的复制粘贴

利用github搭建私人maven仓库