Gradle mavenDeployer override artifact version command line - maven

I use gradle to upload built artifacts to our repository manager (Nexus).
I'm using the following command line:
./gradlew --stacktrace --info uploadArchives -PnexusUsername=de -PnexusPassword=np
In the build.gradle file, I have:
uploadArchives {
repositories {
mavenDeployer {
repository(url: "${nexusUrl}/content/repositories/releases") {
authentication(userName: nexusUsername, password: nexusPassword)
}
}
}
}
How can I override the default version from command line?

I found a quick way to support this.
In the build.gradle, I added and updated uploadArchives:
...
version = getVersion()
// Option to override Nexus version by setting env var CUSTOM_VERSION
def pomVersion = System.getenv("CUSTOM_VERSION") ?: version
...
...
uploadArchives {
repositories {
mavenDeployer {
repository(url: "${nexusUrl}/content/repositories/releases") {
authentication(userName: nexusUsername, password: nexusPassword)
}
pom.version = pomVersion
}
}
}
Now, I just need to set CUSTOM_VERSION before running the command line.
I hope this helps. More options are welcome!

Related

Unable to publish a gradle Jar artifact to a sonatype repository

I am following instructions in the Sonatype documentation for publishing a gradle project.
I have the following gradle version.
$ ./gradlew --version
------------------------------------------------------------
Gradle 7.6
------------------------------------------------------------
Build time: 2022-11-25 13:35:10 UTC
Revision: daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8
Kotlin: 1.7.10
Groovy: 3.0.13
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 1.8.0_181 (Oracle Corporation 25.181-b13)
OS: Mac OS X 10.16 x86_64
When I try to use the uploadArchives task as given in their example, I get the following error:
$ ./gradlew uploadArchives
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/ferozed/stash/libs/mask-json-field-transform/build.gradle' line: 167
* What went wrong:
A problem occurred evaluating root project 'mask-json-field-transform'.
> Could not find method uploadArchives() for arguments [build_6yphclnk6m8p3rtmq5h7m56li$_run_closure12#19fbeecd] on root project 'mask-json-field-transform' of type org.gradle.api.Project.
This is my build.gradle file
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'gradle.plugin.com.github.johnrengelman:shadow:7.1.2'
}
}
plugins {
id 'java' // so that we can use 'implementation', 'testImplementation' for dependencies
id 'maven-publish'
id 'signing'
}
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'com.github.johnrengelman.shadow'
repositories {
jcenter()
mavenCentral()
maven {
url = "https://packages.confluent.io/maven"
}
maven {
url = "https://jitpack.io"
}
}
group = 'io.github.ferozed.kafka.connect'
version = '0.1'
dependencies {
// Kafka
implementation group: 'org.apache.kafka', name: 'connect-api', version: '3.3.1'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.1'
implementation 'com.github.jcustenborder.kafka.connect:connect-utils:0.7.173'
implementation 'com.github.jcustenborder.kafka.connect:kafka-connect-transform-common:0.1.0.14'
//test
testImplementation(platform('org.junit:junit-bom:5.9.0'))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation("io.mockk:mockk:1.9.2")
}
sourceSets {
main {
java {
srcDirs = ["src/main/java"]
}
resources {
srcDirs = ["src/main/avro", "src/main/resources"]
}
}
test {
java {
srcDirs = ["src/test/java"]
}
}
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
tasks.withType(AbstractArchiveTask).configureEach {
preserveFileTimestamps = false
reproducibleFileOrder = true
}
plugins.withId("com.github.johnrengelman.shadow"){
//this block requires the java plugin to be applied first.
plugins.withId("java"){
shadowJar {
//We are overriding the default jar to be the shadow jar
classifier = null
exclude 'META-INF'
exclude 'META-INF/*.INF'
exclude 'META-INF/license/*'
}
jar {
manifest {
attributes(
'Built-By' : System.properties['user.name'],
'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
'Created-By' : "Gradle ${gradle.gradleVersion}",
'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
)
}
}
tasks.build.dependsOn tasks.shadowJar
tasks.shadowJar.mustRunAfter tasks.jar
tasks.shadowJar.mustRunAfter tasks.javadocJar
tasks.shadowJar.mustRunAfter tasks.sourcesJar
}
}
publishing {
publications {
mavenJava(MavenPublication) {
pom {
name = 'Mask Json Field Transform'
description = 'A kafka connect transform to remove the value of a sensitive field in a json document.'
url = 'https://github.com/ferozed/mask-json-field-transform'
properties = [
myProp: "value",
"prop.with.dots": "anotherValue"
]
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'ferozes'
name = 'Feroze Daud'
email = 'ferozed.oss#gmail.com'
}
}
scm {
connection = 'scm:git:git#github.com:ferozed/mask-json-field-transform.git'
developerConnection = 'scm:git:git#github.com:ferozed/mask-json-field-transform.git'
url = 'https://github.com/ferozed/mask-json-field-transform'
}
}
}
}
}
signing {
sign configurations.archives
sign publishing.publications.mavenJava
}
tasks.signArchives.dependsOn tasks.shadowJar
artifacts {
archives javadocJar, sourcesJar
}
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { deployment -> signing.signPom(deployment) }
repository(url: "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: ossrhUsername, password: ossrhPassword)
}
snapshotRepository(url: "https://s01.oss.sonatype.org/content/repositories/snapshots/") {
authentication(userName: ossrhUsername, password: ossrhPassword)
}
pom.project {
name 'Example Application'
packaging 'jar'
// optionally artifactId can be defined here
description 'A kafka connect transform to remove the value of a sensitive field in a json document.'
url 'https://github.com/ferozed/mask-json-field-transform'
scm {
connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'
developerConnection 'scm:svn:https://foo.googlecode.com/svn/trunk/'
url 'https://github.com/ferozed/mask-json-field-transform'
}
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'ferozed'
name 'Feroze Daud'
email 'feroz#gmail.com'
}
}
}
}
}
}
What am I doing wrong?
First and foremost, you may not need to 'fix the error' so to speak, in that it appears the problematic code MAY be able to be outright deleted. However...
To fix the error...
Could not find method uploadArchives() for arguments [build_6yphclnk6m8p3rtmq5h7m56li$_run_closure12#19fbeecd] on root project 'mask-json-field-transform' of type org.gradle.api.Project.
This is telling us that uploadArchives is not valid. See immediately below.
I believe you may need to use the maven plugin to access this functionality.
https://github.com/siddeshbg/gradle-uploadArchives/blob/master/README.md
to do this, add:
id 'maven'
to your plugins{}
block.
The above example explains that the maven plugin is responsible for doing this
The Maven plugin adds support for deploying artifacts to Maven repositories
It is worth considering also that the author of the above repository claims:
This is the legacy publishing mechanism, should not be used in newer
builds
Now... on to the better solution, forgetting the maven plugin, in favor of the maven-publish plugin you're already using...
I do not personally work with Maven often enough to know what other mechanisms one should use, but Google suggests that perhaps the maven-publish plugin you're using might do this WITHOUT the 'uploadArchives{}' portion of your code that is causing the error.
See:
https://docs.gradle.org/current/userguide/publishing_maven.html
Per this page, it would seem the entire uploadArchives block is unnecessary

How to publish to Nexus the artifact generated by a Gradle project in Jenkins

I have a Gradle project with the following uploadArchives configuration to publish its artifact to Nexus:
uploadArchives {
repositories {
mavenDeployer {
repository(url: 'http://releasesrepo')
snapshotRepository(url: 'http://snapshotsrepo')
}
}
}
As you see there are no credentials in this configuration.
This build will be run from Jenkins as a Freestyle project so I will just have to specify in Build - Invoke Gradle Script - Tasks -> build upload
What's the right way to configure the Nexus credentials from Jenkins and pass them to the Gradle taks (I don't want them in the Git repo with the source code)?
There are three different ways:
1.Using environmental variables
def nexusUser = hasProperty('nexusUsername') ? nexusUsername : System.getenv('nexusUsername')
def nexusPassword = hasProperty('nexusPassword') ? nexusPassword : System.getenv('nexusPassword')
def nexusReleaseUrl = hasProperty('nexusReleaseUrl') ? nexusReleaseUrl : System.getenv('nexusReleaseUrl')
def nexusSnapshotUrl = hasProperty('nexusSnapshotUrl') ? nexusSnapshotUrl : System.getenv('nexusSnapshotUrl')
repositories {
mavenDeployer {
repository(url: nexusReleaseUrl) {
authentication(userName: nexusUser, password: nexusPassword);
}
snapshotRepository(url: nexusSnapshotUrl) {
authentication(userName: nexusUser, password: nexusPassword);
}
}
}
2.Using ~/.gradle/gradle.properties (remember is under the user jenkins)
nexusUser=user
nexusPass=password
uploadArchives {
def nexusUser = "${nexusUsername}"
def nexusPassword = "${nexusPassword}"
repositories {
mavenDeployer {
repository(url: 'your nexus releases') {
authentication(userName: nexusUser, password: nexusPassword);
}
snapshotRepository(url: 'your nexus snapshot') {
authentication(userName: nexusUser, password: nexusPassword);
}
}
}
}
3.Using the global credentials
You have an example here (it is for Travis) if you want to have a look

Publish source jar when reading external .gradle file

I have a git submodule (of which I have no control of) in my project which contains file named publish.gradle. The contents of this file are:
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
version = System.getenv()['VERSION'] ?: version
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
artifactory {
contextUrl = repoBaseUrl
publish {
String publishRepoKey = version.endsWith('SNAPSHOT') ? repoKeySnapshot : repoKeyRelease
repository {
repoKey = publishRepoKey // The Artifactory repository key to publish to
username = System.getenv()['ARTIFACTORY_USERNAME'] ?: artifactoryUser // The publisher user name
password = System.getenv()['ARTIFACTORY_PASSWORD'] ?: artifactoryPassword // The publisher password
}
defaults {
publications('mavenJava')
publishArtifacts = true
publishPom = true
}
}
resolve {
repoKey = repoKey
}
}
In my build.gradle file I have the following line:
apply from: "${rootDir}/submodule/gradle/publish.gradle"
When I try to add the following line after this line:
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing.publications.mavenJava.artifact sourceJar {
classifier 'sources'
}
Then the correct things get published, except that the pom file in the repository only contains a <dependencyManagement/> section and all the dependencies are missing. When I remove the above lines from build.gradle the pom is correct.
Try to override the all publishing block with :
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}

Close and release artifact on maven central using gradle

I have this gradle script:
def configureUploadArtifacts(groupId, repoUrl, _packaging) {
def gpgKeyId = System.getenv('GPG_KEY_ID')
def gpgPassword = System.getenv('GPG_KEY_PASSWORD')
def gpgFile = System.getenv('PATH_TO_GPG_FILE')
project.group = groupId;
project.archivesBaseName = name
project.version = getVersionNameFromFile()
ext."signing.keyId" = gpgKeyId
ext."signing.password" = gpgPassword
ext."signing.secretKeyRingFile" = gpgFile
uploadArchives {
apply plugin: 'maven'
apply plugin: 'signing'
signing {
sign configurations.archives
}
def userName = System.getenv('OSSRH_USER_NAME');
def password = System.getenv('OSSRH_PASSWORD');
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: userName, password: password)
}
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
authentication(userName: userName, password: password)
}
pom.project {
name "${project.name}"
packaging "${_packaging}"
// optionally artifactId can be defined here
description 'A collection of core tools I use'
url "http://github.com/${repoUrl}"
scm {
connection "scm:git:git://github.com/${repoUrl}.git"
developerConnection "scm:git:ssh://github.com/${repoUrl}.git"
url "http://github.com/${repoUrl}/tree/master"
}
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'TacB0sS'
name 'My Name'
email 'My Email'
}
}
}
}
}
}
}
I use it on my Jenkins server and it works wonderfully.
I would like it also to close and release the artifacts...
How do I do that?
Solution was to add the following to the root build.gradle file:
ext."oss-releases.username" = System.getenv('OSSRH_USER_NAME')
ext."oss-releases.password" = System.getenv('OSSRH_PASSWORD')
ext."oss-releases.url" = "https://oss.sonatype.org/index.html#stagingRepositories"
apply plugin: 'nexus-workflow'
And run the following from the command line:
bash gradlew nexusStagingRelease
DONE!
You can use gradle-release on your script. It works similar as maven-release-plugin (removes SNAPSHOT from version, builds, create tags, deploys artifacts and updates to the next development version):
apply plugin: 'net.researchgate.release'
In Jenkins, using release plugin, you will need to configure the unattended release:
gradle release -Prelease.useAutomaticVersion=true \
-Prelease.releaseVersion=$VERSION \
-Prelease.newVersion=$NEXT_VERSION

How can I tell gradle to execute uploadArchives on a SNAPSHOT release?

I have the following in my build.gradle:
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
String keyFile = "/Users/myusername/.ssh/maven#myhost"
repository(url: "scp://myhost.com/var/maven/repository") {
authentication(userName: "maven", privateKey: keyFile)
}
}
}
}
This works great - with one caveat: It only uploads release builds. I know that I can specify snapshotRepository to get it to upload snapshots, but what I really want is to know how to invoke this behavior. I would like my snapshots to go to the same repo as the release builds (which I think is the default behavior), but when I do:
./gradlew uploadArchives or ./gradlew assembleDebug uploadArchives
it first builds the release buildType, and uploads this. How can I tell uploadArchives I want it to upload the snapshot build (i.e. debug)?
you need to put you versionName with -SNAPSHOT
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0-SNAPSHOT"
}
}
uploadArchives {
repositories.mavenDeployer {
snapshotRepository(url: '.../snapshots') {
authentication(userName: NULL_NEXUS_USER, password: NULL_NEXUS_PWD)
}
repository(url: '.../releases') {
authentication(userName: NULL_NEXUS_USER, password: NULL_NEXUS_PWD)
}
and then ./gradlew clean build uploadFiles will upload it to snaphots repository.
It took me some googling to find a solution so leaving this here for others:
You can make a new task dependent on the uploadArchives task but check the version and remove the dependsOn and disable the task if not a snapshot eg:
task uploadSnapshotArchives(dependsOn: uploadArchives) {
if (!version.endsWith("-SNAPSHOT")) {
enabled = false;
dependsOn = [];
}
}

Resources