如何在 Artifactory 上发布模块及其版本、依赖项和自定义 artifactId

How to publish modules on Artifactory with their versions, dependencies and a custom artifactId

我正在使用 gradle 的出版物和 Artifactory 插件向 Artifactory 发布一个由不同模块组成的 SDK。

一切都已发布,所有模块都分组在 build 中,名称和版本是我在 gradle.property 文件中指定的:

buildInfo.build.name=test.buildname
buildInfo.build.number=0.1.3

问题是所有模块都是使用默认名称 = 模块名称发布的,没有版本,也没有依赖关系,至少 Artifactory 向我展示的 moduleid;number of artifacts; dependencies 是这样的:

my-real-project-name:FullModuleName:unspecified; 2; 0

这里是用于发布的成绩文件片段(它适用于所有模块的每个 build.gradle 脚本:

apply from: "../../versioning.gradle"
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'

publishing {
    publications {
        modulePublications(MavenPublication) { 
            artifact("$buildDir/outputs/aar/$archivesBaseName-release.aar")
            groupId 'com.mygroup'
            version '0.1.2'
            artifactId shortName

            println "shortName=$shortName"

            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')

                configurations.implementation.allDependencies.each {
                    println "dependency=$it"
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                }
            }
        }
    }
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = 'gradle-dev-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"

        }
        defaults {
            publications('modulePublications')
            properties = ['dev.team': 'android-sdk']
            publishArtifacts = true
            publishPom = true
        }

    }
    resolve {
        repository {
            repoKey = 'gradle-dev'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true

        }
    }
}

在项目级别 build.gradle 文件中我有:

buildscript {
    repositories {
        google()
        jcenter()

        maven {
            url 'http://localhost:8081/artifactory/gradle-dev'
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }

    }
    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.15.2"
    }
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = 'gradle-dev-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true

        }
    }
    resolve {
        repository {
            repoKey = 'gradle-dev'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true

        }
    }
}

发布后,我看到在 build 文件夹中创建了一个 build-info.json 文件:

{
  "version" : "1.0.1",
  "name" : "test.buildname",
  "number" : "0.1.3",
  "type" : "GRADLE",
  "buildAgent" : {
    "name" : "Gradle",
    "version" : "6.4"
  },
  "agent" : {
    "name" : "Gradle",
    "version" : "6.4"
  },
  "started" : "2020-05-22T11:25:55.441+0100",
  "durationMillis" : 1365,
  "principal" : "my username on my machine",
  "artifactoryPrincipal" : "the user name I set for artifactory in the gradle.property file",
  "artifactoryPluginVersion" : "Unknown",
  "vcs" : [ ],
  "licenseControl" : {
    "runChecks" : false,
    "includePublishedArtifacts" : false,
    "autoDiscover" : false,
    "scopesList" : "",
    "licenseViolationsRecipientsList" : ""
  },
  "modules" : [ {
    "id" : "projectname-android-sdk.sdk:ModuleName1:unspecified",
    "artifacts" : [ {
      "type" : "aar",
      "sha1" : "6cf10ee05254c14cc74e021a150f939cf0b9f55c",
      "md5" : "3007163c98733e72ef103ede05275a2f",
      "name" : "ModuleName1-0.0.1.aar"
    }, {
      "type" : "pom",
      "sha1" : "850ab02427a7f23680679ebbf05ca3b809809700",
      "md5" : "5952e9f0e90df9543f6fd8ede6168b91",
      "name" : "ModuleName1-0.0.1.pom"
    } ],
    "excludedArtifacts" : [ ],
    "dependencies" : [ ]
  }, {
    "id" : "projectname-android-sdk.sdk:ModuleName2:unspecified",
    "artifacts" : [ {
      "type" : "aar",
      "sha1" : "c246f3c188ebb064c4a87066863f55eae5553b4e",
      "md5" : "0b8c516d3a3907c9c764046e718a6796",
      "name" : "ModuleName2-0.0.1.aar"
    }, {
      "type" : "pom",
      "sha1" : "4efacb8ddbe214b8b95804c0b4b0fa195eb2758d",
      "md5" : "773da347a05bfacac2176b3f26010812",
      "name" : "ModuleName2-0.0.1.pom"
    } ],
    "excludedArtifacts" : [ ],
    "dependencies" : [ ]
  } ],
  "governance" : {
    "blackDuckProperties" : {
      "runChecks" : false,
      "includePublishedArtifacts" : false,
      "autoCreateMissingComponentRequests" : false,
      "autoDiscardStaleComponentRequests" : false
    }
  }
}

请注意我在 Artifactory 上看到的 "id" : "projectname-android-sdk.sdk:ModuleName1:unspecified"

projectname-android-sdk 是项目的名称,.sdk 是因为每个模块都位于项目根文件夹的子文件夹中,名为 sdkModuleName1 是模块之一的名称。

我已经部分解决了这个问题:我在调用 Artifactory 之前没有清除构建,而是在推送旧构建。我以为我正在标记 aar 并且重新构建它们并不重要。

现在我打电话给:

./gradlew clean assembleDebug

./gradlew artifactoryPublish

现在我正在重新构建,我得到了一个新的 build-info.json,里面有我期待的东西。 我还用谷歌搜索了 Artifactory 上的 Build 是什么,我找到了 this。我可以总结说我混淆了有关构建的信息和有关已发布工件的信息。

当我最后调用 ./gradlew artifactoryPublish 时,我看到:

[pool-79-thread-1] Deploying artifact: https://justatest4artifactory.jfrog.io/artifactory/gradle-dev-local/com/mygroup/ModuleName1/0.1.3/Nickname1-0.1.2.aar

其中 0.1.3buildInfo.build.number0.1.2 是工件的版本。