Skip to content

Commit

Permalink
Fix issue #9 : Cannot run Project.afterEvaluate(Closure) when the p…
Browse files Browse the repository at this point in the history
…roject is already evaluated
  • Loading branch information
johnsonlee committed Oct 15, 2022
1 parent 0e7eda3 commit 1586fcb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,36 @@ abstract class AbstractLibraryPublishPlugin : Plugin<Project> {
}).takeIf(Collection<Pair<String, String>>::isNotEmpty)
?: devAccount
?: setOf("${localName}@local" to localName)
val configure: Project.() -> Unit = {
configureDependencies()

project.run {
afterEvaluate {
configureDependencies()

publishing {
publications {
createMavenPublications(this) {
pom.withXml {
asNode().apply {
appendNode("name", project.name)
publishing {
publications {
createMavenPublications(this) {
pom.withXml {
asNode().apply {
appendNode("name", project.name)
appendNode("url", url)
appendNode("description", project.description ?: project.name)
appendNode("scm").apply {
appendNode("connection", "scm:git:${url}")
appendNode("developerConnection", "scm:git:${url}")
appendNode("url", url)
appendNode("description", project.description ?: project.name)
appendNode("scm").apply {
appendNode("connection", "scm:git:${url}")
appendNode("developerConnection", "scm:git:${url}")
appendNode("url", url)
}
appendNode("licenses").apply {
license?.let {
appendNode("license").apply {
appendNode("name", license.name)
appendNode("url", license.url)
}
}
appendNode("licenses").apply {
license?.let {
appendNode("license").apply {
appendNode("name", license.name)
appendNode("url", license.url)
}
}
appendNode("developers").apply {
developers.forEach { (email, name) ->
appendNode("developer").apply {
appendNode("id", name)
appendNode("name", name)
appendNode("email", email)
}
}
appendNode("developers").apply {
developers.forEach { (email, name) ->
appendNode("developer").apply {
appendNode("id", name)
appendNode("name", name)
appendNode("email", email)
}
}
}
Expand All @@ -71,6 +68,16 @@ abstract class AbstractLibraryPublishPlugin : Plugin<Project> {
}
}
}

project.run {
if (state.executed) {
configure()
} else {
afterEvaluate {
configure()
}
}
}
}

abstract fun Project.createMavenPublications(
Expand Down
20 changes: 18 additions & 2 deletions src/main/kotlin/io/johnsonlee/gradle/publish/ProjectExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,22 @@ fun Project.configureSigning() {

plugins.apply("signing")

afterEvaluate {
val configure: Project.() -> Unit = {
publishing {
signing {
sign(publications)
}
}
}

if (state.executed) {
configure()
} else {
afterEvaluate {
configure()
}
}

logger.info("[${this}] Configuring `signing` completed")
}

Expand Down Expand Up @@ -158,7 +166,7 @@ fun Project.configureMavenRepository() {
}

fun Project.configurePublishRepository() {
afterEvaluate {
val configure: Project.() -> Unit = {
publishing {
repositories {
if (useSonatype) {
Expand All @@ -173,4 +181,12 @@ fun Project.configurePublishRepository() {
}
}
}

if (state.executed) {
configure()
} else {
afterEvaluate {
configure()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ class SonatypePublishPlugin : Plugin<Project> {

configurePublishRepository()

afterEvaluate {
when {
hasAndroidLibraryPlugin -> plugins.apply(AndroidLibraryPublishPlugin::class.java)
hasJavaGradlePlugin -> plugins.apply(GradlePluginPublishPlugin::class.java)
hasKotlinPlugin -> plugins.apply(KotlinLibraryPublishPlugin::class.java)
hasJavaLibraryPlugin -> plugins.apply(JavaLibraryPublishPlugin::class.java)
}
}
applySubplugin()

// for nexus
configureMavenRepository()
Expand All @@ -45,4 +38,22 @@ class SonatypePublishPlugin : Plugin<Project> {
}
}

private fun Project.applySubplugin() {
val apply: Project.() -> Unit = {
when {
hasAndroidLibraryPlugin -> plugins.apply(AndroidLibraryPublishPlugin::class.java)
hasJavaGradlePlugin -> plugins.apply(GradlePluginPublishPlugin::class.java)
hasKotlinPlugin -> plugins.apply(KotlinLibraryPublishPlugin::class.java)
hasJavaLibraryPlugin -> plugins.apply(JavaLibraryPublishPlugin::class.java)
}
}
if (state.executed) {
apply()
} else {
afterEvaluate {
apply()
}
}
}

}

0 comments on commit 1586fcb

Please sign in to comment.