如何使用Gradle推送到Github软件包注册表

时间:2019-08-02 09:06:12

标签: java maven gradle github-package-registry

试图将Gradle项目推送到Github程序包注册表,但是无法正常工作。

io.freefair.github.package-registry-maven-publish插件用于Gradle。

build.gradle中为GitHub配置发布所需的数据-以下代码。并运行发布任务publishAllPublicationsToGutHub。没有收到任何错误,但是我在GitHub软件包注册表中看不到我的软件包。

github {
    slug
    username = "myGitUserName"
    token = "myTokenWithRightAccess"
    tag = "HEAD"
    travis = true
}

期待一些如何使用Gradle发布到Github软件包注册表的示例,或者发布时我做错了

5 个答案:

答案 0 :(得分:6)

新答案

GitHub已发布了官方指南:Configuring Gradle for use with GitHub Packages


旧答案

似乎插件is not very stable yet。看一下the repository I've created一切设置完毕。我设法用该插件here发布了一些软件包。

即使发布了软件包,由于maven-metadata.xml的某些问题,Gradle仍将任务显示为失败:

> Task :publishMainPublicationToGitHub madhead Maven PackagesRepository FAILED
Could not transfer metadata so57323260:test/maven-metadata.xml from/to remote (https://maven.pkg.github.com/madhead): Could not get resource 'so57323260/test/maven-metadata.xml'

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishMainPublicationToGitHub madhead Maven PackagesRepository'.
> Failed to publish publication 'main' to repository 'GitHub madhead Maven Packages'
   > Could not GET 'https://maven.pkg.github.com/madhead/so57323260/test/maven-metadata.xml'. Received status code 422 from server: Unprocessable Entity

但是没关系,有一天可能会解决。

我注意到,由于Maven发布的groupId错误,这些软件包可能不会发布(请参见linked issue)。现在看来,它应该等于Github的项目名称。因此,就我而言,我不得不将so57323260用作groupId项目的madhead/so57323260。包通常不是在Maven中工作的方式,所以这可能是您的问题。

答案 1 :(得分:2)

我能够使用maven-publish插件发布到Github软件包注册表。现在看来一切正常。

我的build.gradle文件如下:

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id 'java'
    id 'maven-publish'
}

group 'com.company.project'
archivesBaseName = 'library-name'
version '0.1.0'

repositories {
    mavenCentral()
}

dependencies {
  // java dependencies
}

publishing {
    repositories {
        maven {
            name = "Github"
            url = uri("https://maven.pkg.github.com/<OWNER>/<REPO>")
            credentials {
                username = findProperty("github.username")
                password = findProperty("github.token")
            }
        }
    }
    publications {
        register("jar", MavenPublication) {
            from(components["java"])
            pom {
                url.set("https://github.com/<OWNER>/<REPO>.git")
            }
        }
    }
}

将您的github用户名和令牌放入gradle.properties文件。

答案 2 :(得分:1)

答案 3 :(得分:1)

还值得设置一个github动作以发布到github软件包repo:


name: Publish package to GitHub Packages
on:
  release:
    types: [created]
jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Publish package
        run: gradle -Pversion=${{ github.event.release.tag_name }} build publish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

每次我们创建带有该标签作为版本的发布标签时,这都会发布一个包。

答案 4 :(得分:0)

正确填写这些属性

  1. 所有者
  2. 存储库
  3. USERNAME(或gradle属性gpr.user
  4. PASSWORD(或gradle属性gpr.key

@请参见演示

  1. https://github.com/youngerier/packagesdemo

  2. https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages