Skip to content

Commit

Permalink
Add built-in "platform" support
Browse files Browse the repository at this point in the history
Also supports a "platform" as variant of the application.
  • Loading branch information
jjohannes committed Jul 6, 2023
1 parent a7a59f1 commit f7dbb4e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.ProjectDependency;
import org.gradle.api.artifacts.VersionCatalog;
import org.gradle.api.artifacts.VersionCatalogsExtension;
Expand All @@ -37,6 +38,7 @@
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradlex.javamodule.dependencies.internal.compile.AddSyntheticModulesToCompileClasspathAction;
Expand All @@ -45,6 +47,7 @@

import javax.inject.Inject;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -299,16 +302,42 @@ public void versionsFromConsistentResolution(String... versionsProvidingProjects
c.setCanBeConsumed(false);
c.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, getObjects().named(Usage.class, Usage.JAVA_RUNTIME));
});
getConfigurations().all(c -> {
if (c != mainRuntimeClasspath) {
getConfigurations().configureEach(c -> {
if (c.isCanBeResolved() && c != mainRuntimeClasspath) {
c.shouldResolveConsistentlyWith(mainRuntimeClasspath);
}
});
for (String versionsProvidingProject : versionsProvidingProjects) {
getDependencies().add(mainRuntimeClasspath.getName(), getDependencies().project(Collections.singletonMap("path", versionsProvidingProject)));
getDependencies().add(mainRuntimeClasspath.getName(), createDependency(versionsProvidingProject));
}
}

public void versionsFromPlatformAndConsistentResolution(String platformProject, String... versionsProvidingProjects) {
boolean platformInJavaProject = Arrays.asList(versionsProvidingProjects).contains(platformProject);
getSourceSets().configureEach(sourceSet -> getConfigurations().getByName(sourceSet.getImplementationConfigurationName()).withDependencies(d -> {
Dependency platformDependency = getDependencies().platform(createDependency(platformProject));
if (platformInJavaProject) {
if (platformProject.startsWith(":")) {
String capability = ((ProjectDependency) platformDependency).getDependencyProject().getGroup() + platformProject + "-platform";
((ProjectDependency) platformDependency).capabilities(c -> c.requireCapability(capability));
} else if (platformDependency instanceof ModuleDependency) {
String capability = platformProject + "-platform";
((ModuleDependency) platformDependency).capabilities(c -> c.requireCapability(capability));
}
}
d.add(platformDependency);
}));

versionsFromConsistentResolution(versionsProvidingProjects);
}

private Dependency createDependency(String project) {
boolean isProjectInBuild = project.startsWith(":");
return getDependencies().create(isProjectInBuild
? getDependencies().project(Collections.singletonMap("path", project))
: project);
}

/**
* Adds support for compiling module-info.java in the given source set with the given task,
* if 'requires runtime' dependencies are used.
Expand Down Expand Up @@ -389,6 +418,9 @@ private String moduleDebugInfo(String moduleName, File moduleInfoFile, File root
@Inject
protected abstract ConfigurationContainer getConfigurations();

@Inject
protected abstract SourceSetContainer getSourceSets();

ModuleInfoCache getModuleInfoCache() {
return moduleInfoCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,58 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.attributes.Usage;
import org.gradle.api.internal.artifacts.configurations.ConfigurationInternal;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaPlatformPlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradlex.javamodule.dependencies.dsl.ModuleVersions;

import static org.gradle.api.attributes.Usage.JAVA_RUNTIME;
import static org.gradle.api.plugins.JavaPlatformPlugin.API_CONFIGURATION_NAME;

@SuppressWarnings("unused")
@NonNullApi
public abstract class JavaModuleVersionsPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
project.getPlugins().withType(JavaPlugin.class, javaPlugin -> setupForProject(project));
project.getPlugins().withType(JavaPlatformPlugin.class, javaPlugin -> setupForProject(project));
project.getPlugins().withType(JavaPlatformPlugin.class, plugin -> setupForJavaPlatformProject(project));
project.getPlugins().withType(JavaPlugin.class, plugin -> setupForJavaProject(project));
}

private void setupForJavaPlatformProject(Project project) {
setupVersionsDSL(project, project.getConfigurations().getByName(API_CONFIGURATION_NAME));
}

private void setupForJavaProject(Project project) {
ObjectFactory objects = project.getObjects();
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);

Configuration versions = project.getConfigurations().create("versions", c -> {
c.setCanBeResolved(false);
c.setCanBeConsumed(false);
});

project.getConfigurations().create("platformElements", c -> {
c.setCanBeResolved(false);
c.setCanBeConsumed(true);
c.setVisible(false);
c.extendsFrom(versions);
c.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.class, JAVA_RUNTIME));
// c.getAttributes().attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.class, REGULAR_PLATFORM));
((ConfigurationInternal) c).beforeLocking(conf -> {
c.getOutgoing().capability(project.getGroup() + ":" + project.getName() + "-platform:" + project.getVersion());
});
});

setupVersionsDSL(project, versions);
}

private void setupForProject(Project project) {
private void setupVersionsDSL(Project project, Configuration configuration) {
project.getPlugins().apply(JavaModuleDependenciesPlugin.class);
JavaModuleDependenciesExtension javaModuleDependencies = project.getExtensions().getByType(JavaModuleDependenciesExtension.class);
Configuration configuration = project.getConfigurations().findByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME);
if (configuration == null) {
configuration = project.getConfigurations().getByName(JavaPlatformPlugin.API_CONFIGURATION_NAME);
}
project.getExtensions().create("moduleInfo", ModuleVersions.class, configuration, javaModuleDependencies);
}

Expand Down

0 comments on commit f7dbb4e

Please sign in to comment.