Skip to content

Commit

Permalink
Upgrade to Kotlin plugin 1.9.20 (#864)
Browse files Browse the repository at this point in the history
* Upgrade to Kotlin plugin 1.9.20

* Workaround Kotlin 1.9.20 bug

Since Kotlin plugin 1.9.20, kapt tasks are broken if dependencies are
added to the KAPT configurations in an `afterEvaluate`. This is precisely
what we were doing. Since using `afterEvaluate` is often a code smell
(but it wasn't in our case), the proposed solution is to deprecate the
use of `additionalSourceSets`, and have an eager configuration of
sourceSets instead, as soon as the `sourceSets(...)` method is called.

It's a small breaking change, in the sense that if the user had
configured the `additionalSourceSets` and later in time decides
to remove entries from that list, then this would no longer work.

* Make Sonar happier
  • Loading branch information
melix authored Nov 13, 2023
1 parent 7b555f3 commit c85aea0
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Foo {}
processing {
incremental(true)
}
}
}
${getRepositoriesBlock('kotlin')}
Expand Down Expand Up @@ -114,12 +114,12 @@ class Foo {}
id("io.micronaut.$plugin")
}
sourceSets {
val custom by creating {
compileClasspath += sourceSets["main"].output
runtimeClasspath += sourceSets["main"].output
}
}
val custom by sourceSets.creating
dependencies {
"customImplementation"(project)
}
micronaut {
version("$micronautVersion")
processing {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.micronaut.gradle.graalvm;

import io.micronaut.gradle.AnnotationProcessing;
import io.micronaut.gradle.MicronautComponentPlugin;
import io.micronaut.gradle.MicronautExtension;
import io.micronaut.gradle.MicronautRuntime;
import io.micronaut.gradle.PluginsHelper;
import io.micronaut.gradle.SourceSetConfigurerRegistry;
import io.micronaut.gradle.internal.AutomaticDependency;
import org.graalvm.buildtools.gradle.NativeImagePlugin;
import org.graalvm.buildtools.gradle.dsl.GraalVMExtension;
Expand All @@ -14,7 +16,6 @@
import org.gradle.api.Project;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskContainer;
Expand Down Expand Up @@ -123,11 +124,22 @@ private void workaroundForResourcesDirectoryMissing(Project project) {
}

private static void configureAnnotationProcessing(Project project, MicronautExtension extension) {
var registry = project.getExtensions().getByType(SourceSetConfigurerRegistry.class);
var knownSourceSets = new HashSet<SourceSet>();
registry.register(sourceSet -> {
addGraalVMAnnotationProcessorDependency(project, Set.of(sourceSet));
knownSourceSets.add(sourceSet);
});
var sourceSets = PluginsHelper.findSourceSets(project);
project.afterEvaluate(unused -> {
ListProperty<SourceSet> sets = extension.getProcessing().getAdditionalSourceSets();
if (sets.isPresent()) {
addGraalVMAnnotationProcessorDependency(project, sets.get());
@SuppressWarnings("deprecation")
var sets = extension.getProcessing().getAdditionalSourceSets();
var additionalSourceSets = sets.get();
for (SourceSet sourceSet : additionalSourceSets) {
if (!knownSourceSets.contains(sourceSet)) {
AnnotationProcessing.showAdditionalSourceSetDeprecationWarning(sourceSet);
registry.register(sourceSet1 -> addGraalVMAnnotationProcessorDependency(project, Set.of(sourceSet)));
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
kotlin = "1.8.22"
ksp = "1.8.22-1.0.11"
kotlin = "1.9.20"
ksp = "1.9.20-1.0.14"
docker = "9.3.3"
diffplug = "3.43.0"
shadow = "8.1.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.SourceSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;

Expand All @@ -14,19 +16,27 @@
* @since 1.0.0
*/
public class AnnotationProcessing implements AnnotationProcessingConfigBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationProcessing.class);

private final Property<String> group;
private final Property<Boolean> incremental;
private final Property<String> module;
private final ListProperty<String> annotations;
private final ListProperty<SourceSet> additionalSourceSets;
private final SourceSetConfigurer sourceSetConfigurer;

public static void showAdditionalSourceSetDeprecationWarning(SourceSet sourceSet) {
LOGGER.warn("Source set '{}' was declared using the additionalSourcesets property. Please use sourceSet(...) instead.", sourceSet.getName());
}

@Inject
public AnnotationProcessing(ObjectFactory objectFactory) {
public AnnotationProcessing(ObjectFactory objectFactory, SourceSetConfigurer sourceSetConfigurer) {
this.incremental = objectFactory.property(Boolean.class).convention(true);
this.module = objectFactory.property(String.class);
this.group = objectFactory.property(String.class);
this.annotations = objectFactory.listProperty(String.class);
this.additionalSourceSets = objectFactory.listProperty(SourceSet.class);
this.sourceSetConfigurer = sourceSetConfigurer;
}

/**
Expand Down Expand Up @@ -59,14 +69,19 @@ public ListProperty<String> getAnnotations() {

/**
* @return The additional configurations to apply annotation processing to.
* @deprecated Use {@link #sourceSets(SourceSet...)} instead.
*/
@Deprecated
public ListProperty<SourceSet> getAdditionalSourceSets() {
return additionalSourceSets;
}

@Override
public AnnotationProcessingConfigBuilder sourceSets(SourceSet... sourceSets) {
this.additionalSourceSets.addAll(sourceSets);
for (SourceSet sourceSet : sourceSets) {
sourceSetConfigurer.onSourceSet(sourceSet);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
public class MicronautBasePlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getExtensions().create("micronaut", MicronautExtension.class);
var registry = new SourceSetConfigurerRegistry();
project.getExtensions().add("micronautSourceSetConfigurer", registry);
project.getExtensions().create("micronaut", MicronautExtension.class, registry);
project.getConfigurations().create(MICRONAUT_BOMS_CONFIGURATION, conf -> {
conf.setCanBeResolved(false);
conf.setCanBeConsumed(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.plugins.PluginManager;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;
Expand All @@ -34,6 +33,7 @@
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -122,42 +122,54 @@ private void configureTesting(Project project, MicronautExtension micronautExten
private void configureMicronautBom(Project project, MicronautExtension micronautExtension) {
Configuration micronautBoms = project.getConfigurations().getByName(MICRONAUT_BOMS_CONFIGURATION);
PluginsHelper.maybeAddMicronautPlaformBom(project, micronautBoms);
var registry = project.getExtensions().getByType(SourceSetConfigurerRegistry.class);
var knownSourceSets = new HashSet<SourceSet>();
registry.register(sourceSet -> {
configureSourceSet(project, sourceSet, micronautBoms);
knownSourceSets.add(sourceSet);
});
project.afterEvaluate(p -> {
project.getConfigurations().configureEach(conf -> {
if (CONFIGURATIONS_TO_APPLY_BOMS.contains(conf.getName())) {
conf.extendsFrom(micronautBoms);
}
});
ListProperty<SourceSet> additionalSourceSets =
var additionalSourceSets =
micronautExtension.getProcessing().getAdditionalSourceSets();

if (additionalSourceSets.isPresent()) {
List<SourceSet> configurations = additionalSourceSets.get();
if (!configurations.isEmpty()) {
for (SourceSet sourceSet : configurations) {
String annotationProcessorConfigurationName = sourceSet
.getAnnotationProcessorConfigurationName();
String implementationConfigurationName = sourceSet
.getImplementationConfigurationName();
List<String> both = Arrays.asList(
implementationConfigurationName,
annotationProcessorConfigurationName
);
for (String configuration : both) {
Configuration conf = project.getConfigurations().findByName(configuration);
if (conf != null) {
conf.extendsFrom(micronautBoms);
}
if (!knownSourceSets.contains(sourceSet)) {
AnnotationProcessing.showAdditionalSourceSetDeprecationWarning(sourceSet);
configureSourceSet(project, sourceSet, micronautBoms);
}
configureAnnotationProcessors(p,
implementationConfigurationName,
annotationProcessorConfigurationName);
}
}
}
});
}

private static void configureSourceSet(Project project, SourceSet sourceSet, Configuration micronautBoms) {
String annotationProcessorConfigurationName = sourceSet
.getAnnotationProcessorConfigurationName();
String implementationConfigurationName = sourceSet
.getImplementationConfigurationName();
List<String> both = Arrays.asList(
implementationConfigurationName,
annotationProcessorConfigurationName
);
for (String configuration : both) {
Configuration conf = project.getConfigurations().findByName(configuration);
if (conf != null) {
conf.extendsFrom(micronautBoms);
}
}
configureAnnotationProcessors(project,
implementationConfigurationName,
annotationProcessorConfigurationName);
}


private void configureJava(Project project, TaskContainer tasks) {

Expand Down Expand Up @@ -250,7 +262,8 @@ private void configureGroovy(Project project, TaskContainer tasks, MicronautExte
}
}
}
ListProperty<SourceSet> additionalSourceSets = micronautExtension.getProcessing().getAdditionalSourceSets();
@SuppressWarnings("deprecation")
var additionalSourceSets = micronautExtension.getProcessing().getAdditionalSourceSets();
if (additionalSourceSets.isPresent()) {
List<SourceSet> sourceSets = additionalSourceSets.get();
for (SourceSet sourceSet : sourceSets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public abstract class MicronautExtension implements ExtensionAware {
public abstract Property<Boolean> getImportMicronautPlatform();

@Inject
public MicronautExtension(ObjectFactory objectFactory) {
this.processing = objectFactory.newInstance(AnnotationProcessing.class);
public MicronautExtension(ObjectFactory objectFactory, SourceSetConfigurer sourceSetConfigurer) {
this.processing = objectFactory.newInstance(AnnotationProcessing.class, sourceSetConfigurer);
this.version = objectFactory.property(String.class);
this.enableNativeImage = objectFactory.property(Boolean.class)
.convention(true);
Expand Down
Loading

0 comments on commit c85aea0

Please sign in to comment.