Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ configurations.runtimeClasspath {
}
```

In addition to creating a common configuration for resolution, a platform project can also be enabled to enforce
versions.

```kotlin
extraJavaModuleInfo {
platformDependency = project.provider { project.dependencies.platform(project.dependencies.create("org.example:bom:1.0.0")) }
}
```

## I have many automatic modules in my project. How can I convert them into proper modules and control what they export or require?

The plugin provides a set of `<sourceSet>moduleDescriptorRecommendations` tasks that generate the real module declarations utilizing [jdeps](https://docs.oracle.com/en/java/javase/11/tools/jdeps.html) and dependency metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.gradle.api.NamedDomainObjectProvider;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.MinimalExternalModuleDependency;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.model.ObjectFactory;
Expand Down Expand Up @@ -44,6 +45,8 @@ public abstract class ExtraJavaModuleInfoPluginExtension {

public abstract Property<String> getVersionsProvidingConfiguration();

public abstract Property<Dependency> getPlatformDependency();

/**
* Add full module information for a given Jar file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static org.gradle.api.attributes.Category.CATEGORY_ATTRIBUTE;
import static org.gradle.api.attributes.Category.ENFORCED_PLATFORM;
import static org.gradle.api.attributes.Category.LIBRARY;
import static org.gradle.api.attributes.Category.REGULAR_PLATFORM;
import static org.gradle.api.attributes.Usage.USAGE_ATTRIBUTE;

import java.io.Serializable;
Expand All @@ -15,6 +17,8 @@
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.artifacts.result.DependencyResult;
import org.gradle.api.artifacts.result.ResolvedDependencyResult;
import org.gradle.api.artifacts.result.UnresolvedDependencyResult;
Expand All @@ -34,6 +38,8 @@
public class PublishedMetadata implements Serializable {
private static final Attribute<String> CATEGORY_ATTRIBUTE_UNTYPED =
Attribute.of(CATEGORY_ATTRIBUTE.getName(), String.class);
private static final Attribute<Category> CATEGORY_ATTRIBUTE_TYPED =
Attribute.of(CATEGORY_ATTRIBUTE.getName(), Category.class);
private static final String DEFAULT_VERSION_SOURCE_CONFIGURATION = "definedDependenciesVersions";

private final String gav;
Expand All @@ -47,10 +53,16 @@ public class PublishedMetadata implements Serializable {
PublishedMetadata(String gav, Project project, ExtraJavaModuleInfoPluginExtension extension) {
this.gav = gav;

List<String> compileDependencies =
componentVariant(extension.getVersionsProvidingConfiguration(), project, Usage.JAVA_API);
List<String> runtimeDependencies =
componentVariant(extension.getVersionsProvidingConfiguration(), project, Usage.JAVA_RUNTIME);
List<String> compileDependencies = componentVariant(
extension.getVersionsProvidingConfiguration(),
extension.getPlatformDependency(),
project,
Usage.JAVA_API);
List<String> runtimeDependencies = componentVariant(
extension.getVersionsProvidingConfiguration(),
extension.getPlatformDependency(),
project,
Usage.JAVA_RUNTIME);

Stream.concat(compileDependencies.stream(), runtimeDependencies.stream())
.distinct()
Expand All @@ -67,7 +79,10 @@ public class PublishedMetadata implements Serializable {

@SuppressWarnings({"UnstableApiUsage", "unchecked"})
private List<String> componentVariant(
Provider<String> versionsProvidingConfiguration, Project project, String usage) {
Provider<String> versionsProvidingConfiguration,
Provider<Dependency> platformDependencyProvider,
Project project,
String usage) {
Configuration versionsSource;
if (versionsProvidingConfiguration.isPresent()) {
versionsSource = project.getConfigurations()
Expand All @@ -81,8 +96,29 @@ private List<String> componentVariant(
project.getExtensions().findByType(SourceSetContainer.class));
}

Configuration singleComponentVariantResolver = project.getConfigurations()
.detachedConfiguration(project.getDependencies().create(gav));
List<Dependency> dependencies = new ArrayList<>();
dependencies.add(project.getDependencies().create(gav));
if (platformDependencyProvider.isPresent()) {
if (!ModuleDependency.class.isAssignableFrom(
platformDependencyProvider.get().getClass())) {
throw new IllegalArgumentException("Unable to determine dependency '"
+ platformDependencyProvider.get().getName() + "' type");
}

ModuleDependency platformDependency = (ModuleDependency) platformDependencyProvider.get();
// A platform dependency must have the platform attribute specified.
Category category = platformDependency.getAttributes().getAttribute(CATEGORY_ATTRIBUTE_TYPED);
if (category == null
|| (!category.getName().equals(REGULAR_PLATFORM)
&& !category.getName().equals(ENFORCED_PLATFORM))) {
throw new IllegalArgumentException(
"Dependency '" + platformDependency.getName() + "' is not a platform");
}
dependencies.add(platformDependency);
}

Configuration singleComponentVariantResolver =
project.getConfigurations().detachedConfiguration(dependencies.toArray(new Dependency[0]));
singleComponentVariantResolver.setCanBeConsumed(false);
singleComponentVariantResolver.shouldResolveConsistentlyWith(versionsSource);
versionsSource.getAttributes().keySet().forEach(a -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,25 @@ class EdgeCasesFunctionalTest extends Specification {
expect:
build()
}

def "resolve against a platform project if specified"() {
given:
buildFile << """
val springBom = "org.springframework:spring-framework-bom:6.2.9"
dependencies {
implementation(platform(springBom))
implementation("org.springframework:spring-jcl")
}

extraJavaModuleInfo {
failOnAutomaticModules.set(true)
platformDependency.set(project.provider { project.dependencies.platform(project.dependencies.create(springBom)) })
module("org.springframework:spring-jcl", "spring.jcl")
}
"""

expect:
build()
}

}
Loading