-
-
Notifications
You must be signed in to change notification settings - Fork 974
Split CLI dependencies from the runtime classpath #15948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 8.0.x
Are you sure you want to change the base?
Changes from all commits
36ad682
6fdfaf8
e1bac4a
237cf03
01ddd85
b4c8828
45632e2
8043852
ea3064a
329cc6a
c6739f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,6 +97,23 @@ dependencies { | |
| } | ||
| } | ||
|
|
||
| // Companion cli artifacts (published by the cli-artifact convention plugin) are additional | ||
| // publications of existing projects, so the subproject enumeration above cannot see them. Each | ||
| // applying project exports its companion coordinate via the `cliArtifactId` extra property, which | ||
| // only exists once that project has been evaluated — so the constraints are computed lazily, in | ||
| // the mutation window Gradle provides right before the configuration is first observed. | ||
| configurations.named('api').configure { apiConfiguration -> | ||
| apiConfiguration.withDependencies { | ||
| for (Project subproject : rootProject.subprojects) { | ||
| def cliArtifactId = subproject.findProperty('cliArtifactId') | ||
| if (cliArtifactId) { | ||
| apiConfiguration.dependencyConstraints.add( | ||
| project.dependencies.constraints.create("${subproject.group}:${cliArtifactId}:${projectVersion}")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related to the |
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| configurations.register('bomDependencies').configure { | ||
| it.canBeResolved = true | ||
| it.transitive = true | ||
|
|
@@ -120,6 +137,11 @@ tasks.register('extractConstraints', ExtractDependenciesTask).configure { Extrac | |
|
|
||
| rootProject.subprojects.each { p -> | ||
| artifactIdMappings[p.name] = p.findProperty('pomArtifactId') ?: p.name | ||
|
|
||
| String cliArtifactId = p.findProperty('cliArtifactId') | ||
| if (cliArtifactId) { | ||
| artifactIdMappings[cliArtifactId] = cliArtifactId | ||
| } | ||
| } | ||
|
|
||
| for (Map.Entry<String, String> dependency : project.ext.gradleBuildProjects.entrySet()) { | ||
|
|
@@ -136,6 +158,11 @@ tasks.register('extractConstraints', ExtractDependenciesTask).configure { Extrac | |
| String artifactId = p.findProperty('pomArtifactId') as String ?: p.name | ||
| String baseVersionName = artifactId.replaceAll('[.]', '-') | ||
| projectCoordinates["${p.group}:${ artifactId}:${p.version}" as String] = baseVersionName | ||
|
|
||
| String cliArtifactId = p.findProperty('cliArtifactId') | ||
| if (cliArtifactId) { | ||
| projectCoordinates["${p.group}:${cliArtifactId}:${p.version}" as String] = cliArtifactId | ||
| } | ||
| } | ||
|
|
||
| for (Map.Entry<String, String> dependency : project.ext.gradleBuildProjects.entrySet()) { | ||
|
|
@@ -191,6 +218,12 @@ ext { | |
| for (String gradleArtifactId : project.ext.gradleBuildProjects) { | ||
| propertyNameCalculator.addProject('org.apache.grails.gradle', gradleArtifactId, project.version as String, gradleArtifactId) | ||
| } | ||
| for (Project p : rootProject.subprojects) { | ||
| String cliArtifactId = p.findProperty('cliArtifactId') | ||
| if (cliArtifactId) { | ||
| propertyNameCalculator.addProject(p.group as String, cliArtifactId, p.version as String, cliArtifactId) | ||
| } | ||
| } | ||
|
|
||
| Map<String, String> pomProperties = [:] | ||
| deps.dependency.each { dep -> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * 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 java.util.regex.Pattern | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import org.codehaus.groovy.ast.ASTNode | ||
| import org.codehaus.groovy.ast.ClassHelper | ||
| import org.codehaus.groovy.ast.ClassNode | ||
| import org.codehaus.groovy.ast.ModuleNode | ||
| import org.codehaus.groovy.control.CompilePhase | ||
| import org.codehaus.groovy.control.SourceUnit | ||
| import org.codehaus.groovy.transform.ASTTransformation | ||
| import org.codehaus.groovy.transform.GroovyASTTransformation | ||
| import org.codehaus.groovy.transform.TransformWithPriority | ||
|
|
||
| import org.apache.grails.common.compiler.GroovyTransformOrder | ||
| import org.grails.compiler.injection.FactoriesFileWriter | ||
| import org.grails.compiler.injection.GlobalGrailsClassInjectorTransformation | ||
| import org.grails.compiler.injection.GrailsASTUtils | ||
| import org.grails.io.support.GrailsResourceUtils | ||
| import org.grails.io.support.UrlResource | ||
|
|
||
| /** | ||
| * A global transformation that registers compiled | ||
| * {@link org.apache.grails.core.cli.ApplicationCommand} implementations in | ||
| * {@code META-INF/grails-cli.factories}, keyed by the command contract's class name. | ||
| * | ||
| * The transform ships in the {@code grails-core-cli} artifact, so it is active exactly when a | ||
| * command can compile — a class implementing the contract only resolves when the cli artifact is | ||
| * on the compile classpath. Command registrations never touch {@code META-INF/grails.factories}. | ||
| * | ||
| * @since 8.0 | ||
| */ | ||
| @GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION) | ||
| @CompileStatic | ||
| class CommandFactoriesTransformation implements ASTTransformation, TransformWithPriority { | ||
|
|
||
| public static final ClassNode APPLICATION_COMMAND_CLASS = ClassHelper.make('org.apache.grails.core.cli.ApplicationCommand') | ||
|
|
||
| /** The command registration file written into the compilation target directory */ | ||
| public static final String CLI_FACTORIES_LOCATION = 'META-INF/grails-cli.factories' | ||
|
|
||
| /** Hand-authored registrations merged into the generated file */ | ||
| protected static final List<String> SOURCE_CLI_FACTORIES_LOCATIONS = [ | ||
| 'src/main/resources/META-INF/grails-cli.factories', | ||
| 'src/cli/resources/META-INF/grails-cli.factories', | ||
| ].asImmutable() | ||
|
|
||
| /** | ||
| * Commands shipped in a companion cli artifact live in the {@code cli} source set | ||
| * ({@code src/cli/groovy} or {@code src/cli/java}), which is not one of the standard | ||
| * project-source locations recognised by {@link GrailsResourceUtils#isProjectSource}. | ||
| */ | ||
| protected static final Pattern CLI_SOURCE_PATTERN = | ||
| Pattern.compile('.+?[\\\\/]src[\\\\/]cli[\\\\/](groovy|java)[\\\\/].+?\\.(groovy|java)$') | ||
|
|
||
| @Override | ||
| int priority() { | ||
| return GroovyTransformOrder.COMMAND_FACTORIES_ORDER | ||
| } | ||
|
|
||
| @Override | ||
| void visit(ASTNode[] nodes, SourceUnit source) { | ||
| ModuleNode ast = source.getAST() | ||
| List<ClassNode> classes = new ArrayList<>(ast.getClasses()) | ||
|
|
||
| URL url = GrailsASTUtils.getSourceUrl(source) | ||
|
|
||
| if (url == null) { | ||
| return | ||
| } | ||
| if (!GrailsResourceUtils.isProjectSource(new UrlResource(url)) && !isCliSource(url)) { | ||
| return | ||
| } | ||
|
|
||
| File compilationTargetDirectory = GlobalGrailsClassInjectorTransformation.resolveCompilationTargetDirectory(source) | ||
|
|
||
| for (ClassNode classNode : classes) { | ||
| FactoriesFileWriter.updateFactoriesWithType(classNode, APPLICATION_COMMAND_CLASS, | ||
| compilationTargetDirectory, CLI_FACTORIES_LOCATION, SOURCE_CLI_FACTORIES_LOCATIONS) | ||
| } | ||
| } | ||
|
|
||
| protected static boolean isCliSource(URL url) { | ||
| CLI_SOURCE_PATTERN.matcher(url.file).matches() | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking for release: this
1.0.0-SNAPSHOTflows intogradleBomDependenciesand from there into theconstraints { api ... }block ofgrails-bom/base, i.e. into the published<dependencyManagement>oforg.apache.grails:grails-bomthat every application consumes. A staged 8.0.x release would ship a BOM recommending a SNAPSHOT oforg.apache.grails.gradle:grails-publish.I understand this is pending apache/grails-gradle-publish#34 shipping — but the PR should be explicitly marked blocked on that release, with the version reverted to a GA coordinate before merge (or at latest before the first 8.0.x release candidate).