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

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

Related

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?

Need to capture activity of the UploadArchive and publish Gradle tasks

I have a scenario where I need to capture below details in the init.gradle file.
Can we get all the activity of task ?
the Inputs params for UploadArchive and publish task, which repo is the artifact getting uploaded, all the GAV details …as POM can be customize within uploadArchive task.
We have applications running v3.5 to v6.3 versions of Gradle.
Can you please assist
Hi #PrasadU
Can we determine which deployment url, uploadArchive task will pick at runtime.
uploadArchive {
repositories {
mavenDeployer {
repository(url: ReleaseURL) {
authentication(userName: Username, password: Password)
}
snapshotRepository(url: SnapshotURL ) {
authentication(userName: Username, password: Password)
}
}
}
}
A task to list the same be made as below
publishing {
publications {
maven(MavenPublication) {
groupId = 'abc.xyz'
artifactId = 'overr-name'
version = '1.1-OV'
from components.java
}
}
repositories {
maven {
url = uri("$buildDir/repos/releases")
}
maven {
url = uri("$buildDir/repos/snaps")
}
}
}
task printML() {
doLast {
tasks.findAll {task ->
if (task.name.matches("publish.*PublicationToMavenLocal")) {
def publication = task.publicationInternal
println("Local => $publication.artifactId $publication.groupId $publication.version")
}
if (task.name.matches("publish.*PublicationTo.*Repository")) {
def publication = task.publicationInternal
println("Remote => $publication.artifactId $publication.groupId $publication.version $task.repository.url")
}
}
}
}
Sample output
> Task :printML
Remote => overr-name abc.xyz 1.1-OV file:/Users/projects/Store/combined-samples/build/repos/snaps
Local => overr-name abc.xyz 1.1-OV
Remote => overr-name abc.xyz 1.1-OV file:/Users/projects/Store/combined-samples/build/repos/releases

Gradle publish repository defaults or ignores

I have defined multiple repositories to publish to, but want the "gradle publish" job only to deploy to some of them.
E.g. in the following configuration I want that a "gradle publish" deploys the artifact to repo_a and repo_b but NOT repo_c.
A deploy to repo_c should only be done when the publishMavenJavaPublicationToRepo_cRepositoryjob is activated.
Is that somehow possible?
Thanks
publishing {
repositories {
maven {
url "https://repo_a/maven-releases/"
credentials {
username 'xxx'
password 'xxx'
}
name "repo_a"
}
maven {
url "https://repo_b/maven-releases/"
credentials {
username 'xxx'
password 'xxx'
}
name "repo_b"
}
maven {
url "https://repo_c/maven-releases/"
credentials {
username 'xxx'
password 'xxx'
}
name "repo_c"
}
}
publications {
mavenJava(MavenPublication) {
....
}
}
}
You can try the following code snippet to disable your publishMavenJavaPublicationToRepo_cRepository publishing task (reference):
afterEvaluate {
tasks.withType(PublishToMavenRepository) { task ->
if (task.repository.name == "repo_c") {
task.enabled = false
task.group = null
}
}
}

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

Gradle mavenDeployer override artifact version command line

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!

Resources