Reputation: 455
I have a gradle file for building a JAR and publishing it in a maven repository on nexus server.
build.gradle
filepublishing {
publications {
maven(MavenPublication) {
artifact shadowJar
}
}
repositories {
maven {
credentials {
username nexusUsername
password nexusPassword
}
if (IsSnapshot.toBoolean())
url "${nexusUrl}/repository/maven-snapshots/"
else
url "${nexusUrl}/repository/maven-releases/"
}
}
}
This task publishes a JAR file to the respective repository(snapshot/releases) based on the value of IsSnapshot
.
I have one more variable: DoPublish
. If DoPublish
is true
, then I want to publish the package; otherwise, I don't want to publish the package on nexus.
I am thinking I should set url
to an empty string in such cases. Are there any better suggestions?
Publishing tasks
generatePomFileFor{module1}Publication - Generates the Maven POM file for publication 'module1'.
generatePomFileFor{module2}Publication - Generates the Maven POM file for publication 'module2'.
generatePomFileForMavenPublication - Generates the Maven POM file for publication 'maven'.
publish - Publishes all publications produced by this project.
publish{module1}PublicationToMavenLocal - Publishes Maven publication 'module1' to the local Maven repository.
publish{module1}PublicationToMavenRepository - Publishes Maven publication 'module1' to Maven repository 'maven'.
publish{module2}PublicationToMavenLocal - Publishes Maven publication 'module2' to the local Maven repository.
publish{module2}PublicationToMavenRepository - Publishes Maven publication 'module2' to Maven repository 'maven'.
publishMavenPublicationToMavenLocal - Publishes Maven publication 'maven' to the local Maven repository.
publishMavenPublicationToMavenRepository - Publishes Maven publication 'maven' to Maven repository 'maven'.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.
Upvotes: 7
Views: 7499
Reputation: 10082
Tasks can be conditionally deactivated using onlyIf
as described in the gradle documentation: https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:using_a_predicate
The task in question here is publishMavenJavaPublicationToMavenRepository
, as described here https://docs.gradle.org/current/userguide/publishing_maven.html, and this is the one that needs to be disabled:
publishMavenJavaPublicationToMavenRepository.onlyIf { IsSnapshot.toBoolean() }
Because the task publishMavenJavaPublicationToMavenRepository
is dynamically generated upon a successful publishing configuration it may not be available when the build script is parsed and evaluated resulting in an exception. In this case, one can update it upon completing the task gragh:
gradle.taskGraph.whenReady { graph ->
publishMavenJavaPublicationToMavenRepository.onlyIf { IsSnapshot.toBoolean() }
}
It may be a different task if you use a different publishing plugin, but the idea is the same
Upvotes: 8