Skip to content

Contribute groovy source sets with extension #17149

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

Merged
merged 32 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e0916ad
Clarify wording for injection of services
May 14, 2021
fc7a204
Merge pull request #17190
bot-gradle May 17, 2021
b92924e
Declare groovy sources as a convention and as an extension on contain…
donat May 10, 2021
6119077
Let DefaultGroovySourceSet actually extend DefaultSourceSet
donat May 10, 2021
b8ab5e9
Fix test
donat May 10, 2021
82f7efa
Access groovy sources via extension in CodeNarc plugin
donat May 10, 2021
27f660f
Access groovy sources via extension in PrecompiledGroovyPluginsPlugin
donat May 10, 2021
dc5c76d
Adjust documentation
donat May 11, 2021
85a39b1
Ignore test failing on CI for now
donat May 12, 2021
07a8a5a
Incorporate review
donat May 13, 2021
0e97406
Use marker interface for groovy source sets
donat May 14, 2021
abffe87
Simplify groovy source set access in CodeNarc
donat May 14, 2021
6f39111
Restore sample
donat May 14, 2021
353f56d
Re-enable swift test
donat May 14, 2021
531002a
Simplify groovy source set access in the precompiled script plugin
donat May 14, 2021
6106e50
Simplify groovy source set access in the groovy pugin
donat May 14, 2021
13a4b39
Restore unnecessary change
donat May 14, 2021
fcdd734
Link GroovySourceDirectorySet in groovy plugin documentation
donat May 14, 2021
a553215
:Use GroovySourceDirectorySet in tutorial sample
donat May 14, 2021
54828a0
Deprecate GroovySourceSet
donat May 14, 2021
2f08856
Fix test
donat May 14, 2021
bae88a2
Fix precompiled script plugin
donat May 14, 2021
cc09686
Fix merge error
donat May 14, 2021
bea45ec
Fix tests
donat May 14, 2021
6a89ff3
Adjust test
donat May 14, 2021
3338f1d
Fix test
donat May 14, 2021
7b2ea4f
Restore test
donat May 17, 2021
22d4269
Simplify snippet
donat May 17, 2021
267785e
Adjust GroovySourceSet javadoc
donat May 17, 2021
a213685
Add documentation
donat May 17, 2021
113d84e
Remove unused services from BaseGroovyPlugin's constructor
donat May 17, 2021
fd5d721
Fix merge issue
donat May 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.ConventionMapping;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.plugins.GroovyBasePlugin;
import org.gradle.api.plugins.quality.internal.AbstractCodeQualityPlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.GroovySourceDirectorySet;
import org.gradle.api.tasks.SourceSet;
import org.gradle.internal.metaobject.DynamicObject;

import java.io.File;

Expand Down Expand Up @@ -114,7 +114,7 @@ private void configureReportsConventionMapping(CodeNarc task, final String baseN
@Override
protected void configureForSourceSet(final SourceSet sourceSet, CodeNarc task) {
task.setDescription("Run CodeNarc analysis for " + sourceSet.getName() + " classes");
DynamicObject dynamicObject = new DslObject(sourceSet).getAsDynamicObject();
task.setSource(dynamicObject.getProperty("allGroovy"));
SourceDirectorySet groovySourceSet = sourceSet.getExtensions().getByType(GroovySourceDirectorySet.class);
task.setSource(groovySourceSet.matching(filter -> filter.include("**/*.groovy")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,35 @@ public class DefaultSourceDirectorySet extends CompositeFileTree implements Sour
private TaskProvider compileTaskProvider;

public DefaultSourceDirectorySet(String name, String displayName, Factory patternSetFactory, FileCollectionFactory fileCollectionFactory, DirectoryFileTreeFactory directoryFileTreeFactory, ObjectFactory objectFactory) {
this(name, displayName, patternSetFactory.create(), patternSetFactory.create(), fileCollectionFactory, directoryFileTreeFactory, objectFactory.directoryProperty(), objectFactory.directoryProperty());
}

DefaultSourceDirectorySet(String name, String displayName, PatternSet patterns, PatternSet filters, FileCollectionFactory fileCollectionFactory, DirectoryFileTreeFactory directoryFileTreeFactory, DirectoryProperty destinationDirectory, DirectoryProperty classesDirectory) {
this.name = name;
this.displayName = displayName;
this.fileCollectionFactory = fileCollectionFactory;
this.directoryFileTreeFactory = directoryFileTreeFactory;
this.patterns = patternSetFactory.create();
this.filter = patternSetFactory.create();
this.patterns = patterns;
this.filter = filters;
this.dirs = new FileCollectionAdapter(new SourceDirectories());
this.destinationDirectory = destinationDirectory;
this.classesDirectory = classesDirectory;
}

public DefaultSourceDirectorySet(SourceDirectorySet sourceSet) {
if (!(sourceSet instanceof DefaultSourceDirectorySet)) {
throw new RuntimeException("Invalid source set type:" + source.getClass());
}
DefaultSourceDirectorySet defaultSourceSet = (DefaultSourceDirectorySet) sourceSet;
this.name = defaultSourceSet.name;
this.displayName = defaultSourceSet.displayName;
this.fileCollectionFactory = defaultSourceSet.fileCollectionFactory;
this.directoryFileTreeFactory = defaultSourceSet.directoryFileTreeFactory;
this.patterns = defaultSourceSet.patterns;
this.filter = defaultSourceSet.filter;
this.dirs = new FileCollectionAdapter(new SourceDirectories());
this.destinationDirectory = objectFactory.directoryProperty();
this.classesDirectory = objectFactory.directoryProperty();
this.destinationDirectory = defaultSourceSet.destinationDirectory;
this.classesDirectory = defaultSourceSet.classesDirectory;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Properties
Name
Default with groovy plugin
Methods
Name
1 change: 1 addition & 0 deletions subprojects/docs/src/docs/dsl/plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Expand Down
28 changes: 28 additions & 0 deletions subprojects/docs/src/docs/release/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ class JavaAgentCommandLineArgumentProvider implements CommandLineArgumentProvide
}
```

### Easier groovy source set configuration in the Kotlin DSL

When using the Kotlin DSL a special construct was required for configuring `groovy` sources:

```kotlin
sourceSets {
main {
withConvention(GroovySourceSet::class) {
groovy {
setSrcDirs(listOf("src/groovy"))
}
}
}
}
```

Gradle 7.1 defines the `groovy` source set as an extension. This means that the Kotlin-DSL has auto-generated accessors and `withConvention` block can be omitted:

```kotlin
sourceSets {
main {
groovy {
setSrcDirs(listOf("src/groovy"))
}
}
}
```

## Promoted features
Promoted features are features that were incubating in previous versions of Gradle but are now supported and subject to backwards compatibility.
See the User Manual section on the “[Feature Lifecycle](userguide/feature_lifecycle.html)” for more information.
Expand Down
4 changes: 2 additions & 2 deletions subprojects/docs/src/docs/userguide/jvm/groovy_plugin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ The Groovy plugin does not add any convention properties to the project.
[[sec:groovy_source_set_properties]]
== Source set properties

The Groovy plugin adds the following convention properties to each source set in the project. You can use these properties in your build script as though they were properties of the source set object.
The Groovy plugin adds the following extensions to each source set in the project. You can use these properties in your build script as though they were properties of the source set object.

=== Groovy Plugin — source set properties

`groovy` — link:{groovyDslPath}/org.gradle.api.file.SourceDirectorySet.html[SourceDirectorySet] (read-only)::
`groovy` — link:{groovyDslPath}/org.gradle.api.tasks.GroovySourceDirectorySet.html[GroovySourceDirectorySet] (read-only)::
_Default value_: Not null
+
The Groovy source files of this source set. Contains all `.groovy` and `.java` files found in the Groovy source directories, and excludes all other types of files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,33 @@ The last set of classes have no external or internal usages and therefore were d
- `DeferredUtil`
- `ChangeListener`

==== The return type of the `groovy { }` block has changed
The `groovy` source set is now defined via extension. This extension is backed by the link:{groovyDslPath}/org.gradle.api.tasks.GroovySourceDirectorySet.html[GroovySourceDirectorySet] interface.
The 'idiomatic' DSL declaration is backward compatible:

```groovy
sourceSets {
main {
groovy {
// ...
}
}
}
```

However, the return type of the groovy block has changed from link:{groovyDslPath}/org.gradle.api.tasks.GroovySourceSet.html[GroovySourceSet] to
link:{groovyDslPath}/org.gradle.api.tasks.GroovySourceDirectorySet.html[GroovySourceDirectorySet]. This means that the following snippet no longer works in Gradle 7.1:

```groovy
sourceSets {
main {
GroovySourceSet sourceSet = groovy {
// ...
}
}
}
```

[[java_exec_properties]]
==== Properties deprecated in JavaExec task

Expand Down Expand Up @@ -302,3 +329,12 @@ link:{javadocPath}/org/gradle/api/plugins/WarPluginConvention.html[WarPluginConv
=== Deprecated `ear` plugin conventions

link:{javadocPath}/org/gradle/plugins/ear/EarPluginConvention.html[EarPluginConvention] is now deprecated and scheduled for removal in Gradle 8.0. Clients should configure the `ear` task directly. Also, link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#withType-java.lang.Class-[tasks.withType(War.class).configureEach(...)] can be used to configure each task of type `Ear`.

[[groovy_source_set_deprecation]]
=== Deprecated `GroovySourceSet` interface
link:{javadocPath}/org/gradle/api/tasks/GroovySourceSet.html[GroovySourceSet] is now deprecated and scheduled for removal in Gradle 8.0. Clients should configure `groovy` sources configuration via the
the link:{javadocPath}/org/gradle/api/tasks/GroovySourceDirectorySet.html[new extension] defined on the source set object:
```java
GroovySourceDirectorySet groovySources = sourceSet.getExtensions().getByType(GroovySourceDirectorySet.class);
groovySources.setSrcDirs(Arrays.asList("sources/groovy"));
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@ dependencies {
// tag::custom-source-locations[]
sourceSets {
main {
withConvention(GroovySourceSet::class) {
groovy {
setSrcDirs(listOf("src/groovy"))
}
groovy {
setSrcDirs(listOf("src/groovy"))
}
}

test {
withConvention(GroovySourceSet::class) {
groovy {
setSrcDirs(listOf("test/groovy"))
}
groovy {
setSrcDirs(listOf("test/groovy"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ tasks.named("compileGroovy") {
tasks.named("compileJava") {
// Java also depends on the result of Groovy compilation
// (which automatically makes it depend of compileGroovy)
classpath += files(sourceSets.main.get().withConvention(GroovySourceSet::class) { groovy }.classesDirectory)
classpath += files(sourceSets.main.get().groovy.classesDirectory)
}
// end::compile-task-classpath[]
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import org.gradle.api.internal.tasks.compile.GroovyCompilerFactory;
import org.gradle.api.internal.tasks.compile.GroovyJavaJointCompileSpec;
import org.gradle.api.internal.tasks.compile.HasCompileOptions;
import org.gradle.api.internal.tasks.compile.MinimalJavaCompilerDaemonForkOptions;
import org.gradle.api.internal.tasks.compile.MinimalGroovyCompileOptions;
import org.gradle.api.internal.tasks.compile.MinimalJavaCompilerDaemonForkOptions;
import org.gradle.api.internal.tasks.compile.incremental.IncrementalCompilerFactory;
import org.gradle.api.internal.tasks.compile.incremental.recomp.GroovyRecompilationSpecProvider;
import org.gradle.api.internal.tasks.compile.incremental.recomp.RecompilationSpecProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileTree;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.plugins.GroovyBasePlugin;
import org.gradle.api.tasks.GroovySourceSet;
import org.gradle.api.tasks.GroovySourceDirectorySet;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;
Expand Down Expand Up @@ -81,7 +80,7 @@ private void exposeScriptsAsPlugins(Project project) {
task.getPrecompiledGroovyScriptsOutputDirectory().convention(buildDir.dir("groovy-dsl-plugins/output/plugin-classes"));

SourceDirectorySet javaSource = pluginSourceSet.getJava();
SourceDirectorySet groovySource = new DslObject(pluginSourceSet).getConvention().getPlugin(GroovySourceSet.class).getGroovy();
SourceDirectorySet groovySource = pluginSourceSet.getExtensions().getByType(GroovySourceDirectorySet.class);
task.getClasspath().from(pluginSourceSet.getCompileClasspath(), javaSource.getClassesDirectory(), groovySource.getClassesDirectory());
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed 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
*
* http://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.gradle.api.internal.tasks;

import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.file.DefaultSourceDirectorySet;
import org.gradle.api.tasks.GroovySourceDirectorySet;

public class DefaultGroovySourceDirectorySet extends DefaultSourceDirectorySet implements GroovySourceDirectorySet {

public DefaultGroovySourceDirectorySet(SourceDirectorySet sourceDirectorySet) {
super(sourceDirectorySet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,34 @@
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.reflect.HasPublicType;
import org.gradle.api.reflect.TypeOf;
import org.gradle.api.tasks.GroovySourceDirectorySet;
import org.gradle.api.tasks.GroovySourceSet;

import javax.annotation.Nullable;

import static org.gradle.api.reflect.TypeOf.typeOf;
import static org.gradle.util.internal.ConfigureUtil.configure;

@Deprecated
public class DefaultGroovySourceSet implements GroovySourceSet, HasPublicType {
private final SourceDirectorySet groovy;
private final GroovySourceDirectorySet groovy;
private final SourceDirectorySet allGroovy;

public DefaultGroovySourceSet(String name, String displayName, ObjectFactory objectFactory) {
groovy = objectFactory.sourceDirectorySet(name, displayName + " Groovy source");
groovy.getFilter().include("**/*.java", "**/*.groovy");
this.groovy = createGroovySourceDirectorySet(name, displayName, objectFactory);
allGroovy = objectFactory.sourceDirectorySet("all" + name, displayName + " Groovy source");
allGroovy.source(groovy);
allGroovy.getFilter().include("**/*.groovy");
}

private static GroovySourceDirectorySet createGroovySourceDirectorySet(String name, String displayName, ObjectFactory objectFactory) {
GroovySourceDirectorySet groovySourceDirectorySet = new DefaultGroovySourceDirectorySet(objectFactory.sourceDirectorySet(name, displayName + " Groovy source"));
groovySourceDirectorySet.getFilter().include("**/*.java", "**/*.groovy");
return groovySourceDirectorySet;
}

@Override
public SourceDirectorySet getGroovy() {
public GroovySourceDirectorySet getGroovy() {
return groovy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.classpath.ModuleRegistry;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.internal.tasks.DefaultGroovySourceSet;
import org.gradle.api.internal.tasks.DefaultSourceSet;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.internal.JvmPluginsHelper;
Expand All @@ -33,6 +32,7 @@
import org.gradle.api.provider.Provider;
import org.gradle.api.reporting.ReportingExtension;
import org.gradle.api.tasks.GroovyRuntime;
import org.gradle.api.tasks.GroovySourceDirectorySet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.GroovyCompile;
import org.gradle.api.tasks.javadoc.Groovydoc;
Expand Down Expand Up @@ -87,10 +87,12 @@ private void configureCompileDefaults() {
project.getTasks().withType(GroovyCompile.class).configureEach(compile -> compile.getConventionMapping().map("groovyClasspath", () -> groovyRuntime.inferGroovyClasspath(compile.getClasspath())));
}

@SuppressWarnings("deprecation")
private void configureSourceSetDefaults() {
project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets().all(sourceSet -> {
final DefaultGroovySourceSet groovySourceSet = new DefaultGroovySourceSet("groovy", ((DefaultSourceSet) sourceSet).getDisplayName(), objectFactory);
final org.gradle.api.internal.tasks.DefaultGroovySourceSet groovySourceSet = new org.gradle.api.internal.tasks.DefaultGroovySourceSet("groovy", ((DefaultSourceSet) sourceSet).getDisplayName(), objectFactory);
new DslObject(sourceSet).getConvention().getPlugins().put("groovy", groovySourceSet);
sourceSet.getExtensions().add(GroovySourceDirectorySet.class, "groovy", groovySourceSet.getGroovy());

final SourceDirectorySet groovySource = groovySourceSet.getGroovy();
groovySource.srcDir("src/" + sourceSet.getName() + "/groovy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.tasks.GroovySourceSet;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.tasks.GroovySourceDirectorySet;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.javadoc.Groovydoc;

Expand Down Expand Up @@ -47,8 +47,8 @@ private void configureGroovydoc(final Project project) {
SourceSet sourceSet = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
groovyDoc.setClasspath(sourceSet.getOutput().plus(sourceSet.getCompileClasspath()));

GroovySourceSet groovySourceSet = new DslObject(sourceSet).getConvention().getPlugin(GroovySourceSet.class);
groovyDoc.setSource(groovySourceSet.getGroovy());
SourceDirectorySet groovySourceSet = sourceSet.getExtensions().getByType(GroovySourceDirectorySet.class);
groovyDoc.setSource(groovySourceSet);
});
}
}
Loading