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

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.

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.

Related

Gradle common repositories for multi module project

I have below project setup for gradle scripts, but I would like to keep one single common repository file for both main and subprojects.
apply from : "$rootDir/gradle/repositories.gradle" does not work inside build.gradle for this purpose. It would work only if I declare repositories again inside build.gradle
**build.gradle**
--------------------------
{
repositories {
maven {
url "https://myprivate.repo.com/artifactory"
credentials {
username = "user1"
password = "psswd"
}
}
}
dependencies {
# some list of dependencies
}
}
**gradle/repositories.gradle**
-------------------------
repositories {
maven {
url "https://myprivate.repo.com/artifactory"
credentials {
username = "user1"
password = "psswd"
}
}
}
**subprojects.gradle**
--------------------------
subprojects {
apply from : "$rootDir/gradle/repositories.gradle"
}

Maven publish plugin complains no username provided but its there

I am trying to push a jar to artifactory, i get the following error from the gradle script:
But the username is there, is hardcoded to "aws":
publishing {
publications {
publicMyArtifact(MavenPublication) {
groupId "com.company.project"
artifactId "artifact"
version = "1.0"
afterEvaluate {
artifact "repo/path/*.jar"
}
}
}
repositories {
maven {
name "reponame"
url "https://accountnumber.d.codeartifact.eu-central-1.amazonaws.com/maven"
credentials {
username = "aws"
password = System.getenv("CODEARTIFACT_AUTH_TOKEN")
}
}
}
}
I invoke this task with the following line in jenkins: sh "./gradlew publish"
Any idea what could be possibly the reason?

Spring Gradle task for queryDsl fails after OpenApiGenerate

Spring project using Gradle. I'm using OpenApiGenerate in combination with QueryDsl (to use with Spring MongoDB).
gradle clean build fails sometimes locally and always on Gitlab-CI:
Successfully generated code to task ':backend:openApiGenerate' property 'outputDir'
34 errors
> Task :backend:querydsl FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':backend:querydsl'.
QueryDsl fails because it can't find sources generated by OpenApiGenerate:
public class SomethingController implements SomethingApi {
^
Here's the gradle build file:
// QueryDSL
configurations {
querydslapt.extendsFrom compileClasspath
}
dependencies {
querydslapt 'com.querydsl:querydsl-apt:4.3.1'
}
task querydsl(type: JavaCompile, group: 'build', description: 'Generate the QueryDSL query types') {
source = sourceSets.main.java
classpath = configurations.compile + configurations.querydslapt
options.annotationProcessorPath = configurations.compile + configurations.querydslapt
options.compilerArgs = [
'-proc:only', //only annotations
'-processor', 'org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor'
]
destinationDir = file("${buildDir}/generated/src/main/java")
}
compileJava.dependsOn querydsl
// OpenAPI
openApiValidate {
inputSpec = "${rootDir}/openapi/specifications/schema.yaml".toString()
}
openApiGenerate {
generatorName = "spring"
library = "spring-boot"
inputSpec = "${rootDir}/openapi/specifications/schema.yaml".toString()
outputDir = "${buildDir}/generated".toString()
systemProperties = [
modelDocs : "false",
models : "",
apis : "",
supportingFiles: "false"
]
configOptions = [
useOptional : "true",
swaggerDocketConfig : "false",
performBeanValidation: "false",
useBeanValidation : "false",
useTags : "true",
singleContentTypes : "true",
basePackage : "...api",
configPackage : "...api",
title : rootProject.name,
java8 : "false",
dateLibrary : "java8",
serializableModel : "true",
artifactId : rootProject.name,
apiPackage : "...api",
modelPackage : "...model",
invokerPackage : "...api",
interfaceOnly : "true"
]
}
compileJava.dependsOn 'openApiGenerate'
Further changes that did not help:
Removed
compileJava.dependsOn 'openApiGenerate'
compileJava.dependsOn querydsl
and added
compileJava.dependsOn querydsl
querydsl.mustRunAfter 'openApiGenerate'
Does anyone have an idea? specifically on why does this sometimes work locally and sometimes not?
Is the output dir of openApiGenerate task is added somehow to src dir? If not, you should have
sourceSets{
main{
java{
dir openApiGenerate.outputDir
}}
}
What helped was changing destinationDir of querydsl to something else:
destinationDir = file("${buildDir}/generated/src/main/java")
to
destinationDir = file("${buildDir}/generated/src/main/java/querydsl")
=> There seems to have been a locking state enforced by openApiGenerate on the initial destinationDir since it was shared by both tasks.

Gradle - two plugins have the same root block name, which resolve conflict

I'm trying to use 2 different plugins that have the same root block name, what's causing conflict and error in the build process.
The 2 plugins are defined in the build.gradle :
plugins {
id 'java'
id 'application'
id 'com.benjaminsproule.swagger' version '1.0.8'
id 'org.detoeuf.swagger-codegen' version '1.7.4'
}
apply plugin: 'org.detoeuf.swagger-codegen'
version '1.0-SNAPSHOT'
mainClassName = 'ServiceMain'
swagger {
apiSource {
springmvc = true
locations = ['com.google.charger']
schemes = ['https']
info {
title = 'Swagger Gradle Plugin Sample'
version = 'v1'
}
swaggerDirectory = 'swagger'
swaggerFileName = 'charger-service-api-swagger'
attachSwaggerArtifact = true
}
}
swagger {
inputSpec = "${project.projectDir}/swagger/charger-service-api-swagger.json"
outputDir = file("${project.projectDir}/../charger-server-api-client/")
lang = 'java'
additionalProperties = [
'invokerPackage' : 'com.google.ev.charger.server',
'modelPackage' : 'com.google.ev.charger.server.model',
'apiPackage' : 'com.google.ev.charger.server.api',
'dateLibrary' : 'joda',
'groupId' : 'com.google.ev',
'artifactId' : 'charger.server',
'artifactVersion' : '1.0.0',
'hideGenerationTimestamp': 'true'
]
}
sourceSets {
swagger {
java {
srcDir file("${project.buildDir.path}/swagger/src/main/java")
}
}
}
As you can see both plugins starts with the same block name (swagger),
Is there any way to specify the plugin for each configuration?
I ended up using code to resolve this issue,
I am using a property called "CreateSwagger" that I'm passing (either from CLI or from gradle.properties file) to separate the 2 scenarios.
The issue I had is that the plugins {} block is not allowed inside the if that evaluates the condition so I needed to use the "old" legacy plugin " apply plugin:"
This is how my build.gradle ended up looking:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.benjaminsproule:swagger-gradle-plugin:1.0.8"
classpath "gradle.plugin.org.detoeuf:swagger-codegen-plugin:1.7.4"
}
}
plugins {
id 'java'
id 'application'
id 'maven'
}
if(project.properties.containsKey('CreateSwagger')){
apply plugin: "com.benjaminsproule.swagger"
} else {
apply plugin: 'org.detoeuf.swagger-codegen'
}
version '1.0-SNAPSHOT'
mainClassName = 'EVChargerRestServiceMain'
if(project.properties.containsKey('CreateSwagger')){
swagger {
apiSource {
springmvc = true
locations = ['com.google.charger']
schemes = ['https']
info {
title = 'Swagger Gradle Plugin Sample'
version = 'v1'
}
swaggerDirectory = 'swagger'
swaggerFileName = 'charger-service-api-swagger'
attachSwaggerArtifact = true
}
}
}
else {
swagger {
inputSpec = "${project.projectDir}/swagger/charger-service-api-swagger.json"
outputDir = file("${project.projectDir}/../charger-server-api-client/")
lang = 'java'
additionalProperties = [
'invokerPackage' : 'com.google.ev.charger.server',
'modelPackage' : 'com.google.ev.charger.server.model',
'apiPackage' : 'com.google.ev.charger.server.api',
'dateLibrary' : 'joda',
'groupId' : 'com.google.ev',
'artifactId' : 'charger.server',
'artifactVersion' : '1.0.0',
'hideGenerationTimestamp': 'true'
]
}
}

Using gradle with a local artifactory repository

I am new to gradle and artifactory both.
What I want to accomplish is to have separate projects with each project creating a jar file that will be used by other projects in the same application.
For example, I have a utility project that has, wait for it..., utility classes. I then have a services project with the, that's right, services. The services use the utilities to accomplish some of their work.
I've spent several hours and finally have the utility project committing to my artifactory repository using this script:
buildscript {
repositories {
mavenLocal()
ivy {
url 'http://picard:8080/artifactory/plugins-release'
}
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
}
}
apply plugin: 'java'
apply plugin: "com.jfrog.artifactory"
archivesBaseName = 'heavyweight-software-util'
repositories {
mavenCentral()
ivy {
url 'http://picard:8080/artifactory/plugins-release'
}
}
dependencies {
testCompile("junit:junit:4.11")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.8'
}
artifactory {
contextUrl = "http://picard:8080/artifactory" //The base Artifactory URL if not overridden by the publisher/resolver
publish {
repository {
repoKey = 'libs-release-local'
username = 'xxxx'
password = "xxxx"
maven = false
ivy {
ivyLayout = '[organization]/[module]/[revision]/[type]s/ivy-[revision].xml'
artifactLayout = '[organization]/[module]/[revision]/[type]s/[module](-[classifier])-[revision].[ext]'
mavenCompatible = false
}
}
}
resolve {
repository {
repoKey = 'libs-release'
username = 'xxxx'
password = "xxxx"
maven = false
ivy {
ivyLayout = '[organization]/[module]/[revision]/[type]s/ivy-[revision].xml'
artifactLayout = '[organization]/[module]/[revision]/[type]s/[module](-[classifier])-[revision].[ext]'
mavenCompatible = false
}
}
}
}
When I run this I get the following:
C:\Users\thom\git\utility\Utility>gradle artifactoryPublish
[buildinfo] Not using buildInfo properties file for this build.
:artifactoryPublish
Deploying build descriptor to: http://picard:8080/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://picard:8080/a
rtifactory/webapp/builds/Utility/1436807848026/2015-07-13T13:17:27.704-0400/
BUILD SUCCESSFUL
Total time: 6.874 secs
Hurrah! Or, so I thought.
Because as soon as this wrapped up, I thought, "Now how do I get this file out." I looked at the links above and they're there, but I can't see how this is a jar file. I tried looking at libs-release-local in my tree browser, but it shows 0 artifacts.
Here's what I found under build info JSON under the build:
{
"version" : "1.0.1",
"name" : "Utility",
"number" : "1436807848026",
"type" : "GRADLE",
"buildAgent" : {
"name" : "Gradle",
"version" : "2.4"
},
"agent" : {
"name" : "Gradle",
"version" : "2.4"
},
"started" : "2015-07-13T13:17:27.704-0400",
"durationMillis" : 474,
"principal" : "thom",
"artifactoryPrincipal" : "admin",
"licenseControl" : {
"runChecks" : false,
"includePublishedArtifacts" : false,
"autoDiscover" : false,
"licenseViolationsRecipientsList" : "",
"scopesList" : ""
},
"buildRetention" : {
"count" : -1,
"deleteBuildArtifacts" : true,
"buildNumbersNotToBeDiscarded" : [ ]
},
"governance" : {
"blackDuckProperties" : {
"runChecks" : false,
"includePublishedArtifacts" : false,
"autoCreateMissingComponentRequests" : false,
"autoDiscardStaleComponentRequests" : false
}
}
}
I've googled and researched and can't seem to figure out how to make use of the jar file I have committed to my repository.
OK, after reading: https://docs.gradle.org/current/userguide/publishing_ivy.html and https://docs.gradle.org/current/userguide/artifact_management.html and finally, http://forums.jfrog.org/405-HTTP-method-PUT-not-supported-td5786632.html I have pieced together an answer to my question. I have attached the build script that performs the upload properly...
buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
}
}
apply plugin: 'java'
apply plugin: 'ivy-publish'
archivesBaseName = 'heavyweight-software-util'
repositories {
mavenCentral()
ivy {
url 'http://picard:8080/artifactory/plugins-release'
}
}
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
testCompile("junit:junit:4.11")
}
publishing {
publications {
ivy(IvyPublication) {
organisation 'com.heavyweightsoftware'
module 'heavyweight-util'
revision '1.0'
from components.java
}
}
repositories {
ivy {
url 'http://picard:8080/artifactory/libs-release-local'
credentials {
username "xxxxx"
password "xxxxx"
}
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.8'
}
Now that I know that the publication is working correctly, I should be able to use the repository without issue.

Resources