diff --git a/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautComponentPlugin.java b/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautComponentPlugin.java index d9444b77..12da10b5 100644 --- a/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautComponentPlugin.java +++ b/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautComponentPlugin.java @@ -18,7 +18,6 @@ import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; -import org.gradle.api.artifacts.Dependency; import org.gradle.api.artifacts.dsl.DependencyHandler; import org.gradle.api.plugins.BasePlugin; import org.gradle.api.plugins.JavaPlugin; @@ -37,7 +36,6 @@ 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; @@ -57,20 +55,19 @@ * library, or a Micronaut application. */ public class MicronautComponentPlugin implements Plugin { - private final static List SOURCESETS = Arrays.asList( + private static final List SOURCESETS = List.of( SourceSet.MAIN_SOURCE_SET_NAME, SourceSet.TEST_SOURCE_SET_NAME ); - private final static Set CONFIGURATIONS_TO_APPLY_BOMS = Collections.unmodifiableSet(new HashSet() {{ - add(ANNOTATION_PROCESSOR_CONFIGURATION_NAME); - add(TEST_ANNOTATION_PROCESSOR_CONFIGURATION_NAME); - add(API_CONFIGURATION_NAME); - add(IMPLEMENTATION_CONFIGURATION_NAME); - add(COMPILE_ONLY_CONFIGURATION_NAME); - }}); + private static final Set CONFIGURATIONS_TO_APPLY_BOMS = Set.of( + ANNOTATION_PROCESSOR_CONFIGURATION_NAME, + TEST_ANNOTATION_PROCESSOR_CONFIGURATION_NAME, + API_CONFIGURATION_NAME, + IMPLEMENTATION_CONFIGURATION_NAME, + COMPILE_ONLY_CONFIGURATION_NAME + ); public static final String MICRONAUT_BOMS_CONFIGURATION = "micronautBoms"; public static final String INSPECT_RUNTIME_CLASSPATH_TASK_NAME = "inspectRuntimeClasspath"; - public static final String MICRONAUT_PLATFORM_COORDINATES = "io.micronaut.platform:micronaut-platform"; @Override public void apply(Project project) { @@ -126,11 +123,8 @@ private void configureTesting(Project project, MicronautExtension micronautExten private void configureMicronautBom(Project project, MicronautExtension micronautExtension) { Configuration micronautBoms = project.getConfigurations().getByName(MICRONAUT_BOMS_CONFIGURATION); - DependencyHandler dependencyHandler = project.getDependencies(); + PluginsHelper.maybeAddMicronautPlaformBom(project, micronautBoms); project.afterEvaluate(p -> { - dependencyHandler.addProvider(micronautBoms.getName(), PluginsHelper.findMicronautVersion(project).map(micronautVersion -> - resolveMicronautPlatform(dependencyHandler, micronautVersion) - )); project.getConfigurations().configureEach(conf -> { if (CONFIGURATIONS_TO_APPLY_BOMS.contains(conf.getName())) { conf.extendsFrom(micronautBoms); @@ -167,10 +161,6 @@ private void configureMicronautBom(Project project, MicronautExtension micronaut } - public static Dependency resolveMicronautPlatform(DependencyHandler dependencyHandler, String micronautVersion) { - return dependencyHandler.platform(MICRONAUT_PLATFORM_COORDINATES + ":" + micronautVersion); - } - private void configureJava(Project project, TaskContainer tasks) { project.afterEvaluate(p -> { diff --git a/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautExtension.java b/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautExtension.java index d63872da..3190c422 100644 --- a/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautExtension.java +++ b/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautExtension.java @@ -25,6 +25,15 @@ public abstract class MicronautExtension implements ExtensionAware { private final Property runtime; private final Property testRuntime; + /** + * If set to false, then the Micronaut Gradle plugins will not automatically + * add the Micronaut Platform BOM to your dependencies. It becomes your + * responsibility to add it directly, or to provide explicit versions for + * Micronaut modules. + * @return the import platform flag. Defaults to true. + */ + public abstract Property getImportMicronautPlatform(); + @Inject public MicronautExtension(ObjectFactory objectFactory) { this.processing = objectFactory.newInstance(AnnotationProcessing.class); @@ -35,6 +44,7 @@ public MicronautExtension(ObjectFactory objectFactory) { .convention(MicronautRuntime.NONE); this.testRuntime = objectFactory.property(MicronautTestRuntime.class) .convention(MicronautTestRuntime.NONE); + getImportMicronautPlatform().convention(true); } /** diff --git a/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautKotlinSupport.java b/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautKotlinSupport.java index 0e312a43..a8a41de0 100644 --- a/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautKotlinSupport.java +++ b/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautKotlinSupport.java @@ -22,7 +22,7 @@ import java.util.List; import java.util.function.Consumer; -import static io.micronaut.gradle.MicronautComponentPlugin.resolveMicronautPlatform; +import static io.micronaut.gradle.PluginsHelper.resolveMicronautPlatform; import static io.micronaut.gradle.PluginsHelper.configureAnnotationProcessors; /** diff --git a/minimal-plugin/src/main/java/io/micronaut/gradle/PluginsHelper.java b/minimal-plugin/src/main/java/io/micronaut/gradle/PluginsHelper.java index f6049f64..5449d936 100644 --- a/minimal-plugin/src/main/java/io/micronaut/gradle/PluginsHelper.java +++ b/minimal-plugin/src/main/java/io/micronaut/gradle/PluginsHelper.java @@ -19,16 +19,20 @@ import io.micronaut.gradle.internal.ConfigurableVersionProperty; import org.gradle.api.InvalidUserCodeException; import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.Dependency; import org.gradle.api.artifacts.DependencySet; import org.gradle.api.artifacts.VersionCatalog; import org.gradle.api.artifacts.VersionCatalogsExtension; import org.gradle.api.artifacts.VersionConstraint; +import org.gradle.api.artifacts.dsl.DependencyHandler; import org.gradle.api.provider.Property; import org.gradle.api.provider.Provider; import org.gradle.api.reflect.TypeOf; import org.gradle.api.tasks.SourceSet; import java.io.File; +import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; @@ -40,6 +44,7 @@ import static org.gradle.api.plugins.JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME; public abstract class PluginsHelper { + public static final String MICRONAUT_PLATFORM_COORDINATES = "io.micronaut.platform:micronaut-platform"; static final List ANNOTATION_PROCESSOR_MODULES = List.of("inject-java"); public static final ConfigurableVersionProperty CORE_VERSION_PROPERTY = ConfigurableVersionProperty.of("core"); public static final ConfigurableVersionProperty DATA_VERSION_PROPERTY = ConfigurableVersionProperty.of("data"); @@ -65,6 +70,18 @@ public abstract class PluginsHelper { VALIDATION_VERSION_PROPERTY ); + public static void maybeAddMicronautPlaformBom(Project p, Configuration configuration) { + MicronautExtension micronautExtension = p.getExtensions().findByType(MicronautExtension.class); + configuration.getDependencies().addAllLater( + micronautExtension.getImportMicronautPlatform().zip(PluginsHelper.findMicronautVersion(p), (usePlatform, version) -> { + if (Boolean.TRUE.equals(usePlatform)) { + return List.of(resolveMicronautPlatform(p.getDependencies(), version)); + } + return Collections.emptyList(); + }) + ); + } + private static Provider findVersionFromProjectProperties(Project p) { return p.getProviders().provider(() -> { Object micronautVersion = p.getProperties().get(MICRONAUT_VERSION_PROPERTY); @@ -200,4 +217,8 @@ static void registerVersionExtensions(List properti micronautExtension.getExtensions().add(type, v.dslName(), property); }); } + + public static Dependency resolveMicronautPlatform(DependencyHandler dependencyHandler, String micronautVersion) { + return dependencyHandler.platform(MICRONAUT_PLATFORM_COORDINATES + ":" + micronautVersion); + } } diff --git a/src/docs/asciidoc/index.adoc b/src/docs/asciidoc/index.adoc index b001fae6..abfe0674 100644 --- a/src/docs/asciidoc/index.adoc +++ b/src/docs/asciidoc/index.adoc @@ -312,6 +312,10 @@ Complete example with the default settings: ---- micronaut { version "{micronaut-version}" + // If set to false, then the `io.micronaut.platform:micronaut-platform` BOM + // will not be automatically applied, and you will have to add it yourself + // to your dependencies, or specify versions of Micronaut modules explicitly + importMicronautPlatform = true processing { // Sets whether incremental annotation processing is enabled incremental true @@ -340,7 +344,11 @@ micronaut { ---- micronaut { version.set("{micronaut-version}") - processing { + // If set to false, then the `io.micronaut.platform:micronaut-platform` BOM + // will not be automatically applied, and you will have to add it yourself + // to your dependencies, or specify versions of Micronaut modules explicitly + importMicronautPlatform.set(true) + processing { // Sets whether incremental annotation processing is enabled incremental.set(true) // Sets the module name. diff --git a/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/MicronautTestResourcesConsumerPlugin.java b/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/MicronautTestResourcesConsumerPlugin.java index 2196cb80..d0ed924b 100644 --- a/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/MicronautTestResourcesConsumerPlugin.java +++ b/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/MicronautTestResourcesConsumerPlugin.java @@ -23,13 +23,11 @@ import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.ConfigurationContainer; import org.gradle.api.artifacts.ProjectDependency; -import org.gradle.api.artifacts.dsl.DependencyHandler; import org.gradle.api.attributes.AttributeContainer; import org.gradle.api.attributes.Usage; import org.gradle.api.plugins.PluginManager; import static io.micronaut.gradle.MicronautComponentPlugin.MICRONAUT_BOMS_CONFIGURATION; -import static io.micronaut.gradle.MicronautComponentPlugin.resolveMicronautPlatform; /** * A lightweight test resources plugin, which requires @@ -52,10 +50,7 @@ public void apply(Project project) { private Configuration createTestResourcesExtension(Project project) { ConfigurationContainer configurations = project.getConfigurations(); Configuration boms = configurations.findByName(MICRONAUT_BOMS_CONFIGURATION); - DependencyHandler dependencyHandler = project.getDependencies(); - dependencyHandler.addProvider(MICRONAUT_BOMS_CONFIGURATION, PluginsHelper.findMicronautVersion(project).map(micronautVersion -> - resolveMicronautPlatform(dependencyHandler, micronautVersion) - )); + PluginsHelper.maybeAddMicronautPlaformBom(project, boms); return project.getConfigurations().create(MicronautTestResourcesPlugin.TESTRESOURCES_CONFIGURATION, conf -> { conf.extendsFrom(boms); conf.setCanBeConsumed(false); diff --git a/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/MicronautTestResourcesPlugin.java b/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/MicronautTestResourcesPlugin.java index 6bd2ba92..b4baa719 100644 --- a/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/MicronautTestResourcesPlugin.java +++ b/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/MicronautTestResourcesPlugin.java @@ -64,7 +64,6 @@ import java.util.stream.Stream; import static io.micronaut.gradle.MicronautComponentPlugin.MICRONAUT_BOMS_CONFIGURATION; -import static io.micronaut.gradle.MicronautComponentPlugin.resolveMicronautPlatform; import static java.util.stream.Stream.concat; /** @@ -273,11 +272,7 @@ private TestResourcesConfiguration createTestResourcesConfiguration(Project proj MicronautExtension micronautExtension = PluginsHelper.findMicronautExtension(project); TestResourcesConfiguration testResources = micronautExtension.getExtensions().create("testResources", TestResourcesConfiguration.class); ProviderFactory providers = project.getProviders(); - testResources.getEnabled().convention( - micronautExtension.getVersion() - .orElse(providers.gradleProperty("micronautVersion")) - .map(MicronautTestResourcesPlugin::isAtLeastMicronaut3dot5) - ); + testResources.getEnabled().convention(true); testResources.getVersion().convention(VersionInfo.getVersion()); testResources.getExplicitPort().convention(explicitPort); testResources.getInferClasspath().convention(true); @@ -401,10 +396,7 @@ public void beforeComplete() { private static Configuration createTestResourcesServerConfiguration(Project project) { ConfigurationContainer configurations = project.getConfigurations(); Configuration boms = configurations.findByName(MICRONAUT_BOMS_CONFIGURATION); - DependencyHandler dependencyHandler = project.getDependencies(); - dependencyHandler.addProvider(MICRONAUT_BOMS_CONFIGURATION, PluginsHelper.findMicronautVersion(project).map(micronautVersion -> - resolveMicronautPlatform(dependencyHandler, micronautVersion) - )); + PluginsHelper.maybeAddMicronautPlaformBom(project, boms); return configurations.create(TESTRESOURCES_CONFIGURATION, conf -> { conf.extendsFrom(boms); conf.setDescription("Dependencies for the Micronaut test resources service");