Search code examples
gradleartifactorypublishing

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


I'm publishing an SDK composed of different modules to Artifactory, using gradle's publications and the Artifactory plugin.

Everything is published and the modules are all grouped in a build with the name and the version that I have specified in the gradle.property file:

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

The problem is that the modules are all published with the default name = module name and with not version and no dependencies at least what Artifactory shows me for the moduleid;number of artifacts; dependencies is something like this:

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

Here a snippet of the grade file for publishing (it is applied to each the build.gradle scripts for all the module:

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

        }
    }
}

In the project level build.gradle file I have:

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

        }
    }
}

After I publish I see a build-info.json file created in the build folder:

{
  "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
    }
  }
}

Please notice the "id" : "projectname-android-sdk.sdk:ModuleName1:unspecified" that is what I see on Artifactory.

projectname-android-sdk is the name of the project, .sdk comes from the fact that each module is in a subfolder, of the project's root folder, called sdk. ModuleName1 is the name of one of the modules.


Solution

  • I have partially fixed the issue: I was not clearing the build before calling Artifactory and I was pushing old builds. I thought that I was labelling the aar and that it wasn't important to build them again.

    Now I'm calling:

    ./gradlew clean assembleDebug

    ./gradlew artifactoryPublish

    Now that I'm building again, I get a new build-info.json with the things I was expecting. I have also googled what a Build is on Artifactory and I found this. I can summarise saying that I had confused the info about the build with the info about the published artifacts.

    When I call ./gradlew artifactoryPublish at the end I see:

    [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

    where 0.1.3 is the buildInfo.build.number and 0.1.2 is the version of the artifact.