diff --git a/convention-plugin/src/main/kotlin/org/metaborg/convention/MavenPublishConventionPlugin.kt b/convention-plugin/src/main/kotlin/org/metaborg/convention/MavenPublishConventionPlugin.kt index 973456e..1097d56 100644 --- a/convention-plugin/src/main/kotlin/org/metaborg/convention/MavenPublishConventionPlugin.kt +++ b/convention-plugin/src/main/kotlin/org/metaborg/convention/MavenPublishConventionPlugin.kt @@ -1,13 +1,15 @@ package org.metaborg.convention +import org.gradle.api.GradleException import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.publish.PublishingExtension import org.gradle.api.publish.maven.MavenPublication +import org.gradle.api.publish.maven.tasks.AbstractPublishToMaven +import org.gradle.api.publish.maven.tasks.PublishToMavenLocal import org.gradle.api.publish.maven.tasks.PublishToMavenRepository import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.create -import org.gradle.kotlin.dsl.extra import org.gradle.kotlin.dsl.withType import java.util.* @@ -100,7 +102,32 @@ class MavenPublishConventionPlugin: Plugin { } } + // Ensure we don't allow publishing if the repository is dirty or unversioned + val assertValidVersion = project.tasks.register("assertValidVersion") { + group = "Verification" + description = "Asserts that there is a valid version for $project." + + doLast { + val versionString = project.version.toString() + if (versionString == Project.DEFAULT_VERSION) { + throw GradleException("Cannot publish, project has no version: ${project.version}") + } else if (versionString.endsWith("+dirty")) { + throw GradleException("Cannot publish, project has a dirty version: ${project.version}") + } + } + } + + tasks.withType().configureEach { + // Make publishing depend on having a valid version + dependsOn(assertValidVersion) + } + gradle.taskGraph.whenReady { + tasks.withType().configureEach { + doLast { + println("Published ${publication.name} to mavenLocal: ${project.group}:${project.name}:${project.version}") + } + } tasks.withType().configureEach { // Conditionally enable the tasks that publish to the respective repositories when { @@ -111,6 +138,10 @@ class MavenPublishConventionPlugin: Plugin { onlyIf { extension.publishToGitHubPackages.get() } } } + + doLast { + println("Published ${publication.name} to ${repository.name}: ${project.group}:${project.name}:${project.version}") + } } } }