From 133736f00fee2dae128b452f017e4b1fd0943a4e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 23 Feb 2021 13:22:41 +0300 Subject: [PATCH] Fix plugin loading order for publishing --- gradle/wrapper/gradle-wrapper.properties | 2 +- .../mipt/npm/gradle/KScienceCommonPlugin.kt | 47 +-- .../npm/gradle/KSciencePublishingPlugin.kt | 319 +++++++++--------- 3 files changed, 192 insertions(+), 176 deletions(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2a56324..442d913 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceCommonPlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceCommonPlugin.kt index d355726..8938426 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceCommonPlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceCommonPlugin.kt @@ -20,11 +20,6 @@ open class KScienceCommonPlugin : Plugin { registerKScienceExtension() repositories.applyRepos() - // apply dokka for all projects - if (!plugins.hasPlugin("org.jetbrains.dokka")) { - plugins.apply("org.jetbrains.dokka") - } - //Configuration for K-JVM plugin pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { //logger.info("Applying KScience configuration for JVM project") @@ -43,6 +38,22 @@ open class KScienceCommonPlugin : Plugin { } } } + tasks.withType { + kotlinOptions { + useIR = true + jvmTarget = KScienceVersions.JVM_TARGET.toString() + } + } + + extensions.findByType()?.apply { + targetCompatibility = KScienceVersions.JVM_TARGET + } + + tasks.apply { + withType { + useJUnitPlatform() + } + } } pluginManager.withPlugin("org.jetbrains.kotlin.js") { @@ -123,25 +134,23 @@ open class KScienceCommonPlugin : Plugin { (tasks.findByName("jsProcessResources") as? Copy)?.apply { fromJsDependencies("jsRuntimeClasspath") } - } - } - afterEvaluate { - extensions.findByType()?.apply { - targetCompatibility = KScienceVersions.JVM_TARGET - } + extensions.findByType()?.apply { + targetCompatibility = KScienceVersions.JVM_TARGET + } - tasks.apply { - withType { - kotlinOptions { - useIR = true - jvmTarget = KScienceVersions.JVM_TARGET.toString() + tasks.apply { + withType { + useJUnitPlatform() } } - withType { - useJUnitPlatform() - } } } + + // apply dokka for all projects + if (!plugins.hasPlugin("org.jetbrains.dokka")) { + plugins.apply("org.jetbrains.dokka") + } + } } diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KSciencePublishingPlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KSciencePublishingPlugin.kt index a0abdce..9641a35 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KSciencePublishingPlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KSciencePublishingPlugin.kt @@ -15,194 +15,201 @@ private fun Project.isSnapshot() = version.toString().contains("dev") || version open class KSciencePublishingPlugin : Plugin { - override fun apply(project: Project): Unit = project.run { - if (plugins.findPlugin("maven-publish") == null) { - plugins.apply("maven-publish") - } + override fun apply(project: Project): Unit { - configure { - plugins.withId("ru.mipt.npm.gradle.js") { - val kotlin = extensions.findByType()!! - - 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) - } - } + //Add publishing plugin and new publications + project.run { + if (plugins.findPlugin("maven-publish") == null) { + plugins.apply("maven-publish") } - plugins.withId("ru.mipt.npm.gradle.jvm") { - val kotlin = extensions.findByType()!! - - 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) - } - } - } - } - - project.afterEvaluate { - 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 { - val dokkaJar: Jar by tasks.creating(Jar::class) { - group = "documentation" - archiveClassifier.set("javadoc") - from(tasks.findByName("dokkaHtml")) - } + plugins.withId("ru.mipt.npm.gradle.js") { + val kotlin = extensions.findByType()!! - // Process each publication we have in this project - publications.withType().forEach { publication -> - publication.artifact(dokkaJar) - publication.pom { - name.set(project.name) - description.set(project.description ?: project.name) - vcs?.let { url.set(vcs) } + val sourcesJar: Jar by project.tasks.creating(Jar::class) { + archiveClassifier.set("sources") + from(kotlin.sourceSets["main"].kotlin) + } - 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=]/*Повторить предыдущую ссылку*/" - } + publications { + create("js", MavenPublication::class) { + from(components["kotlin"]) + artifact(sourcesJar) } } } - val githubUser: String? by project - val githubToken: String? by project + plugins.withId("ru.mipt.npm.gradle.jvm") { + val kotlin = extensions.findByType()!! - 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 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) } } } + } + } - val spaceRepo: String? by project - val spaceUser: String? by project - val spaceToken: String? by project + //configure publications after everything is set in the root project + project.rootProject.afterEvaluate { + 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" } - 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 - } - - } + configure { + val dokkaJar: Jar by tasks.creating(Jar::class) { + group = "documentation" + archiveClassifier.set("javadoc") + from(tasks.findByName("dokkaHtml")) } - } - val bintrayOrg = project.findProperty("bintrayOrg") as? String ?: "mipt-npm" - val bintrayUser: String? by project - val bintrayApiKey: String? by project - val bintrayPublish: String? by project + // Process each publication we have in this project + publications.withType().forEach { publication -> + publication.artifact(dokkaJar) + publication.pom { + name.set(project.name) + description.set(project.description ?: project.name) + vcs?.let { url.set(vcs) } - val bintrayRepo = if (isSnapshot()) { - "dev" - } else { - findProperty("bintrayRepo") as? String - } + 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") + } - 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 + } + vcs?.let { + scm { + url.set(vcs) + tag.set(project.version.toString()) + //developerConnection = "scm:git:[fetch=]/*ВАША ССЫЛКА НА .git файл*/[push=]/*Повторить предыдущую ссылку*/" + } } } } - } - val sonatypePublish: String? by project - val sonatypeUser: String? by project - val sonatypePassword: String? by project + val githubUser: String? by project + val githubToken: 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 (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 + } + } + } + } - if (sonatypePublish == "true" && sonatypeUser != null && sonatypePassword != null) { - val sonatypeRepo: String = if (isSnapshot()) { - "https://oss.sonatype.org/content/repositories/snapshots" + 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 { - "https://oss.sonatype.org/service/local/staging/deploy/maven2" + findProperty("bintrayRepo") as? String } - if (plugins.findPlugin("signing") == null) { - plugins.apply("signing") + 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 + } + } + } } - extensions.configure("signing") { - if (!signingKey.isNullOrBlank()) { - //if key is provided, use it - @Suppress("UnstableApiUsage") - useInMemoryPgpKeys(keyId, signingKey, signingKeyPassphrase) - } // else use file signing - sign(publications) - } + val sonatypePublish: String? by project + val sonatypeUser: String? by project + val sonatypePassword: String? by project - repositories { - maven { - name = "sonatype" - url = uri(sonatypeRepo) - credentials { - username = sonatypeUser - password = sonatypePassword + 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) { + val sonatypeRepo: String = if (isSnapshot()) { + "https://oss.sonatype.org/content/repositories/snapshots" + } else { + "https://oss.sonatype.org/service/local/staging/deploy/maven2" + } + + if (plugins.findPlugin("signing") == null) { + plugins.apply("signing") + } + + extensions.configure("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 + } } } }