Skip to content

Commit

Permalink
add specific support to Gradle plugin publish plugin (#543)
Browse files Browse the repository at this point in the history
* add specific support to Gradle plugin publish plugin

* ktlint
  • Loading branch information
gabrielittner committed Mar 23, 2023
1 parent b4a3937 commit 38949d9
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,10 +9,12 @@
sources jar creation for Kotlin/JS projects anymore starting with 1.8.20. The `KotlinJs` constructor
with a `sourcesJar` parameter has been deprecated.
- Fix incompatibility with Gradle 8.1 for `java-test-fixtures` projects
- Fix incompatibility with `com.gradle.plugin-publish` 1.0.0 and 1.1.0
- New minimum supported versions:
- Gradle 7.4
- Android Gradle Plugin 7.3.0
- Kotlin Gradle Plugin 1.7.0
- `com.gradle.plugin-publish` 1.0.0
- Note: Starting with Kotlin 1.8.20-Beta the `common` sources jar for multiplatform projects will only contain
the sources of the common source set instead of containing the sources from all source sets.

Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -20,6 +20,7 @@ The output of the following Gradle plugins is supported to be published with thi
- `java`
- `java-library`
- `java-gradle-plugin`
- `com.gradle.plugin-publish`
- `java-platform`
- `version-catalog`

Expand Down
29 changes: 27 additions & 2 deletions docs/base.md
Expand Up @@ -299,8 +299,8 @@ For projects using the `org.jetbrains.kotlin.multiplatform` plugin.

### Gradle Plugin


For projects using the `java-gradle-plugin` plugin.
For projects using the `java-gradle-plugin` plugin. When also using `com.gradle.plugin-publish` please
use [GradlePublishPlugin](#gradle-publish-plugin)

=== "build.gradle"

Expand Down Expand Up @@ -338,6 +338,31 @@ For projects using the `java-gradle-plugin` plugin.
}
```

### Gradle Publish Plugin

For projects using the `com.gradle.plugin-publish` plugin. This will always publish a sources jar
and a javadoc jar.

=== "build.gradle"

```groovy
import com.vanniktech.maven.publish.GradlePublishPlugin

mavenPublishing {
configure(new GradlePublishPlugin())
}
```

=== "build.gradle.kts"

```kotlin
import com.vanniktech.maven.publish.GradlePublishPlugin

mavenPublishing {
configure(GradlePublishPlugin())
}
```

### Java Platform


Expand Down
Expand Up @@ -125,6 +125,36 @@ class MavenPublishPluginPlatformTest {
)
}

@TestParameterInjectorTest
fun javaGradlePluginWithPluginPublishProject(@TestParameter gradlePluginPublish: GradlePluginPublish) {
val project = javaGradlePluginWithGradlePluginPublish(gradlePluginPublish)
val result = project.run(fixtures, testProjectDir, testOptions)

assertThat(result).outcome().succeeded()
assertThat(result).artifact("jar").exists()
assertThat(result).artifact("jar").isSigned()
assertThat(result).pom().exists()
assertThat(result).pom().isSigned()
assertThat(result).pom().matchesExpectedPom()
assertThat(result).module().exists()
assertThat(result).module().isSigned()
assertThat(result).sourcesJar().exists()
assertThat(result).sourcesJar().isSigned()
assertThat(result).sourcesJar().containsAllSourceFiles()
assertThat(result).javadocJar().exists()
assertThat(result).javadocJar().isSigned()

val pluginId = "com.example.test-plugin"
val pluginMarkerSpec = project.copy(group = pluginId, artifactId = "$pluginId.gradle.plugin")
val pluginMarkerResult = result.copy(projectSpec = pluginMarkerSpec)
assertThat(pluginMarkerResult).pom().exists()
assertThat(pluginMarkerResult).pom().isSigned()
assertThat(pluginMarkerResult).pom().matchesExpectedPom(
"pom",
PomDependency("com.example", "test-artifact", "1.0.0", null),
)
}

@TestParameterInjectorTest
fun javaGradlePluginKotlinProject(
@TestParameter(valuesProvider = KotlinVersionProvider::class) kotlinVersion: KotlinVersion,
Expand Down
Expand Up @@ -171,6 +171,7 @@ private fun writeSettingFile(path: Path) {
mavenLocal()
mavenCentral()
google()
gradlePluginPortal()
}
}
Expand Down
Expand Up @@ -13,6 +13,7 @@ val kotlinMultiplatformPlugin = PluginSpec("org.jetbrains.kotlin.multiplatform")
val kotlinJsPlugin = PluginSpec("org.jetbrains.kotlin.js")
val kotlinAndroidPlugin = PluginSpec("org.jetbrains.kotlin.android")
val androidLibraryPlugin = PluginSpec("com.android.library")
val gradlePluginPublishPlugin = PluginSpec("com.gradle.plugin-publish")

val fixtures = Paths.get("src/integrationTest/fixtures2").toAbsolutePath()

Expand Down Expand Up @@ -88,6 +89,14 @@ fun javaGradlePluginProjectSpec() = ProjectSpec(
basePluginConfig = "configure(new GradlePlugin(new JavadocJar.Empty(), true))",
)

fun javaGradlePluginWithGradlePluginPublish(gradlePluginPublish: GradlePluginPublish): ProjectSpec {
val base = javaGradlePluginProjectSpec()
return base.copy(
plugins = base.plugins + gradlePluginPublishPlugin.copy(version = gradlePluginPublish.version),
basePluginConfig = "configure(new GradlePublishPlugin())",
)
}

fun javaGradlePluginKotlinProjectSpec(version: KotlinVersion): ProjectSpec {
val plainJavaGradlePluginProject = javaGradlePluginProjectSpec()
return plainJavaGradlePluginProject.copy(
Expand Down
Expand Up @@ -80,3 +80,11 @@ enum class GradleVersion(val value: String) {
val GRADLE_7_6 = GRADLE_8_0
}
}

enum class GradlePluginPublish(val version: String) {
// minimum supported
GRADLE_PLUGIN_PUBLISH_1_0("1.0.0"),

// stable
GRADLE_PLUGIN_PUBLISH_1_1("1.1.0"),
}
Expand Up @@ -50,6 +50,8 @@ private fun Project.configurePlatform() {
when {
plugins.hasPlugin("org.jetbrains.kotlin.multiplatform") -> {} // Handled above.
plugins.hasPlugin("com.android.library") -> {} // Handled above.
plugins.hasPlugin("com.gradle.plugin-publish") ->
baseExtension.configure(GradlePublishPlugin())
plugins.hasPlugin("java-gradle-plugin") ->
baseExtension.configure(GradlePlugin(defaultJavaDocOption() ?: javadoc()))
plugins.hasPlugin("org.jetbrains.kotlin.jvm") ->
Expand Down
16 changes: 16 additions & 0 deletions plugin/src/main/kotlin/com/vanniktech/maven/publish/Platform.kt
Expand Up @@ -91,6 +91,22 @@ data class GradlePlugin @JvmOverloads constructor(
}
}

/**
* To be used for `com.gradle.plugin-publish` projects. Uses the default publication that gets created by that plugin.
*/
class GradlePublishPlugin : Platform() {

override val javadocJar: JavadocJar = JavadocJar.Javadoc()
override val sourcesJar: Boolean = true

override fun configure(project: Project) {
// setup is fully handled by com.gradle.plugin-publish already
}

override fun equals(other: Any?): Boolean = other is GradlePublishPlugin
override fun hashCode(): Int = this::class.hashCode()
}

/**
* To be used for `com.android.library` projects. Applying this creates a publication for the component of the given
* `variant`. Depending on the passed parameters for [javadocJar] and [sourcesJar], `-javadoc` and `-sources` jars will
Expand Down

0 comments on commit 38949d9

Please sign in to comment.