Need to capture activity of the UploadArchive and publish Gradle tasks - gradle

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

Related

In build.gradle how to extract and reuse Maven repository configuration?

I've got this build.gradle file:
repositories {
maven {
credentials {
username "$artifactory_user"
password "$artifactory_password"
}
url 'http://some.domain/artifactory/repo'
}
}
publishing {
repositories {
publications {
maven(MavenPublication) {
from components.java
}
}
maven {
credentials {
username "$artifactory_user"
password "$artifactory_password"
}
url 'http://some.domain/artifactory/repo'
}
}
}
I want to extract and reuse the Maven repository definition.
This is not working:
repositories {
mavenRepository()
}
publishing {
repositories {
publications {
maven(MavenPublication) {
from components.java
}
}
mavenRepository()
}
}
private void mavenRepository() {
maven {
credentials {
username "$artifactory_user"
password "$artifactory_password"
}
url 'http://some.domain/artifactory/repo'
}
}
It results in
Could not find method mavenRepository() for arguments [gradle-dev] on
repository container of type
org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.
How can I achieve this goal?
lets say you have a separate repositories.gradle file and name your maven repo..
maven {
name 'myrepo'
url 'https://xxxx'
credentials {
username = System.getenv("xxx") ?: "${xxx}"
password = System.getenv("xxx") ?: "${xxx}"
}
}
maven { name 'anotherrepo' }
then at root project level you can run:
apply from: "$rootDir/gradle/repositories.gradle"
This will populate repositories with all repos. Then to use populate a specific one somehwere else, you can use:
repositories.add(rootProject.repositories.getByName('myrepo'))
I found the answer on this blog page: https://themightyprogrammer.dev/snippet/reusing-gradle-repo
thanks to mightprogrammer!

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

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

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

Resources