Skip to content

Commit 87e44f0

Browse files
authored
Merge pull request #329 from rahulsom/make-publishing-config-conditional
fix: Make publishing config conditional
2 parents ec2c837 + c799da3 commit 87e44f0

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed

README.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ export ORG_GRADLE_PROJECT_signingKey=???
7070
export ORG_GRADLE_PROJECT_signingPassword=???
7171
----
7272

73+
=== Remote Publishing Setup
74+
75+
Waena does not configure remote Sonatype/JReleaser publishing in included/composite builds by default.
76+
For standalone builds, it configures remote publishing only when a publishing-related task is requested
77+
(`snapshot`, `candidate`, `final`, tasks containing `publish`, `release`, `sonatype`, or `jreleaser`).
78+
This keeps regular `build`/`test` runs and composite builds from eagerly creating remote publishing repositories.
79+
80+
You can override this behavior with a project property:
81+
82+
[source,shell]
83+
----
84+
# Force-enable remote publishing setup
85+
./gradlew -PwaenaConfigureRemotePublishing=true test
86+
87+
# Force-disable remote publishing setup
88+
./gradlew -PwaenaConfigureRemotePublishing=false final
89+
----
90+
7391
=== Publishing snapshots
7492

7593
[source,shell]

waena-plugin/src/main/kotlin/com/github/rahulsom/waena/WaenaPublishedPlugin.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ class WaenaPublishedPlugin : Plugin<Project> {
5757

5858
if (target.rootProject == target) {
5959
target.rootProject.tasks.findByPath("release")?.dependsOn(":publish")
60-
target.rootProject.tasks.getByPath("closeSonatypeStagingRepository").mustRunAfter(":publish")
60+
target.rootProject.tasks.findByPath("closeSonatypeStagingRepository")?.mustRunAfter(":publish")
6161
target.rootProject.tasks.findByPath("jreleaserDeploy")?.mustRunAfter(":publish")
6262
} else {
6363
target.rootProject.tasks.findByPath("release")?.dependsOn(":${target.name}:publish")
64-
target.rootProject.tasks.getByPath("closeSonatypeStagingRepository").mustRunAfter(":${target.name}:publish")
64+
target.rootProject.tasks.findByPath("closeSonatypeStagingRepository")?.mustRunAfter(":${target.name}:publish")
6565
target.rootProject.tasks.findByPath("jreleaserDeploy")?.mustRunAfter(":${target.name}:publish")
6666
}
6767

@@ -146,7 +146,7 @@ class WaenaPublishedPlugin : Plugin<Project> {
146146
val scmInfoPlugin = project.rootProject.plugins.getAt(ScmInfoPlugin::class.java)
147147
val originUrl = origin ?: scmInfoPlugin.findProvider(project).calculateOrigin(project)
148148

149-
val hostedRepoRegex =
149+
@Suppress("RegExpRepeatedSpace", "RegExpUnexpectedAnchor") val hostedRepoRegex =
150150
Regex("""
151151
^ # Start of the string
152152
(?:https?://|git://|git@) # Match the protocol (http, https, git) or git@

waena-plugin/src/main/kotlin/com/github/rahulsom/waena/WaenaRootPlugin.kt

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class WaenaRootPlugin : Plugin<Project> {
2222

2323
companion object {
2424
val CENTRAL = Pair("https://central.sonatype.com/repository/maven-snapshots/", "https://central.sonatype.com/api/v1/publisher")
25+
const val CONFIGURE_REMOTE_PUBLISHING_PROPERTY = "waenaConfigureRemotePublishing"
2526
}
2627

2728
override fun apply(target: Project) {
@@ -37,25 +38,37 @@ class WaenaRootPlugin : Plugin<Project> {
3738
rootProject.plugins.apply(TaskTreePlugin::class.java)
3839
rootProject.plugins.apply(InfoPlugin::class.java)
3940
val waenaExtension = rootProject.extensions.create("waena", WaenaExtension::class.java, rootProject)
41+
val configureRemotePublishing = shouldConfigureRemotePublishing(rootProject)
4042

4143
rootProject.allprojects.forEach { target ->
4244
target.plugins.apply(ContactsPlugin::class.java)
4345
}
4446

45-
configureNexusPublishPlugin(rootProject, waenaExtension)
47+
if (configureRemotePublishing) {
48+
configureNexusPublishPlugin(rootProject, waenaExtension)
4649

47-
rootProject.afterEvaluate {
48-
if (!useNexusPublishPlugin(rootProject)) {
49-
configureJReleaser(rootProject)
50+
rootProject.afterEvaluate {
51+
if (!useNexusPublishPlugin(rootProject)) {
52+
configureJReleaser(rootProject)
53+
}
5054
}
55+
} else {
56+
rootProject.logger.info(
57+
"Skipping remote publishing configuration. " +
58+
"Set -P$CONFIGURE_REMOTE_PUBLISHING_PROPERTY=true to force-enable it."
59+
)
5160
}
5261
}
5362

5463
private fun Project.configureJReleaser(rootProject: Project) {
5564
val isSnapshot = rootProject.version.toString().endsWith("-SNAPSHOT")
56-
rootProject.tasks.getByPath(":initializeSonatypeStagingRepository").enabled = isSnapshot
57-
rootProject.tasks.getByPath(":closeSonatypeStagingRepository").enabled = isSnapshot
58-
rootProject.tasks.getByPath(":releaseSonatypeStagingRepository").enabled = isSnapshot
65+
listOf(
66+
":initializeSonatypeStagingRepository",
67+
":closeSonatypeStagingRepository",
68+
":releaseSonatypeStagingRepository"
69+
).forEach { taskPath ->
70+
rootProject.tasks.findByPath(taskPath)?.enabled = isSnapshot
71+
}
5972
rootProject.allprojects.forEach { project ->
6073
project.afterEvaluate {
6174
project.tasks.findByName("publishNebulaPublicationToSonatypeRepository")?.enabled = isSnapshot
@@ -131,6 +144,36 @@ class WaenaRootPlugin : Plugin<Project> {
131144
return publishMode != WaenaExtension.PublishMode.Central
132145
}
133146

147+
private fun shouldConfigureRemotePublishing(rootProject: Project): Boolean {
148+
rootProject.findProperty(CONFIGURE_REMOTE_PUBLISHING_PROPERTY)?.toString()?.let { configuredValue ->
149+
configuredValue.toBooleanStrictOrNull()?.let { return it }
150+
rootProject.logger.warn(
151+
"Ignoring invalid value '$configuredValue' for $CONFIGURE_REMOTE_PUBLISHING_PROPERTY. " +
152+
"Expected true or false."
153+
)
154+
}
155+
156+
if (rootProject.gradle.parent != null) {
157+
return false
158+
}
159+
160+
val requestedTasks = rootProject.gradle.startParameter.taskNames
161+
if (requestedTasks.isEmpty()) {
162+
return true
163+
}
164+
return requestedTasks
165+
.map { taskPath -> taskPath.substringAfterLast(':') }
166+
.any { taskName ->
167+
taskName.equals("snapshot", ignoreCase = true)
168+
|| taskName.equals("candidate", ignoreCase = true)
169+
|| taskName.equals("final", ignoreCase = true)
170+
|| taskName.contains("publish", ignoreCase = true)
171+
|| taskName.contains("release", ignoreCase = true)
172+
|| taskName.contains("sonatype", ignoreCase = true)
173+
|| taskName.contains("jreleaser", ignoreCase = true)
174+
}
175+
}
176+
134177
fun provideUrls(extension: WaenaExtension) = DefaultProvider({
135178
when (extension.publishMode.get()) {
136179
WaenaExtension.PublishMode.Central -> CENTRAL

waena-plugin/src/test/kotlin/com/github/rahulsom/waena/WaenaPluginFunctionalTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class WaenaPluginFunctionalTest {
154154

155155
private fun showConfig(projectDir: File, gradleVersion: String?): String? {
156156
val showConfigRunner = baseRunner(projectDir, gradleVersion)
157-
showConfigRunner.withArguments("showconfig")
157+
showConfigRunner.withArguments("-P${WaenaRootPlugin.CONFIGURE_REMOTE_PUBLISHING_PROPERTY}=true", "showconfig")
158158
val showConfigResult = showConfigRunner.build().output
159159
return showConfigResult
160160
}

0 commit comments

Comments
 (0)