-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
117 additions
and
81 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
convention-plugin/src/main/kotlin/org/metaborg/convention/MavenPublishConventionPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package org.metaborg.convention | ||
|
||
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.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.* | ||
|
||
private const val METABORG_ARTIFACTS_PUBLICATION_NAME = "MetaborgArtifacts" | ||
private const val GITHUB_PACKAGES_PUBLICATION_NAME = "GitHubPackages" | ||
|
||
/** | ||
* Configures a Gradle project that publishes artifacts to Maven repositories. | ||
*/ | ||
@Suppress("unused") | ||
class MavenPublishConventionPlugin: Plugin<Project> { | ||
override fun apply(project: Project): Unit = with(project) { | ||
// Add the configuration extension | ||
val extension = extensions.create<MavenPublishConventionExtension>("mavenPublishConvention") | ||
extension.setConvention() | ||
|
||
// Apply the Maven Publish plugin | ||
plugins.apply("maven-publish") | ||
|
||
// Set the metadata for (existing) publications | ||
configure<PublishingExtension> { | ||
afterEvaluate { | ||
val metadata = extension.metadata | ||
publications { | ||
withType<MavenPublication> { | ||
pom { | ||
description.set(project.description) | ||
url.set(metadata.httpUrlFormat.with(extension.repoOwner, extension.repoName) { format, owner, name -> | ||
String.format(Locale.ROOT, format, owner, name) | ||
}) | ||
inceptionYear.set(metadata.inceptionYear) | ||
licenses { | ||
license { | ||
name.set(metadata.license.map { it.id }) | ||
url.set(metadata.license.map { it.url }) | ||
distribution.set("repo") | ||
} | ||
} | ||
developers { | ||
metadata.developers.map { | ||
for (dev in it) { | ||
developer { | ||
id.set(dev.id) | ||
name.set(dev.name) | ||
email.set(dev.email) | ||
} | ||
} | ||
} | ||
} | ||
scm { | ||
connection.set(metadata.scmUrlFormat.with(extension.repoOwner, extension.repoName) { format, owner, name -> | ||
String.format(Locale.ROOT, format, owner, name) | ||
}) | ||
developerConnection.set(metadata.scmUrlFormat.with(extension.repoOwner, extension.repoName) { format, owner, name -> | ||
String.format(Locale.ROOT, format, owner, name) | ||
}) | ||
url.set(metadata.scmUrlFormat.with(extension.repoOwner, extension.repoName) { format, owner, name -> | ||
String.format(Locale.ROOT, format, owner, name) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Configure the repositories to publish to | ||
repositories { | ||
|
||
// Configure publishing to Metaborg Artifacts | ||
maven { | ||
val releasesRepoUrl = uri("https://artifacts.metaborg.org/content/repositories/releases/") | ||
val snapshotsRepoUrl = uri("https://artifacts.metaborg.org/content/repositories/snapshots/") | ||
name = METABORG_ARTIFACTS_PUBLICATION_NAME | ||
url = if (project.extra["isReleaseVersion"] as Boolean) releasesRepoUrl else snapshotsRepoUrl | ||
credentials { | ||
username = project.findProperty("publish.repository.metaborg.artifacts.username") as String? ?: System.getenv("METABORG_ARTIFACTS_USERNAME") | ||
password = project.findProperty("publish.repository.metaborg.artifacts.password") as String? ?: System.getenv("METABORG_ARTIFACTS_PASSWORD") | ||
} | ||
} | ||
|
||
// Configure publishing to GitHub Packages | ||
maven { | ||
name = GITHUB_PACKAGES_PUBLICATION_NAME | ||
url = with(extension.repoOwner, extension.repoName) { owner, name -> uri("https://maven.pkg.github.com/$owner/$name") }.get() | ||
credentials { | ||
username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_ACTOR") | ||
password = project.findProperty("gpr.publishKey") as String? ?: System.getenv("GITHUB_TOKEN") | ||
} | ||
} | ||
} | ||
} | ||
|
||
gradle.taskGraph.whenReady { | ||
tasks.withType<PublishToMavenRepository>().configureEach { | ||
// Conditionally enable the tasks that publish to the respective repositories | ||
when { | ||
name.endsWith("PublicationTo${METABORG_ARTIFACTS_PUBLICATION_NAME}Repository") -> { | ||
onlyIf { extension.publishToMetaborgArtifacts.get() } | ||
} | ||
name.endsWith("PublicationTo${GITHUB_PACKAGES_PUBLICATION_NAME}Repository") -> { | ||
onlyIf { extension.publishToGitHubPackages.get() } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
81 changes: 0 additions & 81 deletions
81
convention-plugin/src/main/kotlin/org/metaborg/gradle/MavenPublishConventionPlugin.kt
This file was deleted.
Oops, something went wrong.