From f32a40efad9a413483828b7f4bf7d78ab6369ba4 Mon Sep 17 00:00:00 2001 From: Yan Pujante Date: Fri, 30 Apr 2021 07:59:15 -0700 Subject: [PATCH] fix for dependencies not defined in pom changed how release work by publishing a software component (which contains the proper dependencies) + other artifacts (like javadoc and sources) --- README.md | 10 ++- RELEASE.md | 4 + .../pongasoft/gradle/core/BuildInfo.groovy | 8 ++ .../gradle/plugins/CmdlinePlugin.groovy | 8 +- .../gradle/plugins/ReleasePlugin.groovy | 80 ++++++++++++------- .../gradle/plugins/SigningPlugin.groovy | 4 +- .../plugins/SonatypePublishingPlugin.groovy | 4 +- .../org/pongasoft/gradle/utils/Utils.groovy | 2 +- org.pongasoft.test-2/build.gradle | 3 + project-spec.groovy | 2 +- 10 files changed, 83 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 97f1c42..56c1cca 100644 --- a/README.md +++ b/README.md @@ -124,16 +124,20 @@ Check the `repositories.gradle` file that comes with this project for examples. ---------------------------- This plugin does the following (by default): -- creates a `releaseMaster` configuration which extends `archives` +- creates an empty `releaseMaster` configuration - creates `javadocJar`, `groovydocJar` and `sourcesJar` tasks (depending on the kind of artifacts built) -- creates a `release` task which automatically publishes all the artifacts that are part of the `releaseMaster` configuration using a publication named `release` in a repository named `release` +- creates a `release` task which automatically publishes the `java` software component as well as all the artifacts + that are part of the `releaseMaster` configuration using a publication named `release` in a repository named `release` This plugin is highly configurable: +- you can change the software component (`release.releaseComponent`) - you can change the configurations (`release.releaseConfigurations`, `release.sourcesConfigurations`, `release.javadocConfigurations`, `release.groovydocConfigurations`) - you can change the name of the task, repository or publication (all defaulting to `release`) - you can disable the creation of the task entirely by setting `release.repositoryName` (or `release.publicationName`) to `null` -- you can invoke `release.createConfigurationPublicationTask(...)` to create your own task(s) to publish a particular configuration in a particular combination of publication/repository (useful after disabling the main task generation) +- you can invoke `release.task(...)` to create your own task(s) to publish a particular component and/or + configuration artifacts in a particular combination of publication/repository (useful after disabling + the main task generation) This plugin exports the `releaseType` concept. diff --git a/RELEASE.md b/RELEASE.md index 3277acf..a23766a 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,3 +1,7 @@ +3.0.2 (2021/04/30) +------------------ +* Fix for dependencies not included in pom: instead of publishing artifacts, publish a software component (+ other artifacts like javadoc and sources) + 3.0.1 (2021/04/27) ------------------ * Fix when both javadoc and groovydoc diff --git a/buildSrc/src/main/groovy/org/pongasoft/gradle/core/BuildInfo.groovy b/buildSrc/src/main/groovy/org/pongasoft/gradle/core/BuildInfo.groovy index 7a42bd2..2890ae1 100644 --- a/buildSrc/src/main/groovy/org/pongasoft/gradle/core/BuildInfo.groovy +++ b/buildSrc/src/main/groovy/org/pongasoft/gradle/core/BuildInfo.groovy @@ -47,6 +47,7 @@ public class BuildInfo String buildDurationString def buildTasks def buildProperties + def releasedComponent def releasedArtifacts = Collections.synchronizedCollection([]) def publishedArtifacts = Collections.synchronizedCollection([]) @@ -96,6 +97,10 @@ public class BuildInfo addArtifacts(project, configuration, releasedArtifacts) } + void addReleasedComponent(def component) { + releasedComponent = component + } + private static void addArtifacts(Project project, Configuration configuration, def artifacts) { configuration.allArtifacts.all { PublishArtifact artifact -> @@ -172,6 +177,9 @@ public class BuildInfo if(buildTasks) map.buildTasks = buildTasks + if(releasedComponent) + map.releasedComponent = releasedComponent + if(releasedArtifacts) map.releasedArtifacts = releasedArtifacts diff --git a/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/CmdlinePlugin.groovy b/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/CmdlinePlugin.groovy index 36f060f..db37f5a 100644 --- a/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/CmdlinePlugin.groovy +++ b/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/CmdlinePlugin.groovy @@ -219,14 +219,16 @@ class CmdlinePlugin implements Plugin from root include pattern compression = convention.compression - destinationDir = convention.packageFile.parentFile - archiveName = convention.packageFile.name + destinationDirectory = convention.packageFile.parentFile + archiveFileName = convention.packageFile.name } packageTask.doLast { logger."${convention.cmdlineLogLevel}"("Created package [${convention.packageFile}]") } + project.tasks.findByName(ReleasePlugin.RELEASE_MASTER_TASK_NAME)?.dependsOn(packageTask) + project.task([type: SingleArtifactTask, dependsOn: packageTask], "package-assemble-artifact") { artifactFile = convention.packageFile @@ -235,7 +237,7 @@ class CmdlinePlugin implements Plugin extension: convention.packageExtension, configurations: convention.artifactConfigurations ] - } + } } } diff --git a/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/ReleasePlugin.groovy b/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/ReleasePlugin.groovy index 3aba72a..30c6837 100644 --- a/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/ReleasePlugin.groovy +++ b/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/ReleasePlugin.groovy @@ -34,13 +34,11 @@ import org.pongasoft.gradle.utils.Utils * the 'releaseMaster' configuration or to a new configuration and make 'releaseMaster' extends * from it. * - * By convention ({@link ReleasePluginExtension#releaseConfigurations}) 'releaseMaster' extends - * from 'archives'. - * * @author ypujante@linkedin.com */ class ReleasePlugin implements Plugin { public static final String RELEASE_MASTER_CONFIGURATION = 'releaseMaster' + public static final String RELEASE_MASTER_TASK_NAME = 'releaseMaster' Project project @@ -62,6 +60,9 @@ class ReleasePlugin implements Plugin // creating releaseMaster configuration Configuration releaseMasterConfiguration = findOrAddConfiguration(project, RELEASE_MASTER_CONFIGURATION) + // Create the task so that it is available in other plugins + project.tasks.register(RELEASE_MASTER_TASK_NAME) + /** * Needs to be executed after the build script has been evaluated */ @@ -75,18 +76,20 @@ class ReleasePlugin implements Plugin // 2. Add sources and docs addSourcesAndDocs(extension) - // 3. Create releaseMaster task + // 3. Populate releaseMaster task if(extension.publicationName && extension.repositoryName) { - Task releaseMasterTask = - extension.createConfigurationPublicationTask(releaseMasterConfiguration.name, - "releaseMaster", - extension.publicationName, - extension.repositoryName) + Task releaseMasterTask = extension.task(RELEASE_MASTER_TASK_NAME, + extension.publicationName, + extension.repositoryName, + extension.releaseComponent, + releaseMasterConfiguration.name) if(releaseMasterTask) { // 4. add the artifacts that were just released to the build info releaseMasterTask.doLast { buildInfo.addReleasedArtifacts(project, releaseMasterConfiguration) + if(extension.releaseComponent) + buildInfo.addReleasedComponent(extension.releaseComponent) } // 6. create the "release" convenient task @@ -223,7 +226,8 @@ class ReleasePluginExtension _project = project } - def releaseConfigurations = ['archives'] as Set + def releaseComponent = 'java' + def releaseConfigurations = [] as Set def sourcesConfigurations = [sources: []] def javadocConfigurations = [javadoc: ['docs']] def groovydocConfigurations = [groovydoc: ['docs']] @@ -233,37 +237,53 @@ class ReleasePluginExtension def publicationName = "release" /** - * Creates a task with the provided name that will publish all artifacts defined by the provided configuration - * in the publication defined by the combination `publicationName`/`repositoryName` + * Registers (or reuse if exists) a task with the provided name. The (optional) component is added to the + * publication. The artifacts from the (optional) configuration are also added to the publication. The task + * is a shortcut to invoking the publishXXXPublicationToYYYRepository with a more manageable name + * (provided as argument to this call) * - * @return the task or `null` if the configuration, publication or repository does not exist */ - Task createConfigurationPublicationTask(String configurationName, - String taskName, - String publicationName, - String repositoryName) { - - def configuration = _project.configurations.findByName(configurationName) - if(configuration == null) { - _project.logger.warn("[${_project.name}]: Could not find a configuration named [${configurationName}]") - return null - } + * @return the task or null if the configuration, component, publication or repository does not exist */ + Task task(String taskName, + String publicationName, + String repositoryName, + String componentName = null, + String configurationName = null) { def publication = _project.publishing.publications.findByName(publicationName) - // add all the artifacts from the configuration to the publication if(publication) { - configuration.allArtifacts.each { artifact -> - if(artifact instanceof PublishArtifactImpl) - artifact.addToPublication(publication) - else - publication.artifact(artifact) + // add the main software components + if(componentName) { + def component = _project.components.findByName(componentName) + if(component) { + publication.from(component) + } else { + _project.logger.warn("[${_project.name}]: Could not find a component named [${componentName}]") + return null + } + } + + // add other artifacts + if(configurationName) { + def configuration = _project.configurations.findByName(configurationName) + if (configuration == null) { + _project.logger.warn("[${_project.name}]: Could not find a configuration named [${configurationName}]") + return null + } + + configuration.allArtifacts.each { artifact -> + if (artifact instanceof PublishArtifactImpl) + artifact.addToPublication(publication) + else + publication.artifact(artifact) + } } } def repository = _project.publishing.repositories.findByName(repositoryName) if(repository != null && publication != null) { - Task task = _project.tasks.register(taskName).get() + Task task = _project.tasks.findByName(taskName) ?: _project.tasks.register(taskName).get() task.dependsOn("publish${publication.name.capitalize()}PublicationTo${repository.name.capitalize()}Repository") return task } diff --git a/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/SigningPlugin.groovy b/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/SigningPlugin.groovy index 971581f..d944e00 100644 --- a/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/SigningPlugin.groovy +++ b/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/SigningPlugin.groovy @@ -22,11 +22,11 @@ import org.gradle.api.publish.plugins.PublishingPlugin import org.pongasoft.gradle.utils.Utils /** - * The purpose of this plugin is to apply the `signing` plugin and automatically: + * The purpose of this plugin is to apply the signing plugin and automatically: * * - populate the necessary properties by looking at argument on command line/env/userConfig/spec * - add releaseMaster to the signed artifact (which adds a signReleaseMaster task) if - * `org.pongasoft.release` is used + * org.pongasoft.release is used */ class SigningPlugin implements Plugin { private Project project diff --git a/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/SonatypePublishingPlugin.groovy b/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/SonatypePublishingPlugin.groovy index cc5b752..4de94b2 100644 --- a/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/SonatypePublishingPlugin.groovy +++ b/buildSrc/src/main/groovy/org/pongasoft/gradle/plugins/SonatypePublishingPlugin.groovy @@ -23,11 +23,11 @@ import org.pongasoft.gradle.core.ReleaseType import org.pongasoft.gradle.utils.Utils /** - * This plugin adds the `sonatype` extension to easily configure deployment to maven central. + * This plugin adds the sonatype extension to easily configure deployment to maven central. * * - populate the credentials by looking at argument on command line/env/userConfig/spec * - use snapshots vs non snapshots urls - * - use `legacy` or `s01` repositories + * - use legacy or s01 repositories */ class SonatypePublishingPlugin implements Plugin { /** diff --git a/buildSrc/src/main/groovy/org/pongasoft/gradle/utils/Utils.groovy b/buildSrc/src/main/groovy/org/pongasoft/gradle/utils/Utils.groovy index 1513263..4842580 100644 --- a/buildSrc/src/main/groovy/org/pongasoft/gradle/utils/Utils.groovy +++ b/buildSrc/src/main/groovy/org/pongasoft/gradle/utils/Utils.groovy @@ -103,7 +103,7 @@ class Utils } /** - * @return `true` if the version is a snapshot version defined by `-SNAPSHOT` at the end */ + * @return true if the version is a snapshot version defined by -SNAPSHOT at the end */ static boolean isSnapshotVersion(Project project) { return project.version.endsWith('-SNAPSHOT') } diff --git a/org.pongasoft.test-2/build.gradle b/org.pongasoft.test-2/build.gradle index 92ee641..bae3f0c 100644 --- a/org.pongasoft.test-2/build.gradle +++ b/org.pongasoft.test-2/build.gradle @@ -20,3 +20,6 @@ apply plugin: org.pongasoft.gradle.plugins.ExternalPublishingPlugin apply plugin: org.pongasoft.gradle.plugins.ReleasePlugin apply plugin: org.pongasoft.gradle.plugins.SigningPlugin +release { + releaseComponent = null +} \ No newline at end of file diff --git a/project-spec.groovy b/project-spec.groovy index e0507f5..0b09080 100644 --- a/project-spec.groovy +++ b/project-spec.groovy @@ -17,7 +17,7 @@ spec = [ name: 'gradle-plugins', group: 'org.pongasoft', - version: '3.0.1', + version: '3.0.2', versions: [ jdk: '8'