使用HTTP从GitHub Package下载maven包

437 阅读1分钟

GitHub Packages 是一个与 Maven 兼容的存储库,可以独立于 Github 访问。

在GitHub包中浏览可用版本

一个标准maven项目中pom.xml对包定义了3个关键字段:

  <groupId>{groupId}</groupId>
  <artifactId>{artifactId}</artifactId>
  <version>{version}</version>

这些文件在maven存储库的路径中使用,在GitHub Packages上的URL具有以下形式:

https://maven.pkg.github.com/{githubUser}/{githubRepository}/{groupId}/{artifactId}/maven-metadata.xml

获取Maven 存储库的元数据

wget -d --header="Authorization: token ${ACCESS_TOKEN}" 
https://maven.pkg.github.com/{githubUser}/{githubRepository}/{groupId}/{artifactId}/maven-metadata.xml

其格式如下:

{  
    "groupId": "{groupId}",  
    "artifactId": "{artifactId}",  
    "versioning": {  
        "latest": "{version}",  
        "versions": {  
            "version":"{version}"  
        },  
        "lastUpdated":"20230306040421"  
    }  
}

查看指定版本的元数据

wget -d --header="Authorization: token ${ACCESS_TOKEN}" 
https://maven.pkg.github.com/{githubUser}/{githubRepository}/{groupId}/{artifactId}/{version}/maven-metadata.xml

其格式如下

{
    "groupId":"{groupId}",
    "artifactId":"{artifactId}",
    "version":"{version}",
    "versioning":{
        "snapshot":{
            "timestamp":"20230306.040330",
            "buildNumber":"2"
        },
        "lastUpdated":"20230306040421",
        "snapshotVersions":{
            "snapshotVersion":[
                {
                    "extension":"jar",
                    "value":"0.0.1-20230306.025957-1",
                    "updated":"20230306030048"
                },
                {
                    "extension":"jar.sha1",
                    "value":"0.0.1-20230306.025957-1",
                    "updated":"20230306030049"
                },
                {
                    "extension":"jar.md5",
                    "value":"0.0.1-20230306.025957-1",
                    "updated":"20230306030050"
                },
                {
                    "extension":"pom",
                    "value":"0.0.1-20230306.025957-1",
                    "updated":"20230306030051"
                },
                {
                    "extension":"pom.sha1",
                    "value":"0.0.1-20230306.025957-1",
                    "updated":"20230306030053"
                },
                {
                    "extension":"pom.md5",
                    "value":"0.0.1-20230306.025957-1",
                    "updated":"20230306030054"
                },
                {
                    "extension":"jar",
                    "value":"0.0.1-20230306.040330-2",
                    "updated":"20230306040416"
                },
                {
                    "extension":"jar.sha1",
                    "value":"0.0.1-20230306.040330-2",
                    "updated":"20230306040417"
                },
                {
                    "extension":"jar.md5",
                    "value":"0.0.1-20230306.040330-2",
                    "updated":"20230306040418"
                },
                {
                    "extension":"pom",
                    "value":"0.0.1-20230306.040330-2",
                    "updated":"20230306040419"
                },
                {
                    "extension":"pom.sha1",
                    "value":"0.0.1-20230306.040330-2",
                    "updated":"20230306040420"
                },
                {
                    "extension":"pom.md5",
                    "value":"0.0.1-20230306.040330-2",
                    "updated":"20230306040421"
                }
            ]
        }
    },
    "@modelVersion":""
}

使用wget下载包

 wget -d --header="Authorization: token {ACCESS_TOKEN}" https://maven.pkg.github.com/{githubUser}/{githubRepository}/{groupId}/{artifactId}/{version}/{artifactId}-{value}.jar

在 Maven 中,一个版本可以有多个value代表每个构建版本, 此{value}为maven-metadata.xml下面的value字段。

使用maven下载包

mvn dependency:copy 插件可以用来代替wgetcurl下载一个 jar 文件

mvn dependency:copy \
  -Dartifact={groupId}:{artifactId}:{version} \
  -DoutputDirectory=. \
  -DrepositoryId=github '
  --global-settings settings.xml