Skip to content

Commit

Permalink
Let the user ignore automatic dependencies (#869)
Browse files Browse the repository at this point in the history
In some circumstances, automatic dependencies can get in the way. This
should be rare but there was no built-in mechanism to ignore such
dependencies. It is now possible to list these using the `micronaut`
extension, for example:

```
micronaut {
    ignoredAutomaticDependencies.add("io.micronaut.data:micronaut-data-processor")
}
```

The responsibility of adding these is then shifted to the user.

Fixes micronaut-projects/micronaut-sql#1157
  • Loading branch information
melix authored Nov 14, 2023
1 parent 341f391 commit 78058d7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;

import javax.inject.Inject;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -34,6 +35,16 @@ public abstract class MicronautExtension implements ExtensionAware {
*/
public abstract Property<Boolean> getImportMicronautPlatform();

/**
* The Micronaut plugins can automatically add dependencies to your project.
* If, for some reason, a dependency shouldn't be automatically added, you can
* add its coordinates to this set. The format is "group:name". It must not include
* the version.
*
* @return the set of ignored automatic dependencies, as group:name strings.
*/
public abstract SetProperty<String> getIgnoredAutomaticDependencies();

@Inject
public MicronautExtension(ObjectFactory objectFactory, SourceSetConfigurer sourceSetConfigurer) {
this.processing = objectFactory.newInstance(AnnotationProcessing.class, sourceSetConfigurer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import org.gradle.api.artifacts.MinimalExternalModuleDependency;
import org.gradle.api.artifacts.VersionCatalog;
import org.gradle.api.artifacts.VersionCatalogsExtension;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* Represents a dependency which is automatically
Expand All @@ -43,36 +44,42 @@ public record AutomaticDependency(
) {
public void applyTo(Project p) {
p.getPlugins().withType(MicronautComponentPlugin.class, unused -> {
DependencyHandler dependencyHandler = p.getDependencies();
MicronautExtension micronautExtension = p.getExtensions().getByType(MicronautExtension.class);
dependencyHandler.addProvider(configuration, p.getProviders().provider(() -> {
if (versionProperty.isPresent()) {
Property<String> provider = (Property<String>) micronautExtension.getExtensions().findByName(versionProperty.get().dslName());
if (provider != null && provider.isPresent()) {
return dependencyHandler.create(coordinates + ":" + provider.get());
var dependencyHandler = p.getDependencies();
var micronautExtension = p.getExtensions().getByType(MicronautExtension.class);
var ignoredDependencies = micronautExtension.getIgnoredAutomaticDependencies();
p.getConfigurations().getByName(configuration).getDependencies().addAllLater(
p.getProviders().provider(() -> {
var ignored = ignoredDependencies.getOrElse(Set.of());
if (ignored.contains(coordinates)) {
return List.of();
}
}
// If the Micronaut version catalog is applied via the settings plugin, we won't use an "empty" version
// but fetch it from the catalog if possible
VersionCatalogsExtension versionCatalogs = p.getExtensions().findByType(VersionCatalogsExtension.class);
if (versionCatalogs != null) {
Optional<VersionCatalog> mn = versionCatalogs.find("mn");
if (mn.isPresent()) {
VersionCatalog micronautCatalog = mn.get();
Optional<Provider<MinimalExternalModuleDependency>> dependencyProvider = micronautCatalog.getLibraryAliases()
if (versionProperty.isPresent()) {
Property<String> provider = (Property<String>) micronautExtension.getExtensions().findByName(versionProperty.get().dslName());
if (provider != null && provider.isPresent()) {
return List.of(dependencyHandler.create(coordinates + ":" + provider.get()));
}
}
// If the Micronaut version catalog is applied via the settings plugin, we won't use an "empty" version
// but fetch it from the catalog if possible
VersionCatalogsExtension versionCatalogs = p.getExtensions().findByType(VersionCatalogsExtension.class);
if (versionCatalogs != null) {
Optional<VersionCatalog> mn = versionCatalogs.find("mn");
if (mn.isPresent()) {
VersionCatalog micronautCatalog = mn.get();
Optional<Provider<MinimalExternalModuleDependency>> dependencyProvider = micronautCatalog.getLibraryAliases()
.stream()
.map(micronautCatalog::findLibrary)
.map(Optional::get)
.filter(d -> coordinates.equals(d.get().getModule().toString()))
.findFirst();
if (dependencyProvider.isPresent()) {
return dependencyProvider.get().get();
if (dependencyProvider.isPresent()) {
return List.of(dependencyProvider.get().get());
}
}
}
}
return dependencyHandler.create(coordinates);
}));

return List.of(dependencyHandler.create(coordinates));
})
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,36 @@ public class ExampleTest {
result.output.contains('Could not find io.micronaut:micronaut-http-server-netty:2048')

}

def "can ignore an automatic dependency"() {
given:
settingsFile << "rootProject.name = 'hello-world'"
buildFile << """
plugins {
id "io.micronaut.minimal.application"
}
micronaut {
version "$micronautVersion"
runtime "netty"
testRuntime "junit5"
ignoredAutomaticDependencies.add("io.micronaut:micronaut-inject-java")
}
$repositoriesBlock
mainClassName="example.Application"
"""
testProjectDir.newFolder("src", "test", "java", "example")
def javaFile = writeExampleClass()
when:
def result = build('test')

def task = result.task(":test")
println result.output

then:
!result.output.contains('Creating bean classes for 1 type elements')
task.outcome == TaskOutcome.FAILED

}
}

0 comments on commit 78058d7

Please sign in to comment.