From b8710cec19d1ae3d0a09e0d95061b3638a3d1142 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 7 Apr 2022 16:47:47 -0500 Subject: [PATCH] #143 - Add ability to opt out of automatically adding a Maven artifact repository in subproject --- .../publishplugin/NexusPublishPlugin.kt | 7 +++-- .../ProjectNexusPublishingExtension.kt | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/io/github/gradlenexus/publishplugin/ProjectNexusPublishingExtension.kt diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/NexusPublishPlugin.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/NexusPublishPlugin.kt index f5282f1a..151c7b98 100644 --- a/src/main/kotlin/io/github/gradlenexus/publishplugin/NexusPublishPlugin.kt +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/NexusPublishPlugin.kt @@ -127,6 +127,7 @@ class NexusPublishPlugin : Plugin { allprojects { val publishingProject = this plugins.withId("maven-publish") { + val projectExtension = publishingProject.extensions.create(ProjectNexusPublishingExtension.NAME, project) val nexusRepositories = addMavenRepositories(publishingProject, extension, registry) nexusRepositories.forEach { (nexusRepo, mavenRepo) -> val initializeTask = rootProject.tasks.named("initialize${nexusRepo.capitalizedName}StagingRepository") @@ -142,7 +143,7 @@ class NexusPublishPlugin : Plugin { releaseTask { mustRunAfter(publishAllTask) } - configureTaskDependencies(publishingProject, initializeTask, publishAllTask, closeTask, releaseTask, mavenRepo) + configureTaskDependencies(publishingProject, initializeTask, publishAllTask, closeTask, releaseTask, mavenRepo, projectExtension) } } } @@ -181,7 +182,8 @@ class NexusPublishPlugin : Plugin { publishAllTask: TaskProvider, closeTask: TaskProvider, releaseTask: TaskProvider, - mavenRepo: MavenArtifactRepository + mavenRepo: MavenArtifactRepository, + projectExtension: ProjectNexusPublishingExtension ) { val mavenPublications = project.the().publications.withType() mavenPublications.configureEach { @@ -190,6 +192,7 @@ class NexusPublishPlugin : Plugin { "publish${publication.name.capitalize()}PublicationTo${mavenRepo.name.capitalize()}Repository" ) publishTask { + onlyIf { projectExtension.enabled.getOrElse(true) } dependsOn(initializeTask) doFirst { logger.info("Uploading to {}", repository.url) } } diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/ProjectNexusPublishingExtension.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/ProjectNexusPublishingExtension.kt new file mode 100644 index 00000000..ee0fe3f3 --- /dev/null +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/ProjectNexusPublishingExtension.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.github.gradlenexus.publishplugin + +import org.gradle.api.Project +import org.gradle.kotlin.dsl.property + +open class ProjectNexusPublishingExtension(project: Project) { + companion object { + internal const val NAME = "projectNexusPublishing" + } + + @Suppress("UnstableApiUsage") + val enabled = project.objects.property().apply { + set(true) + } +}