如何将Maven插件上传至GitHub Packages?

huangapple go评论74阅读模式
英文:

How to upload maven plugin to Github packages?

问题

考虑到我有一个Maven插件项目,我想将其发布到Github的公共Maven仓库,名为“Github Packages”。我已经按照说明完成了所有步骤,对于普通项目,一切都可以正常工作。但是对于打包为"maven-plugin"的Maven插件项目,这些说明不起作用。

在构建日志中,我看到类似以下内容:

> [WARNING] 无法传输元数据 repo-name/maven-metadata.xml
> 从 github (https://maven.pkg.github.com/user-name/repo-name) 到
> 失败的传输文件:
> https://maven.pkg.github.com/user-name/repo-name/group-id/maven-metadata.xml
> 返回代码为: 422 ,原因短语: Unprocessable Entity。

似乎Maven部署插件需要在group-id的根目录中找到maven-metadata.xml,但找不到它,也没有人将其放在那里。如何解决这个问题?

我使用的是Apache Maven 3.3.9,并使用以下命令:

mvn clean deploy

--附加信息:我使用的pom文件示例:

<?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>

<repositories>
    <repository>
        <id>central</id>
        <url>https://repo1.maven.org/maven2</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>github</id>
        <name>GitHub my_repo Apache Maven Packages</name>
        <url>https://maven.pkg.github.com/my_nick/my_repo</url>
    </repository>
</repositories>

<version>1.0.0</version>
<groupId>x</groupId>
<artifactId>some-plugin</artifactId>
<packaging>maven-plugin</packaging>

<dependencies>
    <dependency>
        <groupId>x</groupId>
        <artifactId>my-dependency</artifactId>
        <version>1.0.0</version>
    </dependency>

    <dependency>
        <groupId>com.github.javaparser</groupId>
        <artifactId>javaparser-core</artifactId>
        <version>3.15.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.6.3</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.6.0</version>
        </plugin>
    </plugins>
</build>
</project>
英文:

Consider I have a maven plugin project and I want to publish it to Github's public maven repository called "Github Packages". I've done everything by instruction and for normal projects everything works fine out of the box. But for maven plugin projects with packaging=maven-plugin the instruction doesn't work.

In build log I see something like this:

> [WARNING] Could not transfer metadata repo-name/maven-metadata.xml
> from/to github (https://maven.pkg.github.com/user-name/repo-name):
> Failed to transfer file:
> https://maven.pkg.github.com/user-name/repo-name/group-id/maven-metadata.xml.
> Return code is: 422 , ReasonPhrase:Unprocessable Entity.

It seems the maven deploy plugin needs maven-metadata.xml in the group-id's root, but can't find it and no one puts it there. How to solve this problem?

I use Apache Maven 3.3.9, and use the command:

mvn clean deploy

--Addition: example of pom file I'm using:

<?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>

<repositories>
    <repository>
        <id>central</id>
        <url>https://repo1.maven.org/maven2</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>github</id>
        <name>GitHub my_repo Apache Maven Packages</name>
        <url>https://maven.pkg.github.com/my_nick/my_repo</url>
    </repository>
</repositories>

<version>1.0.0</version>
<groupId>x</groupId>
<artifactId>some-plugin</artifactId>
<packaging>maven-plugin</packaging>

<dependencies>
    <dependency>
        <groupId>x</groupId>
        <artifactId>my-dependency</artifactId>
        <version>1.0.0</version>
    </dependency>

    <dependency>
        <groupId>com.github.javaparser</groupId>
        <artifactId>javaparser-core</artifactId>
        <version>3.15.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.6.3</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.6.0</version>
        </plugin>
    </plugins>
</build>
</project>

答案1

得分: 1

很遗憾,我还没有找到适合我的问题的正确答案,似乎现在无法将Maven插件添加到Github Packages。

然而,我找到了一个解决方法,它使用S3作为存储库后端,因此您不需要使用Nexus或JFrog等重量级解决方案。您可以阅读这个这个来了解如何操作。

英文:

Unfortunately I haven't found the right answer to my question, it seems that for now it's impossible to add Maven plugins to Github Packages.

However I found a workaround which uses S3 as a repository backend, so you don't need heavyweight solutions like Nexus or JFrog. You can read this and this on how to do it.

答案2

得分: 0

如果您已经将工件上传到GitHub Packages,那么这意味着您已经正确配置了一切。

我猜测422错误的真正原因是您正在尝试使用相同版本上传已经上传过的相同工件。如果它不是SNAPSHOT版本,那么存储库应该拒绝替换它,以使其行为正确。

当我尝试重新部署已部署的软件包时,我遇到了相同的错误:

Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project MavenPluginForGithub:
Failed to deploy artifacts: Could not transfer artifact ru.dmochalov:SampleMavenPluginForGithub:jar:1.0.3 from/to github (https://maven.pkg.github.com/dmochalov/hello-world):
Failed to transfer file: https://maven.pkg.github.com/dmochalov/hello-world/ru/dmochalov/SampleMavenPluginForGithub/1.0.3/SampleMavenPluginForGithub-1.0.3.jar.
Return code is: 422, ReasonPhrase: Unprocessable Entity. -> [Help 1]

如何修复?

我认为您有两个选项:

  1. 将插件的版本 <version>1.0.0</version> 从1.0.0递增到1.0.1。如果插件不稳定且正在开发中,考虑使用1.0.1-SNAPSHOT版本。GitHub允许重新部署带有SNAPSHOT版本的工件。因此在开发时您总是可以重新部署它。
  2. 从存储库中删除该软件包。您只能在私有存储库中执行此操作。

422错误 vs 401错误

我猜测错误代码没有被广泛接受的规范或标准化,不同的存储库行为不同。例如,Maven Central存储库在尝试替换已部署版本时会回复401错误
为什么GitHub决定使用422是一个谜。社区论坛中有一个答案,但没有适当的解释。

英文:

If you have already uploaded the artifact to the GitHub Packages then it means you have configured everything right.

I suppose the real reason for the 422 error is that you are trying to upload the same artifact with the same version that already was uploaded. And if it is not a SNAPSHOT version then the repository should deny replacing it so it behaves correctly.

I got the same error when trying to redeploy the already deployed package:

Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project MavenPluginForGithub: 
Failed to deploy artifacts: Could not transfer artifact ru.dmochalov:SampleMavenPluginForGithub:jar:1.0.3 from/to github (https://maven.pkg.github.com/dmochalov/hello-world): 
Failed to transfer file: https://maven.pkg.github.com/dmochalov/hello-world/ru/dmochalov/SampleMavenPluginForGithub/1.0.3/SampleMavenPluginForGithub-1.0.3.jar. 
Return code is: 422, ReasonPhrase: Unprocessable Entity. -> [Help 1]

How to fix?

Is suppose you have two options:

  1. Increment the version <version>1.0.0</version> of the plugin from 1.0.0 to 1.0.1. Consider using 1.0.1-SNAPSHOT versions if the plugin is unstable and under development. GitHub allows redeploying artifacts with SNAPSHOT versions. So you could always redeploy it when developing.
  2. Delete the package from the repo. You can do it only for packages in a private repository.

422 error vs 401 error

I suppose that there is not accepted specification or standardization for error codes and different repositories behave differently. For example, the Maven Central repository replies with 401 error when attempting to replace the already deployed version.
Why GitHub decided to use 422 is a mystery. There is an answer in the community forum but without proper explanation.

答案3

得分: 0

我在从GitHub Actions发布Maven构件到GitHub Packages时遇到了相同的问题,即服务器返回422:无法处理的实体。原因是上传的构件对应的标签尚不存在。

在这种情况下,错误消息可能会更好一些。

英文:

I had the same problem 422 from server: Unprocessable Entity when publishing Maven artifacts from GitHub Actions to GitHub Packages. The reason was that the corresponding tag for the uploaded artifact didn't exist yet.

The error message may be better in this case.

答案4

得分: -2

以下是您要的翻译内容:

这里是如何完成此操作的**官方 GitHub 链接。然而,由于这似乎没有太大帮助,这里有一个链接到一个Gradle 论坛问题**,应该能帮助您解决这个问题。祝您好运!

英文:

Here's the official github link for how to do exactly that. However, since this doesn't seem to be of much help, here's a link to a gradle forum question that should help you with this. Best of luck!

huangapple
  • 本文由 发表于 2020年4月10日 16:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61136940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定