How to publish(deploy) multiple zip files to artifactory using gradle? - gradle

I would like to upload multiple zip files(from different directories) to artifactory using gradle script. I was able to upload single distribution using below code.
distributions {
main {
baseName = 'sample'
contents {
from{'src/main/dist/sample'}
}
}
}
publishing {
repositories {
maven {
url "${artifactoryURL}/myFiles"
credentials {
username artifactoryUser
password artifactoryPassword
}
}
}
publications {
distribution(MavenPublication) {
groupId 'com.test'
artifactId 'sample'
version version
artifact (distZip) {
}
}
}
}
Please help in uploading multiple zip files from different source ("src/main/dist/second").

This is full example of build.gragle. To publish zips you should run distZip artifactoryPublish
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
}
}
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
apply plugin: 'distribution'
//Defining list of our distributions, every value is map which contains value and version of every distribution
def dists = [
[name:'sample', version: 1],
[name:'second', version: 1]
]
distributions {
//Creating distribution from each value of the distributions list defined above
dists.each { dist ->
"$dist.name" {
baseName = "$dist.name"
contents {
from{"src/main/dist/${dist.name}"}
}
}
}
}
//Every new distribution creates task "${distributionName}DistZip" so we make posible to run them with base task distZip
distZip.dependsOn {
tasks.findAll{ task -> task.name.endsWith('DistZip') }
}
publishing {
publications {
//Again iterating list to make publication for every distribution
dists.each { dist ->
//Every publication has name of the distibution
"$dist.name"(MavenPublication) {
groupId "test"
version = dist.version
artifactId = dist.name
artifact("$buildDir/distributions/${dist.name}.zip")
}
}
}
}
artifactory {
contextUrl = "$URL"
publish {
repository {
repoKey = 'REPO_KEY'
username = USER_NAME
password = PASSWORD
}
defaults {
//Publish every distribution to Artifactory
dists.each { dist ->
publications(dist.name)
}
}
}
}

Related

Could not PUT https://oss.sonatype.org/service/local/staging/deploy/maven2/bom-plugin/

I'm trying to publish my first Gradle plugin to maven central (sonatype), but I'm receiving the following error:
Execution failed for task ':publishBom-pluginPluginMarkerMavenPublicationToMavenRepository'.
> Failed to publish publication 'bom-pluginPluginMarkerMaven' to repository 'maven'
> Could not PUT 'https://oss.sonatype.org/service/local/staging/deploy/maven2/bom-plugin/bom-plugin.gradle.plugin/0.3/bom-plugin.gradle.plugin-0.3.pom'.
Received status code 400 from server: Bad Request
It seems that for some reason, the publishing task is trying to publish my plugin at the root of the sonatype repository, it works when I do execute the task publishToMavenLocal, however, published this strange file in my m2 local: /.m2/repository/bom-plugin/bom-plugin.gradle.plugin and also publish the plugin here /.m2/repository/dev/thiagosouto/bom-plugin/0.3/
I think this is causing the issue but I don't have any clue to solve this problem.
My build.gradle.kts file
plugins {
java
`java-gradle-plugin`
`kotlin-dsl`
`maven-publish`
signing
kotlin("jvm") version "1.4.31"
id("com.gradle.plugin-publish") version "0.13.0"
}
group "dev.thiagosouto"
version "0.3"
buildscript {
repositories {
mavenCentral()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
}
gradlePlugin {
plugins {
create("bom-plugin") {
id = "bom-plugin"
implementationClass = "dev.thiagosouto.plugins.bom.BomPlugin"
}
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.squareup:kotlinpoet:1.7.2")
testImplementation("com.google.truth:truth:1.1.2")
testImplementation("junit:junit:4.13.2")
testImplementation("dev.thiagosouto:file-butler:0.3.0")
}
sourceSets.main {
java.srcDirs("src/main/kotlin")
}
sourceSets.test {
java.srcDirs("src/test/kotlin")
}
tasks.withType<GenerateModuleMetadata> {
enabled = false
}
publishing {
val ossrhUsername: String by project
val ossrhPassword: String by project
repositories {
maven(url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
credentials {
username = ossrhUsername
password = ossrhPassword
}
}
}
publications {
group = "dev.thiagosouto"
version = "0.3"
create<MavenPublication>("mavenJava") {
pom {
name.set("bom-plugin")
description.set("A library to help apply tdd through help functions");
url.set("https://thiagosouto.dev")
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
scm {
connection.set("scm:git:git://github.com/othiagosouto/bom-plugin.git/")
developerConnection.set("scm:git:ssh://github.com:othiagosouto/bom-plugin.git")
url.set("https://github.com/othiagosouto/bom-plugin")
}
developers {
developer {
id.set("othiagosouto")
name.set("Thiago Souto silva de barros Santos")
email.set("soutosss#gmail.com")
}
}
}
}
}
}
afterEvaluate {
signing {
sign(publishing.publications["mavenJava"])
}
}

how to publish a jar to NEXUS repository

I want to build a jar out of my Gradle project and push it to the nexus repository. As part of this, I created a Jenkins file and added a task "publishing" task in build.gradle.
My Jenkins file:
pipeline {
agent any
environment {
NEXUS = credentials('nexus-user')
}
options {
ansiColor('xterm')
buildDiscarder logRotator(daysToKeepStr: '30', numToKeepStr: '100')
}
triggers { pollSCM('H/5 * * * *') }
stages {
stage('Checkout'){
steps { checkout scm }
}
stage('Build') {
steps { sh "./gradlew assemble" }
}
stage('deploy') {
steps {
sh "gradle -Duser.home=\"$WORKSPACE\" --gradle-user-home=\"$WORKSPACE/.gradle\" -PnexusUsername=$NEXUS_USR -PnexusPassword=$NEXUS_PSW publish"
}
}
}
}
And build.gradle
publishing {
publications{
maven(MavenPublication){
artifactId = "testApp"
from components.java
}
}
repositories {
maven {
url = "http://localhost:8081/nexus/content/repositories/${version.endsWith('-SNAPSHOT') ? "snapshots" : "releases"}"
credentials {
username = "Dont know how to pass the username here"
password = "Dont know how to pass the password here"
}
}
}
}
Could anyone tell me how to get the username and password from Gradle and set here for publishing the jar to nexus.
Answer is:
publishing {
publications{
maven(MavenPublication){
artifactId = "testApp"
from components.java
}
}
repositories {
maven {
url = "http://localhost:8081/nexus/content/repositories/${version.endsWith('-SNAPSHOT') ? "snapshots" : "releases"}"
credentials {
username = "nexusUsername"
password = "nexusPassword"
}
}
}
}

Kotlin Multiplatform Library Project Upload Issue - POM Multiple Artifacts

We have a Kotlin multi-platform library project with a build script which builds for various platforms, i.e. common, JS & JVM, using the "old-style" sub-project structure and processes. Under this system, the artifacts produced can be successfully uploaded to our Sonatype Nexus repo (the root build.gradle is included below (1)).
We are now trying to convert to the "new-style" Kotlin multi-platform approach/structure (Building Multiplatform Projects with Gradle), but we are getting the following error when we try to upload the artifacts to the repo (the modified root build.gradle included below (2)):
BUILD FAILED in 0s
8 actionable tasks: 1 executed, 7 up-to-date
A POM cannot have multiple artifacts with the same type and classifier. Already have MavenArtifact objjson-metadata:jar.asc:asc:null, trying to add MavenArtifact objjson-metadata:jar.asc:asc:null.
10:36:52: Task execution finished 'uploadArchives'.
Does anyone know why we get this error?
The settings for each artifact created during the build seem to need to be updated in the script, but we are not sure how to do that.
Thanks in advance!
(1) Original ("old-style" multi-platform) root build.gradle:
buildscript {
ext.kotlinVersion = '1.2.61'
repositories {
jcenter()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath 'com.bmuschko:gradle-nexus-plugin:2.3.1'
}
}
plugins {
id 'io.codearte.nexus-staging' version '0.11.0'
}
allprojects {
group 'nz.salect.objJSON'
version '0.24-SNAPSHOT'
}
ext {
libraries = [
kotlin_stdlib : "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion",
kotlin_stdlib_common : "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion",
kotlin_stdlib_js : "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion",
kotlin_reflect_lib : "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion",
kotlin_test : "org.jetbrains.kotlin:kotlin-test:$kotlinVersion",
kotlin_test_common : "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion",
kotlin_test_js : "org.jetbrains.kotlin:kotlin-test-js:$kotlinVersion",
kotlin_test_annotations_common: "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion",
kotlin_test_junit : "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion",
]
}
subprojects {
repositories {
jcenter()
mavenCentral()
}
//Nexus/Maven upload settings
apply plugin: 'com.bmuschko.nexus'
modifyPom {
project {
name 'objJSON'
description 'A JSON serialization library fully implemented in Kotlin.'
url 'https://bitbucket.org/objdict/objjson'
inceptionYear '2016'
scm {
url 'https://bitbucket.org/objdict/objjson'
connection 'scm:https://bitbucket.org/objdict/objjson.git'
developerConnection 'scm:git://bitbucket.org/objdict/objjson.git'
}
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
developers {
developer {
id 'xxx'
name 'xxx'
email 'xxx'
}
}
}
}
extraArchive {
sources = true
tests = true
javadoc = true
}
nexus {
sign = true
repositoryUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
snapshotRepositoryUrl = 'https://oss.sonatype.org/content/repositories/snapshots/'
}
//End Nexus/Maven upload settings
}
nexusStaging {
packageGroup = "nz.salect"
}
(2) Updated ("new-style" multi-platform) root build.gradle:
buildscript {
ext.kotlinVersion = "1.3.11"
repositories {
jcenter()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
classpath("com.bmuschko:gradle-nexus-plugin:2.3.1")
}
}
plugins {
// from the multiplatform reference file
id "kotlin-multiplatform" version "1.3.11"
id "maven-publish"
id "io.codearte.nexus-staging" version "0.11.0"
//Nexus/Maven upload settings
//id "bmuschko.nexus"
}
//Nexus/Maven upload settings
apply plugin: "com.bmuschko.nexus"
//ext { - all moved to buildSrc
// libraries = [
// kotlin_stdlib: "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion",
// kotlin_stdlib_common: "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion",
// kotlin_stdlib_js: "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion",
//
// kotlin_reflect_lib: "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion",
//
// kotlin_test: "org.jetbrains.kotlin:kotlin-test:$kotlinVersion",
// kotlin_test_common: "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion",
// kotlin_test_js: "org.jetbrains.kotlin:kotlin-test-js:$kotlinVersion",
// kotlin_test_annotations_common: "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion",
//
// kotlin_test_junit: "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion",
// ]
//}
allprojects {
group "nz.salect.objJSON"
version Share.version
repositories {
jcenter()
mavenCentral()
}
modifyPom {
project {
name "objJSON"
description "A JSON serialization library fully implemented in Kotlin."
url "https://bitbucket.org/objdict/objjson"
inceptionYear "2016"
scm {
url "https://bitbucket.org/objdict/objjson"
connection "scm:https://bitbucket.org/objdict/objjson.git"
developerConnection "scm:git://bitbucket.org/objdict/objjson.git"
}
licenses {
license {
name "The Apache Software License, Version 2.0"
url "http://www.apache.org/licenses/LICENSE-2.0.txt"
distribution "repo"
}
}
developers {
developer {
id "xxx"
name "xxx"
email "xxx"
}
}
}
}
extraArchive {
sources = true
tests = true
javadoc = true
}
nexus {
sign = true
repositoryUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
snapshotRepositoryUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
}
//End Nexus/Maven upload settings
}
nexusStaging {
packageGroup = "nz.salect"
}
repositories {
mavenCentral()
}
kotlin {
targets {
fromPreset(presets.jvm, "jvm") {
mavenPublication {
artifactId = "objjson-jvm"
}
}
fromPreset(presets.js, "js") {
mavenPublication {
artifactId = "objjson-js"
}
}
// For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
// For Linux, preset should be changed to e.g. presets.linuxX64
// For MacOS, preset should be changed to e.g. presets.macosX64
//fromPreset(presets.mingwX64, "mingw")
}
sourceSets {
commonMain {
dependencies {
implementation("$Libraries.kotlin_stdlib_common")
implementation("$Libraries.kotlin_reflect_lib")
}
}
commonTest {
dependencies {
implementation("$Libraries.kotlin_test_common")
implementation("$Libraries.kotlin_test_annotations_common")
}
}
jvmMain {
dependencies {
implementation("$Libraries.kotlin_stdlib")
implementation("$Libraries.kotlin_reflect_lib")
}
}
jvmTest {
dependencies {
implementation("$Libraries.kotlin_test")
implementation("$Libraries.kotlin_test_junit")
}
}
jsMain {
dependencies {
implementation("$Libraries.kotlin_stdlib_js")
}
}
jsTest {
dependencies {
implementation("$Libraries.kotlin_test_js")
}
}
mingwMain {
}
mingwTest {
}
}
}

com.jfrog.artifactory gradle plugin 401 Unauthorised

I have previously successfully set up bintray and artifactory accounts to publish snapshot versions to the OSS JFrog Artifactory repository, but after setting up a GitHub/Bintray/Artifactory organisation under the same user, I am unable to publish snapshots.
When attempting to run
./gradlew artifactoryPublish -Dsnapshot=true -DbintrayUser=myBintrayUser -DbintrayKey=myBintrayApiKey -DbuildNumber=#
I get the following error:
java.io.IOException: Failed to deploy file. Status code: 401 Response message: Artifactory returned the following errors:
Unauthorized Status code: 401
I've tried using both bintray users (my personal and the organisation) but get the same response. I've also tried regenerating a new API key at https://bintray.com/profile/edit, but has not worked (and now also seems to be out of sync with the key at https://oss.jfrog.org/artifactory/webapp/#/profile) which I can't edit.
The build.gradle file is:
buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
}
plugins {
id 'java-library'
id 'maven'
id 'maven-publish'
// Automatic SEMVER
// ./gradlew release
id 'net.vivin.gradle-semantic-build-versioning' version '4.0.0' apply false
// SNAPSHOT publishing to oss-jfrog-artifactory
// ./gradlew artifactoryPublish -Dsnapshot=true -DbintrayUser=<YOUR_USER_NAME> -DbintrayKey=<YOUR_API_KEY> -DbuildNumber=NNN
id 'com.jfrog.artifactory' version '4.6.2'
// RELEASE publishing to bintray
// ./gradlew bintrayUpload -DbintrayUser=<YOUR_USER_NAME> -DbintrayKey=<YOUR_API_KEY>
id 'com.jfrog.bintray' version '1.8.1'
}
wrapper.gradleVersion = '4.5.1'
def groupName = 'noxtech'
group = 'uk.co.noxtech'
archivesBaseName = 'noxtech-java-utils'
description = 'Assorted Java 8 utilities'
def projectUrl = "https://github.com/noxtech/noxtech-java-utils"
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
api 'joda-time:joda-time:2.9.9'
implementation 'org.projectlombok:lombok:1.16.20'
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest-all:1.3'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
javadoc.failOnError = false
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
def pomConfig = {
licenses {
license {
name "The Apache Software License, Version 2.0"
url "http://www.apache.org/licenses/LICENSE-2.0.txt"
distribution "repo"
}
}
scm {
url projectUrl
}
}
publishing {
publications {
mavenPublication(MavenPublication) {
from components.java
artifact sourcesJar {
classifier "sources"
}
artifact javadocJar {
classifier "javadoc"
}
groupId = project.group
artifactId = project.archivesBaseName
version = project.version.toString()
pom.withXml {
def root = asNode()
root.appendNode('description', project.description)
root.appendNode('name', project.name)
root.appendNode('url', projectUrl)
root.children().last() + pomConfig
}
}
}
repositories {
maven {
// change to point to your repo, e.g. http://my.org/repo
url "$buildDir/repo"
}
}
}
bintray {
user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
key = project.hasProperty('bintrayKey') ? project.property('bintrayKey') : System.getenv('BINTRAY_KEY')
publications = ['mavenPublication']
pkg {
repo = "maven"
name = project.archivesBaseName
userOrg = groupName
licenses = ['Apache-2.0']
websiteUrl = projectUrl
vcsUrl = projectUrl + '.git'
issueTrackerUrl = projectUrl + '/issues'
version {
name = project.version.toString()
desc = project.description
vcsTag = project.version.toString()
released = new Date()
}
}
}
artifactory {
contextUrl = 'http://oss.jfrog.org'
publish {
repository {
repoKey = 'oss-snapshot-local'
username = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
password = project.hasProperty('bintrayKey') ? project.property('bintrayKey') : System.getenv('BINTRAY_KEY')
}
defaults {
publications('mavenPublication')
publishArtifacts = true
publishPom = true
}
}
resolve {
repoKey = 'jcenter'
}
clientConfig.info.setBuildNumber(project.hasProperty('buildNumber') ? project.property('buildNumber') : System.getenv('BUILD_NUMBER'))
}
This turned out to be a simple solution. When moving over to use the organisation from the personal account on CircleCI, the environment variables were lost.

Gradle build - Maven Modello

I have a Maven build which uses maven-modello (1.4) to generate Java classes/XSD's etc. from an description file (modello file). I'm searching for a possible solution in Gradle to solve the same problem.
I haven't test it, but can something like that do the trick:
import org.codehaus.modello.Modello
// Dependencies
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.codehaus.modello:modello-maven-plugin:1.5'
}
}
// Execution
task modello << {
buildDir.mkdirs()
file("$projectDir/models").eachFile { modelFile ->
if (modelFile.name.endsWith('.mdo')) {
new Modello().generate(modelFile.newReader(), generator, parameters)
}
}
}
// Configuration
modello.ext {
generator = 'java'
parameters = new Properties()
parameters.'modello.output.directory' = buildDir.absoluteFile
parameters.'modello.version' = '1.5'
parameters.'modello.package.with.version' = false
parameters.'modello.output.useJava5' = true
parameters.'modello.output.encoding' = 'UTF-8'
}

Resources