Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply substitution only to configurations on java SourceSets #10

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,47 @@
*/
package com.palantir.gradle.jakartapackagealignment;

import com.google.common.collect.ImmutableSet;
import java.util.Set;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.component.ModuleComponentSelector;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;

public class JakartaPackageAlignmentPlugin implements Plugin<Project> {
@Override
public final void apply(Project project) {
project.getExtensions().getByType(SourceSetContainer.class).configureEach(sourceSet -> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps withPlugin to ensure SourceSetContainer exists?

Copy link
Contributor Author

@bjlaub bjlaub Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be using withPlugin on java, java-library, or both?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is java-base sufficient? I think it's an ancestor of both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also, looks like java-library will apply java as well, so i think either java or java-base should be sufficient

configureAllConfigurationsForSourceSet(project, sourceSet);
});
}

private void configureAllConfigurationsForSourceSet(Project project, SourceSet sourceSet) {
// see: https://docs.gradle.org/current/userguide/java_plugin.html#java_source_set_configurations
Set<String> configurationsToConfigure = ImmutableSet.of(
sourceSet.getCompileClasspathConfigurationName(),
sourceSet.getRuntimeClasspathConfigurationName(),
sourceSet.getAnnotationProcessorConfigurationName());

project.getConfigurations().configureEach(configuration -> {
configuration.getResolutionStrategy().getDependencySubstitution().all(dep -> {
if (dep.getRequested() instanceof ModuleComponentSelector) {
ModuleComponentSelector selector = (ModuleComponentSelector) dep.getRequested();
VersionMappings.getReplacement(selector)
.ifPresent(replacement -> dep.useTarget(
replacement,
"forced to Java EE 8 dependency because the requested Jakarta "
+ "dependency is < Jakarta EE 9"));
}
});
if (configurationsToConfigure.contains(configuration.getName())) {
configureConfigurationForSubstitution(configuration);
}
});
}

private void configureConfigurationForSubstitution(Configuration configuration) {
configuration.getResolutionStrategy().getDependencySubstitution().all(dep -> {
if (dep.getRequested() instanceof ModuleComponentSelector) {
ModuleComponentSelector selector = (ModuleComponentSelector) dep.getRequested();
VersionMappings.getReplacement(selector)
.ifPresent(replacement -> dep.useTarget(
replacement,
"forced to Java EE 8 dependency because the requested Jakarta "
+ "dependency is < Jakarta EE 9"));
}
});
}
}