Skip to content

Commit

Permalink
Add an option to not automatically add BOM (#701)
Browse files Browse the repository at this point in the history
* Add an option to not automatically add BOM

This option has (at least) a couple use cases:

1. the issue described in #681, when a user wants to drive the
Micronaut version from a catalog, or directly in their dependencies
(although this is probably redundant with #694)

2. we use the Gradle plugins in our module builds, where it's going
to look for the platform BOM, which is not necessarily what we
want, since we want to use local versions of projects and possibly
override transitive versions

Closes #681

* Add documentation
  • Loading branch information
melix authored Apr 24, 2023
1 parent 8d0b353 commit a8c32f0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -57,20 +55,19 @@
* library, or a Micronaut application.
*/
public class MicronautComponentPlugin implements Plugin<Project> {
private final static List<String> SOURCESETS = Arrays.asList(
private static final List<String> SOURCESETS = List.of(
SourceSet.MAIN_SOURCE_SET_NAME,
SourceSet.TEST_SOURCE_SET_NAME
);
private final static Set<String> CONFIGURATIONS_TO_APPLY_BOMS = Collections.unmodifiableSet(new HashSet<String>() {{
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<String> 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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public abstract class MicronautExtension implements ExtensionAware {
private final Property<MicronautRuntime> runtime;
private final Property<MicronautTestRuntime> 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<Boolean> getImportMicronautPlatform();

@Inject
public MicronautExtension(ObjectFactory objectFactory) {
this.processing = objectFactory.newInstance(AnnotationProcessing.class);
Expand All @@ -35,6 +44,7 @@ public MicronautExtension(ObjectFactory objectFactory) {
.convention(MicronautRuntime.NONE);
this.testRuntime = objectFactory.property(MicronautTestRuntime.class)
.convention(MicronautTestRuntime.NONE);
getImportMicronautPlatform().convention(true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> 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");
Expand All @@ -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<String> findVersionFromProjectProperties(Project p) {
return p.getProviders().provider(() -> {
Object micronautVersion = p.getProperties().get(MICRONAUT_VERSION_PROPERTY);
Expand Down Expand Up @@ -200,4 +217,8 @@ static void registerVersionExtensions(List<ConfigurableVersionProperty> properti
micronautExtension.getExtensions().add(type, v.dslName(), property);
});
}

public static Dependency resolveMicronautPlatform(DependencyHandler dependencyHandler, String micronautVersion) {
return dependencyHandler.platform(MICRONAUT_PLATFORM_COORDINATES + ":" + micronautVersion);
}
}
10 changes: 9 additions & 1 deletion src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit a8c32f0

Please sign in to comment.