Skip to content

Commit

Permalink
Fix Gradle plugins when coordinates are modified (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 committed Dec 28, 2022
1 parent 1f8a68b commit a4ce37a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
23 changes: 23 additions & 0 deletions .idea/runConfigurations/deploySonatype.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions deployer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import io.deepmedia.tools.deployer.impl.SonatypeAuth
plugins {
`kotlin-dsl`
`java-gradle-plugin`
id("io.deepmedia.tools.deployer") version "0.9.0-rc01"
id("io.deepmedia.tools.deployer") version "0.9.1-rc02"
}

dependencies {
Expand Down Expand Up @@ -39,7 +39,7 @@ gradlePlugin {
}

group = "io.deepmedia.tools.deployer"
version = "0.9.0"
version = "0.9.1"

deployer {
verbose.set(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ internal fun Project.configurePom(
connection.set(spec.projectInfo.scm.connection)
developerConnection.set(spec.projectInfo.scm.developerConnection)
}
maven.pom.withXml {
component.xml?.invoke(this) { componentToFetch ->
val publishing = this@configurePom.extensions.getByType(PublishingExtension::class.java)
componentToFetch.maybeCreatePublication(publishing.publications, spec)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.deepmedia.tools.deployer.isKmpProject
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Transformer
import org.gradle.api.XmlProvider
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.publish.PublicationContainer
Expand Down Expand Up @@ -35,6 +36,7 @@ open class Component @Inject constructor(objects: ObjectFactory) {
else -> (origin as Origin.MavenPublication).name
}
return when {
publications.findByName(name) != null -> publications[name] as MavenPublication
origin is Origin.SoftwareComponent -> publications.create(name, MavenPublication::class)
origin is Origin.MavenPublication && origin.clone -> publications.create(name, MavenPublication::class)
else -> publications[name] as MavenPublication
Expand Down Expand Up @@ -119,6 +121,8 @@ open class Component @Inject constructor(objects: ObjectFactory) {
internal fun configureWhen(block: Project.(configurationBlock: () -> Unit) -> Unit) {
configureWhenBlock = block
}

internal var xml: ((XmlProvider, (Component) -> MavenPublication) -> Unit)? = null
}

@Suppress("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.gradle.kotlin.dsl.*
import org.gradle.plugin.devel.GradlePluginDevelopmentExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.w3c.dom.Element
import javax.inject.Inject

open class Content @Inject constructor(private val objects: ObjectFactory) : ComponentScope {
Expand Down Expand Up @@ -68,40 +69,66 @@ open class Content @Inject constructor(private val objects: ObjectFactory) : Com
companion object {
internal fun inferred(project: Project): DomainObjectSet<Component> {
val set = project.objects.domainObjectSet(Component::class)
fun component(configure: Component.() -> Unit) {
fun component(configure: Component.() -> Unit): Component {
val comp: Component = project.objects.newInstance()
comp.configure()
comp.inferred = true
set.add(comp)
return comp
}
project.afterEvaluate {
when {
// KMP:
// Kotlin multiplatform projects have one artifact per target, plus one for metadata.
project.isKmpProject -> {
val kotlin = project.kotlinExtension as KotlinMultiplatformExtension
kotlin.targets.all {
component { fromKotlinTarget(this@all, clone = true) }
}
}

// GRADLE PLUGINS:
// When java-gradle-plugin is applied and isAutomatedPublishing is true, X+1 publications
// are created. First is "pluginMaven" with the artifact, and the rest are plugin markers
// called "<PLUGIN_NAME>PluginMarkerMaven". Markers have no jar, just pom file.
// called "<PLUGIN_NAME>PluginMarkerMaven". Markers have no jar, just pom file with single dep.
// We use xml action to rewrite the dependency in the markers, because the main component coordinates
// might be overridden by the user while Gradle refers to the original pluginMaven publication which we clone.
// See MavenPluginPublishPlugin.java::createMavenMarkerPublication()
project.isGradlePluginProject -> run {
val gradlePlugin = project.extensions.getByType<GradlePluginDevelopmentExtension>()
if (!gradlePlugin.isAutomatedPublishing) return@run
component {
val mainComponent = component {
fromMavenPublication("pluginMaven", clone = true)
}
gradlePlugin.plugins.all {
component {
fromGradlePluginDeclaration(this@all, clone = true)
xml = { xml, publicationOf ->
val mainPublication = publicationOf(mainComponent)
val root: Element = xml.asElement()
val oldDependencies = (0 until root.childNodes.length).map { root.childNodes.item(it) }.firstOrNull { it.nodeName == "dependencies" }
if (oldDependencies != null) {
root.removeChild(oldDependencies)
}
val dependencies = root.appendChild(root.ownerDocument.createElement("dependencies"))
val dependency = dependencies.appendChild(root.ownerDocument.createElement("dependency"))
val groupId = dependency.appendChild(root.ownerDocument.createElement("groupId"))
groupId.textContent = mainPublication.groupId
val artifactId = dependency.appendChild(root.ownerDocument.createElement("artifactId"))
artifactId.textContent = mainPublication.artifactId
val version = dependency.appendChild(root.ownerDocument.createElement("version"))
version.textContent = mainPublication.version
}
}
}
}

// TODO: starting from 7.1.0, user has many options. we should read from AGP extension.
// https://android.googlesource.com/platform/tools/base/+/refs/heads/mirror-goog-studio-main/build-system/gradle-api/src/main/java/com/android/build/api/dsl/LibraryPublishing.kt
project.isAndroidLibraryProject -> component { fromSoftwareComponent("release") }

project.isKotlinProject -> component { fromSoftwareComponent("kotlin") }

project.isJavaProject -> component { fromSoftwareComponent("java") }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.model.ObjectFactory
import org.gradle.api.publish.PublicationContainer
import org.gradle.api.publish.maven.MavenArtifactSet
import org.gradle.api.publish.maven.MavenPom
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.kotlin.dsl.newInstance
import kotlin.reflect.KClass

Expand Down

0 comments on commit a4ce37a

Please sign in to comment.