Skip to content

Commit

Permalink
directly support java-gradle-plugin projects (#115)
Browse files Browse the repository at this point in the history
* directly support java-gradle-plugin projects

* code style

* Gradle isn't doing weird things after all
  • Loading branch information
gabrielittner authored Feb 14, 2020
1 parent d16a6c3 commit b77782a
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id "java-gradle-plugin"
id "com.vanniktech.maven.publish"
}

repositories {
mavenCentral()
}


gradlePlugin {
plugins {
mavenPublishPlugin {
// the id here should be different from the group id and artifact id
id = 'com.example.test-plugin'
implementationClass = 'com.vanniktech.maven.publish.test.TestPlugin'
}
}
}

apply from: "maven-publish.gradle"
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.test-plugin</groupId>
<artifactId>com.example.test-plugin.gradle.plugin</artifactId>
<version>1.0.0</version>
<url>https://github.com/vanniktech/gradle-maven-publish-plugin/</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>vanniktech</id>
<name>Niklas Baudy</name>
<url>https://github.com/vanniktech/</url>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/vanniktech/gradle-maven-publish-plugin.git</connection>
<developerConnection>scm:git:ssh://git@github.com/vanniktech/gradle-maven-publish-plugin.git</developerConnection>
<url>https://github.com/vanniktech/gradle-maven-publish-plugin/</url>
</scm>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>test-artifact</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-artifact</artifactId>
<version>1.0.0</version>
<name>Gradle Maven Publish Plugin Test Artifact</name>
<description>Testing the Gradle Maven Publish Plugin</description>
<url>https://github.com/vanniktech/gradle-maven-publish-plugin/</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>vanniktech</id>
<name>Niklas Baudy</name>
<url>https://github.com/vanniktech/</url>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/vanniktech/gradle-maven-publish-plugin.git</connection>
<developerConnection>scm:git:ssh://git@github.com/vanniktech/gradle-maven-publish-plugin.git</developerConnection>
<url>https://github.com/vanniktech/gradle-maven-publish-plugin/</url>
</scm>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.vanniktech.maven.publish.test;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

/**
* Just a test class with Javadoc.
*/
public class TestPlugin implements Plugin<Project> {
/**
* Main method which does something.
*
* @param args Command-line arguments passed to the program.
*/
public void apply(Project project) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ class MavenPublishPluginIntegrationTest(
assertPomContentMatches(linuxArtifactId)
}

@Test fun generatesArtifactsAndDocumentationOnGradlePluginProject() {
setupFixture("passing_java_gradle_plugin_project")

val result = executeGradleCommands(uploadArchivesTargetTaskName, "--info", "--stacktrace")

assertThat(result.task(":$uploadArchivesTargetTaskName")?.outcome).isEqualTo(SUCCESS)
repoFolder.walk().sorted().forEach { println(it) }
assertExpectedCommonArtifactsGenerated()
assertPomContentMatches()

if (!useLegacyMode) {
val pluginId = "com.example.test-plugin"
val markerArtifactFolder = repoFolder.resolve("${pluginId.replace(".", "/")}/$pluginId.gradle.plugin/$TEST_VERSION_NAME")
val pomFile = "$pluginId.gradle.plugin-$TEST_VERSION_NAME.pom"
assertArtifactGenerated(markerArtifactFolder, pomFile)
assertPomContentMatches(markerArtifactFolder, pomFile)
}
}

@Test fun generatesArtifactsAndDocumentationOnMinimalPomProject() {
setupFixture("minimal_pom_project")

Expand Down Expand Up @@ -227,10 +246,16 @@ class MavenPublishPluginIntegrationTest(
}

private fun assertPomContentMatches(artifactId: String = TEST_POM_ARTIFACT_ID) {
val pomFileName = "$artifactId-$TEST_VERSION_NAME.pom"
assertPomContentMatches(artifactFolder(artifactId), "$artifactId-$TEST_VERSION_NAME.pom")
}

private fun assertPomContentMatches(
artifactFolder: File,
pomFileName: String
) {
val resolvedPomFile = artifactFolder.resolve(pomFileName)
// in legacyMode for Android the packaging is written, for all other modes it's currently not written
// https://github.com/vanniktech/gradle-maven-publish-plugin/issues/82
val resolvedPomFile = artifactFolder(artifactId).resolve(pomFileName)
val lines = resolvedPomFile.readLines()
if (lines.contains(" <packaging>aar</packaging>")) {
resolvedPomFile.writeText("")
Expand All @@ -241,9 +266,10 @@ class MavenPublishPluginIntegrationTest(
}
}
}
val actualLines = resolvedPomFile.readLines()

val expectedPom = testProjectDir.root.resolve(EXPECTED_DIR).resolve(pomFileName)
assertThat(resolvedPomFile).hasSameContentAs(expectedPom)
val expectedLines = testProjectDir.root.resolve(EXPECTED_DIR).resolve(pomFileName).readLines()
assertThat(actualLines).isEqualTo(expectedLines)
}

private fun assertSourceJarContainsFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ internal abstract class BaseMavenPublishPlugin : Plugin<Project> {
configurer.configureTarget(it)
}

if (project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
configurer.configureKotlinMppProject()
} else if (project.plugins.hasPlugin("com.android.library")) {
configurer.configureAndroidArtifacts()
} else {
configurer.configureJavaArtifacts()
}
configurePublishing(project, configurer)

NexusConfigurer(project)
}
Expand Down Expand Up @@ -92,6 +86,18 @@ internal abstract class BaseMavenPublishPlugin : Plugin<Project> {
}
}

private fun configurePublishing(project: Project, configurer: Configurer) {
if (project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
configurer.configureKotlinMppProject()
} else if (project.plugins.hasPlugin("java-gradle-plugin")) {
configurer.configureGradlePluginProject()
} else if (project.plugins.hasPlugin("com.android.library")) {
configurer.configureAndroidArtifacts()
} else {
configurer.configureJavaArtifacts()
}
}

protected abstract fun configureMavenDeployer(
upload: Upload,
project: Project,
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/vanniktech/maven/publish/Configurer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal interface Configurer {

fun configureKotlinMppProject()

fun configureGradlePluginProject()

fun configureAndroidArtifacts()

fun configureJavaArtifacts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin as GradleMavenPublishPlugin
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.AbstractArchiveTask
import org.gradle.plugin.devel.GradlePluginDevelopmentExtension
import java.net.URI

@Suppress("TooManyFunctions")
internal class MavenPublishConfigurer(
private val project: Project,
private val targets: Iterable<MavenPublishTarget>
Expand All @@ -27,7 +29,8 @@ internal class MavenPublishConfigurer(
init {
project.plugins.apply(GradleMavenPublishPlugin::class.java)

if (!project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
if (!project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform") &&
!project.plugins.hasPlugin("java-gradle-plugin")) {
configurePublications()
}
configureSigning()
Expand All @@ -36,13 +39,17 @@ internal class MavenPublishConfigurer(
private fun configurePublications() {
val publications = project.publishing.publications
publications.create(PUBLICATION_NAME, MavenPublication::class.java) { publication ->
publication.artifactId = publishPom.artifactId
configurePom(publication)
}
}

private fun configurePom(publication: MavenPublication) {
publication.groupId = publishPom.groupId
private fun configurePom(
publication: MavenPublication,
groupId: String = publishPom.groupId,
artifactId: String = publishPom.artifactId
) {
publication.groupId = groupId
publication.artifactId = artifactId
publication.version = publishPom.version

@Suppress("UnstableApiUsage")
Expand Down Expand Up @@ -125,19 +132,38 @@ internal class MavenPublishConfigurer(
private fun publishTaskName(publication: Publication, repository: String) =
"publish${publication.name.capitalize()}PublicationTo${repository.capitalize()}Repository"

override fun configureKotlinMppProject() {
// Source jars are only created for platforms, not the common artifact.
project.publishing.publications.named("kotlinMultiplatform") {
val emptySourcesJar = project.tasks.register("emptySourcesJar", EmptySourcesJar::class.java)
(it as MavenPublication).addTaskOutput(emptySourcesJar)
}
override fun configureGradlePluginProject() {
val sourcesJar = project.tasks.register(SOURCES_TASK, SourcesJar::class.java)
val javadocsJar = project.tasks.register(JAVADOC_TASK, JavadocsJar::class.java)

val javadocsJar = project.tasks.register("javadocsJar", JavadocsJar::class.java)
project.publishing.publications.withType(MavenPublication::class.java).all {
it.artifactId = it.artifactId.replace(project.name, publishPom.artifactId)
if (it.name == "pluginMaven") {
configurePom(it)

it.addTaskOutput(javadocsJar)
it.addTaskOutput(sourcesJar)
}

project.extensions.getByType(GradlePluginDevelopmentExtension::class.java).plugins.forEach { plugin ->
if (it.name == "${plugin.name}PluginMarkerMaven") {
// keep the current group and artifact ids, they are based on the gradle plugin id
configurePom(it, groupId = it.groupId, artifactId = it.artifactId)
}
}
}
}

override fun configureKotlinMppProject() {
val javadocsJar = project.tasks.register(JAVADOC_TASK, JavadocsJar::class.java)

project.publishing.publications.withType(MavenPublication::class.java).all {
configurePom(it, artifactId = it.artifactId.replace(project.name, publishPom.artifactId))
it.addTaskOutput(javadocsJar)

// Source jars are only created for platforms, not the common artifact.
if (it.name == "kotlinMultiplatform") {
val emptySourcesJar = project.tasks.register("emptySourcesJar", EmptySourcesJar::class.java)
it.addTaskOutput(emptySourcesJar)
}
}
}

Expand All @@ -159,10 +185,10 @@ internal class MavenPublishConfigurer(

publication.from(project.components.getByName("java"))

val sourcesJar = project.tasks.register("sourcesJar", SourcesJar::class.java)
val sourcesJar = project.tasks.register(SOURCES_TASK, SourcesJar::class.java)
publication.addTaskOutput(sourcesJar)

val javadocsJar = project.tasks.register("javadocsJar", JavadocsJar::class.java)
val javadocsJar = project.tasks.register(JAVADOC_TASK, JavadocsJar::class.java)
publication.addTaskOutput(javadocsJar)

if (project.plugins.hasPlugin("groovy")) {
Expand All @@ -186,5 +212,7 @@ internal class MavenPublishConfigurer(

companion object {
const val PUBLICATION_NAME = "maven"
const val JAVADOC_TASK = "javadocsJar"
const val SOURCES_TASK = "sourcesJar"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ internal class UploadArchivesConfigurer(
it.configuration = project.configurations.getByName(ARCHIVES_CONFIGURATION)
}

override fun configureGradlePluginProject() {
configureJavaArtifacts()
}

override fun configureKotlinMppProject() {
throw UnsupportedOperationException("Publishing multiplatform projects not supported in legacy mode")
}
Expand Down

0 comments on commit b77782a

Please sign in to comment.