jdaugherty commented on code in PR #15948: URL: https://github.com/apache/grails-core/pull/15948#discussion_r3608689909
########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliArtifactGradlePlugin.groovy: ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.commands + +import groovy.transform.CompileStatic + +import org.gradle.api.GradleException +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration +import org.gradle.api.artifacts.PublishArtifact +import org.gradle.api.component.AdhocComponentWithVariants +import org.gradle.api.component.ConfigurationVariantDetails +import org.gradle.api.component.SoftwareComponentFactory +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer +import org.gradle.api.tasks.bundling.Jar +import org.gradle.api.plugins.JavaPluginExtension + +import javax.inject.Inject + +import org.apache.grails.gradle.publish.AdditionalPublication +import org.apache.grails.gradle.publish.GrailsPublishExtension + +/** + * Configures a Grails plugin project to ship its CLI commands as a companion artifact under its + * own Maven coordinate ({@code <artifactId>-cli}), keeping command code and CLI-only dependencies + * off the runtime classpath of consuming applications. + * + * Applying the plugin: + * <ul> + * <li>creates a {@code cli} source set ({@code src/cli/groovy}) exposed as a feature variant;</li> + * <li>adds {@code org.apache.grails:grails-core-cli} (the command contract) and the plugin's own + * runtime classes to the cli compile classpath, so commands compile with no additional + * configuration and are registered automatically in {@code META-INF/grails-cli.factories};</li> + * <li>keeps the cli variants out of the plugin's default publication and exposes them through a + * dedicated {@code cli} software component;</li> + * <li>stamps the runtime jar with the {@code Grails-Cli-Artifact} manifest attribute, so the + * Grails Gradle plugin adds the companion to a consuming application's {@code grailsCli} + * configuration automatically;</li> + * <li>registers the companion publication with the Grails publish plugin + * ({@code org.apache.grails.gradle.grails-publish}) when it is applied.</li> + * </ul> + * + * Configure through the {@code cliArtifact} extension: + * <pre> + * cliArtifact { + * automaticModuleName = 'com.example.myplugin.cli' + * } + * </pre> + * + * @since 8.0 + */ +@CompileStatic +abstract class GrailsCliArtifactGradlePlugin implements Plugin<Project> { + + public static final String CLI_SOURCE_SET_NAME = 'cli' + public static final String CLI_COMPONENT_NAME = 'cli' + public static final String CLI_PUBLICATION_NAME = 'cli' + + /** The runtime-jar manifest attribute advertising the companion cli coordinate */ + public static final String CLI_ARTIFACT_MANIFEST_ATTRIBUTE = 'Grails-Cli-Artifact' + + @Inject + abstract SoftwareComponentFactory getSoftwareComponentFactory() + + @Override + void apply(Project project) { + if (!project.pluginManager.hasPlugin('java')) { + throw new GradleException("The Grails cli-artifact plugin requires the `java` plugin (or a plugin that applies it, e.g. the Grails plugin plugin) to be applied to project `${project.name}` first.") + } + + CliArtifactExtension extension = project.extensions.create('cliArtifact', CliArtifactExtension) + extension.artifactId.convention(project.provider { "${project.name}-cli" as String }) + extension.defaultDependencies.convention(true) + + SourceSetContainer sourceSets = project.extensions.getByType(SourceSetContainer) + SourceSet cliSourceSet = sourceSets.create(CLI_SOURCE_SET_NAME) + + JavaPluginExtension java = project.extensions.getByType(JavaPluginExtension) + java.registerFeature(CLI_SOURCE_SET_NAME) { spec -> + // no explicit capability: the default (`${group}:${project.name}-cli:${version}`) Review Comment: Fixed. When `cliArtifact { artifactId }` is customized, the cli variants now additionally carry the advertised capability (`$group:$artifactId`) alongside the Gradle default, so an in-build requireCapability against the advertised coordinate resolves. The published-metadata rewrite also recognizes customized companion coordinates now (previously only the `<module>-cli` naming convention), so the capability request is still replaced with the plain companion coordinate in the GMM. Covered end-to-end in CliAutoDiscoverySpec (in-build discovery + resolution) and CliCompanionPublishingSpec (publish + external resolve) with a customized acme-tools companion. ########## grails-core/src/test/groovy/org/apache/grails/core/cli/compiler/CommandFactoriesTransformationSpec.groovy: ########## @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.core.cli.compiler + +import spock.lang.Specification +import spock.lang.Unroll + +/** + * Commands shipped in a companion cli artifact compile from the {@code cli} source set + * ({@code src/cli/groovy}), which is not a standard project-source location — the transformation + * must still register them in {@code META-INF/grails-cli.factories}. + */ +class CommandFactoriesTransformationSpec extends Specification { Review Comment: Also added the indirect-implementor case: a command implementing the contract through a trait (the GrailsApplicationCommand pattern) is registered correctly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
