Change publishing scheme

This commit is contained in:
Alexander Nozik 2021-03-06 20:50:17 +03:00
parent d673eb84ae
commit 8dcf45b8d5
6 changed files with 261 additions and 247 deletions

View File

@ -9,10 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
### Changed ### Changed
- Publishing repositories are explicit and defined in the top level project
### Deprecated ### Deprecated
- Publishing plugin
### Removed ### Removed
- Bintray publishing
### Fixed ### Fixed

View File

@ -8,7 +8,7 @@ plugins {
} }
group = "ru.mipt.npm" group = "ru.mipt.npm"
version = "0.8.4" version = "0.9.0-dev-1"
description = "Build tools for DataForge and kscience projects" description = "Build tools for DataForge and kscience projects"

View File

@ -8,29 +8,52 @@ import org.gradle.kotlin.dsl.*
import org.jetbrains.changelog.ChangelogPlugin import org.jetbrains.changelog.ChangelogPlugin
import org.jetbrains.dokka.gradle.DokkaPlugin import org.jetbrains.dokka.gradle.DokkaPlugin
import org.jetbrains.dokka.gradle.DokkaTask import org.jetbrains.dokka.gradle.DokkaTask
import ru.mipt.npm.gradle.internal.addGithubPublishing
import ru.mipt.npm.gradle.internal.addSonatypePublishing
import ru.mipt.npm.gradle.internal.addSpacePublishing
import ru.mipt.npm.gradle.internal.setupPublication
@Suppress("unused") @Suppress("unused")
class KSciencePublishingExtension(val project: Project) { class KSciencePublishingExtension(val project: Project) {
var vcs: String? by project.extra private var initializedFlag = false
// github publishing fun setup(vcsUrl: String){
var githubOrg: String? by project.extra project.setupPublication(vcsUrl)
var githubProject: String? by project.extra initializedFlag = true
}
// Space publishing /**
var spaceRepo: String? by project.extra * github publishing
var spaceUser: String? by project.extra */
var spaceToken: String? by project.extra fun github(githubProject: String, githubOrg: String = "mipt-npm") {
//automatically initialize vcs using github
if(!initializedFlag){
setup("https://github.com/$githubOrg/$githubProject")
}
project.addGithubPublishing(githubOrg, githubProject)
}
// Bintray publishing /**
var bintrayOrg: String? by project.extra * Space publishing
var bintrayUser: String? by project.extra */
var bintrayApiKey: String? by project.extra fun space(spaceRepo: String = "https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven") {
var bintrayRepo: String? by project.extra require(initializedFlag){"The publishing is not set up use 'setup' method to do so"}
project.addSpacePublishing(spaceRepo)
}
// Sonatype publising // // Bintray publishing
var sonatypeUser: String? by project.extra // var bintrayOrg: String? by project.extra
var sonatypePassword: String? by project.extra // var bintrayUser: String? by project.extra
// var bintrayApiKey: String? by project.extra
// var bintrayRepo: String? by project.extra
/**
* Sonatype publising
*/
fun sonatype(){
require(initializedFlag){"The publishing is not set up use 'setup' method to do so"}
project.addSonatypePublishing()
}
} }
@ -110,9 +133,9 @@ open class KScienceProjectPlugin : Plugin<Project> {
if (rootReadmeExtension.readmeTemplate.exists()) { if (rootReadmeExtension.readmeTemplate.exists()) {
val modulesString = buildString { val modulesString = buildString {
subprojects.forEach { subproject-> subprojects.forEach { subproject ->
val name = subproject.name val name = subproject.name
val path = subproject.path.replaceFirst(":","").replace(":","/") val path = subproject.path.replaceFirst(":", "").replace(":", "/")
val ext = subproject.extensions.findByType<KScienceReadmeExtension>() val ext = subproject.extensions.findByType<KScienceReadmeExtension>()
appendln("<hr/>") appendln("<hr/>")
appendln("\n* ### [$name]($path)") appendln("\n* ### [$name]($path)")

View File

@ -2,235 +2,16 @@ package ru.mipt.npm.gradle
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.*
import org.gradle.plugins.signing.SigningExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
private fun Project.isSnapshot() = version.toString().contains("dev") || version.toString().endsWith("SNAPSHOT")
@Deprecated("To be replaced by maven-publish")
open class KSciencePublishingPlugin : Plugin<Project> { open class KSciencePublishingPlugin : Plugin<Project> {
override fun apply(project: Project): Unit { override fun apply(project: Project): Unit = project.run {
if (plugins.findPlugin("maven-publish") == null) {
//Add publishing plugin and new publications plugins.apply("maven-publish")
project.run {
if (plugins.findPlugin("maven-publish") == null) {
plugins.apply("maven-publish")
}
configure<PublishingExtension> {
plugins.withId("ru.mipt.npm.gradle.js") {
val kotlin = extensions.findByType<KotlinJsProjectExtension>()!!
val sourcesJar: Jar by project.tasks.creating(Jar::class) {
archiveClassifier.set("sources")
from(kotlin.sourceSets["main"].kotlin)
}
publications {
create("js", MavenPublication::class) {
from(components["kotlin"])
artifact(sourcesJar)
}
}
}
plugins.withId("ru.mipt.npm.gradle.jvm") {
val kotlin = extensions.findByType<KotlinJvmProjectExtension>()!!
val sourcesJar: Jar by project.tasks.creating(Jar::class) {
archiveClassifier.set("sources")
from(kotlin.sourceSets["main"].kotlin)
}
publications {
create("jvm", MavenPublication::class) {
from(components["kotlin"])
artifact(sourcesJar)
}
}
}
}
}
//configure publications after everything is set in the root project
project.rootProject.afterEvaluate {
//root project release task
val release by tasks.getting
project.run {
val githubOrg: String = project.findProperty("githubOrg") as? String ?: "mipt-npm"
val githubProject: String? by project
val vcs = findProperty("vcs") as? String
?: githubProject?.let { "https://github.com/$githubOrg/$it" }
configure<PublishingExtension> {
val dokkaJar: Jar by tasks.creating(Jar::class) {
group = "documentation"
archiveClassifier.set("javadoc")
from(tasks.findByName("dokkaHtml"))
}
// Process each publication we have in this project
publications.withType<MavenPublication>().forEach { publication ->
publication.artifact(dokkaJar)
publication.pom {
name.set(project.name)
description.set(project.description ?: project.name)
vcs?.let { url.set(vcs) }
licenses {
license {
name.set("The Apache Software License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
distribution.set("repo")
}
}
developers {
developer {
id.set("MIPT-NPM")
name.set("MIPT nuclear physics methods laboratory")
organization.set("MIPT")
organizationUrl.set("http://npm.mipt.ru")
}
}
vcs?.let {
scm {
url.set(vcs)
tag.set(project.version.toString())
//developerConnection = "scm:git:[fetch=]/*ВАША ССЫЛКА НА .git файл*/[push=]/*Повторить предыдущую ссылку*/"
}
}
}
}
val githubUser: String? by project
val githubToken: String? by project
if (githubProject != null && githubUser != null && githubToken != null) {
project.logger.info("Adding github publishing to project [${project.name}]")
repositories {
maven {
name = "github"
url = uri("https://maven.pkg.github.com/mipt-npm/$githubProject/")
credentials {
username = githubUser
password = githubToken
}
}
}
}
val spaceRepo: String? by project
val spaceUser: String? by project
val spaceToken: String? by project
if (spaceRepo != null && spaceUser != null && spaceToken != null) {
project.logger.info("Adding mipt-npm Space publishing to project [${project.name}]")
repositories {
maven {
name = "space"
url = uri(spaceRepo!!)
credentials {
username = spaceUser
password = spaceToken
}
}
}
}
val bintrayOrg = project.findProperty("bintrayOrg") as? String ?: "mipt-npm"
val bintrayUser: String? by project
val bintrayApiKey: String? by project
val bintrayPublish: String? by project
val bintrayRepo = if (isSnapshot()) {
"dev"
} else {
findProperty("bintrayRepo") as? String
}
val projectName = project.name
if (bintrayPublish == "true" && bintrayRepo != null && bintrayUser != null && bintrayApiKey != null) {
project.logger.info("Adding bintray publishing to project [$projectName]")
repositories {
maven {
name = "bintray"
url = uri(
"https://api.bintray.com/maven/$bintrayOrg/$bintrayRepo/$projectName/;publish=1;override=1"
)
credentials {
username = bintrayUser
password = bintrayApiKey
}
}
}
}
val sonatypePublish: String? by project
val sonatypeUser: String? by project
val sonatypePassword: String? by project
val keyId: String? by project
val signingKey: String? =
project.findProperty("signingKey") as? String ?: System.getenv("signingKey")
val signingKeyPassphrase: String? by project
if (sonatypePublish == "true" && sonatypeUser != null && sonatypePassword != null && !isSnapshot()) {
val sonatypeRepo: String = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
if (plugins.findPlugin("signing") == null) {
plugins.apply("signing")
}
extensions.configure<SigningExtension>("signing") {
if (!signingKey.isNullOrBlank()) {
//if key is provided, use it
@Suppress("UnstableApiUsage")
useInMemoryPgpKeys(keyId, signingKey, signingKeyPassphrase)
} // else use file signing
sign(publications)
}
repositories {
maven {
name = "sonatype"
url = uri(sonatypeRepo)
credentials {
username = sonatypeUser
password = sonatypePassword
}
}
}
}
}
val publicationPlatform = project.findProperty("publication.platform") as? String
val publicationName = if (publicationPlatform == null) {
"AllPublications"
} else {
publicationPlatform.capitalize() + "Publication"
}
tasks.findByName("publish${publicationName}ToSonatypeRepository")?.let {
release.dependsOn(it)
}
tasks.findByName("publish${publicationName}ToSpaceRepository")?.let {
release.dependsOn(it)
}
}
} }
} }
} }

View File

@ -21,14 +21,11 @@ internal fun LanguageSettingsBuilder.applySettings(): Unit {
internal fun RepositoryHandler.applyRepos(): Unit { internal fun RepositoryHandler.applyRepos(): Unit {
mavenCentral() mavenCentral()
maven("https://repo.kotlin.link")
maven("https://dl.bintray.com/kotlin/kotlin-eap") maven("https://dl.bintray.com/kotlin/kotlin-eap")
maven("https://dl.bintray.com/kotlin/kotlin-dev") maven("https://dl.bintray.com/kotlin/kotlin-dev")
maven("https://kotlin.bintray.com/kotlinx") maven("https://kotlin.bintray.com/kotlinx")
maven("https://kotlin.bintray.com/kotlin-js-wrappers/") maven("https://kotlin.bintray.com/kotlin-js-wrappers/")
maven("https://dl.bintray.com/mipt-npm/kscience")
maven("https://dl.bintray.com/mipt-npm/dev")
maven("https://dl.bintray.com/mipt-npm/dataforge")
maven("https://repo.kotlin.link")
} }
internal fun Copy.fromJsDependencies(configurationName: String) = project.afterEvaluate { internal fun Copy.fromJsDependencies(configurationName: String) = project.afterEvaluate {

View File

@ -0,0 +1,210 @@
package ru.mipt.npm.gradle.internal
import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.*
import org.gradle.plugins.signing.SigningExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
private fun Project.requestPropertyOrNull(propertyName: String): String? = findProperty(propertyName) as? String
?: System.getenv(propertyName)
private fun Project.requestProperty(propertyName: String): String = requestPropertyOrNull(propertyName)
?: error("Property $propertyName not defined")
internal fun Project.setupPublication(vcs: String) = allprojects {
plugins.withId("maven-publish") {
configure<PublishingExtension> {
plugins.withId("ru.mipt.npm.gradle.js") {
val kotlin = extensions.findByType<KotlinJsProjectExtension>()!!
val sourcesJar: Jar by tasks.creating(Jar::class) {
archiveClassifier.set("sources")
from(kotlin.sourceSets["main"].kotlin)
}
publications {
create("js", MavenPublication::class) {
from(components["kotlin"])
artifact(sourcesJar)
}
}
}
plugins.withId("ru.mipt.npm.gradle.jvm") {
val kotlin = extensions.findByType<KotlinJvmProjectExtension>()!!
val sourcesJar: Jar by tasks.creating(Jar::class) {
archiveClassifier.set("sources")
from(kotlin.sourceSets["main"].kotlin)
}
publications {
create("jvm", MavenPublication::class) {
from(components["kotlin"])
artifact(sourcesJar)
}
}
}
val dokkaJar: Jar by tasks.creating(Jar::class) {
group = "documentation"
archiveClassifier.set("javadoc")
from(tasks.findByName("dokkaHtml"))
}
// Process each publication we have in this project
publications.withType<MavenPublication>().forEach { publication ->
publication.artifact(dokkaJar)
publication.pom {
name.set(project.name)
description.set(project.description ?: project.name)
url.set(vcs)
licenses {
license {
name.set("The Apache Software License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
distribution.set("repo")
}
}
developers {
developer {
id.set("MIPT-NPM")
name.set("MIPT nuclear physics methods laboratory")
organization.set("MIPT")
organizationUrl.set("http://npm.mipt.ru")
}
}
scm {
url.set(vcs)
tag.set(project.version.toString())
//developerConnection = "scm:git:[fetch=]/*ВАША ССЫЛКА НА .git файл*/[push=]/*Повторить предыдущую ссылку*/"
}
}
}
}
}
}
internal fun Project.isSnapshot() = version.toString().contains("dev") || version.toString().endsWith("SNAPSHOT")
internal val Project.publicationTarget: String
get() {
val publicationPlatform = project.findProperty("publication.platform") as? String
return if (publicationPlatform == null) {
"AllPublications"
} else {
publicationPlatform.capitalize() + "Publication"
}
}
internal fun Project.addGithubPublishing(
githubOrg: String,
githubProject: String
) {
val githubUser: String = requestProperty("publishing.github.user")
val githubToken: String = requestProperty("publishing.github.token")
allprojects {
plugins.withId("maven-publish") {
configure<PublishingExtension> {
logger.info("Adding github publishing to project [${project.name}]")
repositories {
maven {
name = "github"
url = uri("https://maven.pkg.github.com/$githubOrg/$githubProject/")
credentials {
username = githubUser
password = githubToken
}
}
}
}
val publicationTask = tasks.getByName("publish${publicationTarget}ToGithubRepository")
rootProject.tasks.findByName("release")?.dependsOn(publicationTask)
}
}
}
internal fun Project.addSpacePublishing(spaceRepo: String) {
val spaceUser: String = requestProperty("publishing.space.user")
val spaceToken: String = requestProperty("publishing.space.token")
allprojects {
plugins.withId("maven-publish") {
configure<PublishingExtension> {
project.logger.info("Adding mipt-npm Space publishing to project [${project.name}]")
repositories {
maven {
name = "space"
url = uri(spaceRepo)
credentials {
username = spaceUser
password = spaceToken
}
}
}
}
val publicationTask = tasks.getByName("publish${publicationTarget}ToSpaceRepository")
rootProject.tasks.findByName("release")?.dependsOn(publicationTask)
}
}
}
internal fun Project.addSonatypePublishing() {
val sonatypeUser: String = requestProperty("publishing.sonatype.user")
val sonatypePassword: String = requestProperty("publishing.sonatype.password")
val signingId: String? = requestPropertyOrNull("publishing.signing.id")
allprojects {
plugins.withId("maven-publish") {
configure<PublishingExtension> {
val sonatypeRepo: String = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
if (plugins.findPlugin("signing") == null) {
plugins.apply("signing")
}
extensions.configure<SigningExtension>("signing") {
if (!signingId.isNullOrBlank()) {
val signingKey: String = requestProperty("publishing.signing.key")
val signingPassphrase: String = requestProperty("publishing.signing.passPhrase")
//if key is provided, use it
@Suppress("UnstableApiUsage")
useInMemoryPgpKeys(signingId, signingKey, signingPassphrase)
} // else use file signing
sign(publications)
}
repositories {
maven {
name = "sonatype"
url = uri(sonatypeRepo)
credentials {
username = sonatypeUser
password = sonatypePassword
}
}
}
}
val publicationTask = tasks.getByName("publish${publicationTarget}ToSonatypeRepository")
rootProject.tasks.findByName("release")?.dependsOn(publicationTask)
}
}
}
//internal val Project.bintrayPublish: Boolean
// get() = (findProperty("publishing.bintray.publish") as? String)?.toBoolean() ?: false
//internal val Project.bintrayOrg: String? get() = findProperty("publishing.bintray.org") as? String
//internal val Project.bintrayUser: String? get() = findProperty("publishing.bintray.user") as? String
//internal val Project.bintrayApiKey: String? get() = findProperty("publishing.bintray.apiKey") as? String