Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# JVM Dependency Conflict Resolution Gradle plugin - Changelog

## Version 2.4
* [Fix] [#238](https://github.com/gradlex-org/jvm-dependency-conflict-resolution/issues/238) Patch DSL now works for dependencies with non-standard variant names (e.g. com.google.guava).

## Version 2.3
* [New Rule] [#66](https://github.com/gradlex-org/jvm-dependency-conflict-resolution/issues/66) itext:itext / com.lowagie:itext (Thanks [Björn Kautler](https://github.com/Vampire) for reporting)
* [New Rule] [#222](https://github.com/gradlex-org/jvm-dependency-conflict-resolution/issues/222) dk.brics.automaton:automaton / dk.brics:automaton (Thanks [Ketan Padegaonkar](https://github.com/ketan) for reporting)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import org.gradle.api.artifacts.CacheableRule;
import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.ComponentMetadataRule;
import org.gradle.api.attributes.Category;
import org.gradle.api.attributes.Usage;

import javax.inject.Inject;

import static org.gradlex.jvm.dependency.conflict.resolution.rules.VariantSelection.allVariantsMatching;

/**
* See:
* <a href="https://docs.gradle.org/current/userguide/component_metadata_rules.html#fixing_wrong_dependency_details">
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
*/
@CacheableRule
public abstract class AddApiDependencyMetadataRule implements ComponentMetadataRule {
Expand All @@ -39,6 +43,8 @@ public AddApiDependencyMetadataRule(String dependency) {

@Override
public void execute(ComponentMetadataContext context) {
context.getDetails().allVariants(v -> v.withDependencies(d -> d.add(dependency)));
allVariantsMatching(context,
id -> (id.matches(Usage.USAGE_ATTRIBUTE, Usage.JAVA_API) || id.matches(Usage.USAGE_ATTRIBUTE, Usage.JAVA_RUNTIME)) && id.matches(Category.CATEGORY_ATTRIBUTE, Category.LIBRARY),
v -> v.withDependencies(d -> d.add(dependency)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import org.gradle.api.artifacts.CacheableRule;
import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.ComponentMetadataRule;
import org.gradle.api.attributes.Category;
import org.gradle.api.attributes.Usage;

import javax.inject.Inject;

import static org.gradlex.jvm.dependency.conflict.resolution.rules.VariantSelection.allVariantsMatching;

/**
* See:
* <a href="https://docs.gradle.org/current/userguide/component_metadata_rules.html#fixing_wrong_dependency_details">
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
*/
@CacheableRule
public abstract class AddCompileOnlyApiDependencyMetadataRule implements ComponentMetadataRule {
Expand All @@ -39,7 +43,8 @@ public AddCompileOnlyApiDependencyMetadataRule(String dependency) {

@Override
public void execute(ComponentMetadataContext context) {
context.getDetails().withVariant("compile", v -> v.withDependencies(d -> d.add(dependency))); // .pom
context.getDetails().withVariant("apiElements", v -> v.withDependencies(d -> d.add(dependency))); // .module
allVariantsMatching(context,
id -> (id.matches(Usage.USAGE_ATTRIBUTE, Usage.JAVA_API) && id.matches(Category.CATEGORY_ATTRIBUTE, Category.LIBRARY)),
v -> v.withDependencies(d -> d.add(dependency)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import org.gradle.api.artifacts.CacheableRule;
import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.ComponentMetadataRule;
import org.gradle.api.attributes.Category;
import org.gradle.api.attributes.Usage;

import javax.inject.Inject;

import static org.gradlex.jvm.dependency.conflict.resolution.rules.VariantSelection.allVariantsMatching;

/**
* See:
* <a href="https://docs.gradle.org/current/userguide/component_metadata_rules.html#fixing_wrong_dependency_details">
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
*/
@CacheableRule
public abstract class AddRuntimeOnlyDependencyMetadataRule implements ComponentMetadataRule {
Expand All @@ -39,7 +43,8 @@ public AddRuntimeOnlyDependencyMetadataRule(String dependency) {

@Override
public void execute(ComponentMetadataContext context) {
context.getDetails().withVariant("runtime", v -> v.withDependencies(d -> d.add(dependency))); // .pom
context.getDetails().withVariant("runtimeElements", v -> v.withDependencies(d -> d.add(dependency))); // .module
allVariantsMatching(context,
id -> id.matches(Usage.USAGE_ATTRIBUTE, Usage.JAVA_RUNTIME) && id.matches(Category.CATEGORY_ATTRIBUTE, Category.LIBRARY),
v -> v.withDependencies(d -> d.add(dependency)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
import org.gradle.api.artifacts.CacheableRule;
import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.ComponentMetadataRule;
import org.gradle.api.attributes.Category;
import org.gradle.api.attributes.Usage;

import javax.inject.Inject;
import java.util.stream.Collectors;

import static org.gradlex.jvm.dependency.conflict.resolution.rules.VariantSelection.allVariantsMatching;

/**
* See:
* <a href="https://docs.gradle.org/current/userguide/component_metadata_rules.html#fixing_wrong_dependency_details">
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
*/
@CacheableRule
public abstract class ReduceToCompileOnlyApiDependencyMetadataRule implements ComponentMetadataRule {
Expand All @@ -40,7 +44,8 @@ public ReduceToCompileOnlyApiDependencyMetadataRule(String dependency) {

@Override
public void execute(ComponentMetadataContext context) {
context.getDetails().withVariant("runtime", v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList())))); // .pom
context.getDetails().withVariant("runtimeElements", v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList())))); // .module
allVariantsMatching(context,
id -> id.matches(Usage.USAGE_ATTRIBUTE, Usage.JAVA_RUNTIME) && id.matches(Category.CATEGORY_ATTRIBUTE, Category.LIBRARY),
v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
import org.gradle.api.artifacts.CacheableRule;
import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.ComponentMetadataRule;
import org.gradle.api.attributes.Category;
import org.gradle.api.attributes.Usage;

import javax.inject.Inject;
import java.util.stream.Collectors;

import static org.gradlex.jvm.dependency.conflict.resolution.rules.VariantSelection.allVariantsMatching;

/**
* See:
* <a href="https://docs.gradle.org/current/userguide/component_metadata_rules.html#fixing_wrong_dependency_details">
Expand All @@ -40,7 +44,8 @@ public ReduceToRuntimeOnlyDependencyMetadataRule(String dependency) {

@Override
public void execute(ComponentMetadataContext context) {
context.getDetails().withVariant("compile", v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList())))); // .pom
context.getDetails().withVariant("apiElements", v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList())))); // .module
allVariantsMatching(context,
id -> id.matches(Usage.USAGE_ATTRIBUTE, Usage.JAVA_API) && id.matches(Category.CATEGORY_ATTRIBUTE, Category.LIBRARY),
v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList())))); // .module
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
import org.gradle.api.artifacts.CacheableRule;
import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.ComponentMetadataRule;
import org.gradle.api.attributes.Category;
import org.gradle.api.attributes.Usage;

import javax.inject.Inject;
import java.util.stream.Collectors;

import static org.gradlex.jvm.dependency.conflict.resolution.rules.VariantSelection.allVariantsMatching;

/**
* See:
* <a href="https://docs.gradle.org/current/userguide/component_metadata_rules.html#fixing_wrong_dependency_details">
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
*/
@CacheableRule
public abstract class RemoveDependencyMetadataRule implements ComponentMetadataRule {
Expand All @@ -40,7 +44,8 @@ public RemoveDependencyMetadataRule(String dependency) {

@Override
public void execute(ComponentMetadataContext context) {
context.getDetails().allVariants(v -> v.withDependencies(d ->
d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList()))));
allVariantsMatching(context,
id -> (id.matches(Usage.USAGE_ATTRIBUTE, Usage.JAVA_API) || id.matches(Usage.USAGE_ATTRIBUTE, Usage.JAVA_RUNTIME)) && id.matches(Category.CATEGORY_ATTRIBUTE, Category.LIBRARY),
v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList()))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright the GradleX team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gradlex.jvm.dependency.conflict.resolution.rules;

import org.gradle.api.Named;
import org.gradle.api.attributes.Attribute;

import java.util.Map;
import java.util.Objects;

class VariantIdentification {

private final Map<String, String> attributes;

VariantIdentification(Map<String, String> attributes) {
this.attributes = attributes;
}

boolean matches(Attribute<? extends Named> attribute, String value) {
return matches(attribute.getName(), value);
}

boolean matches(String attribute, String value) {
return Objects.equals(attributes.get(attribute), value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright the GradleX team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gradlex.jvm.dependency.conflict.resolution.rules;

import org.gradle.api.Action;
import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.VariantMetadata;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.attributes.Category;
import org.gradle.api.attributes.LibraryElements;
import org.gradle.api.attributes.Usage;
import org.gradle.internal.component.external.model.ModuleComponentResolveMetadata;
import org.gradle.internal.component.model.VariantResolveMetadata;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;

final class VariantSelection {

static class MavenVariant {
String name;
Map<String, String> attributes = new HashMap<>();

MavenVariant(String name) {
this.name = name;
}

MavenVariant attribute(String name, String value) {
attributes.put(name, value);
return this;
}
}

static final MavenVariant MAVEN_RUNTIME_VARIANT = new MavenVariant("runtime")
.attribute(Usage.USAGE_ATTRIBUTE.getName(), Usage.JAVA_RUNTIME)
.attribute(Category.CATEGORY_ATTRIBUTE.getName(), Category.LIBRARY)
.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE.getName(), LibraryElements.JAR);
static final MavenVariant MAVEN_COMPILE_VARIANT = new MavenVariant("compile")
.attribute(Usage.USAGE_ATTRIBUTE.getName(), Usage.JAVA_API)
.attribute(Category.CATEGORY_ATTRIBUTE.getName(), Category.LIBRARY)
.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE.getName(), LibraryElements.JAR);


private VariantSelection() { }

static void allVariantsMatching(ComponentMetadataContext context, Predicate<VariantIdentification> id, Action<? super VariantMetadata> action) {
List<String> variantNames = discoverNames(context, id);
variantNames.forEach(variantName -> context.getDetails().withVariant(variantName, action));
}

private static List<String> discoverNames(ComponentMetadataContext context, Predicate<VariantIdentification> id) {
ModuleComponentResolveMetadata metadata = extractMetadataFromContext(context);
List<VariantResolveMetadata> variants = getVariants(metadata);

if (variants.isEmpty()) {
return Stream.of(MAVEN_RUNTIME_VARIANT, MAVEN_COMPILE_VARIANT)
.filter(v -> id.test(new VariantIdentification(v.attributes)))
.map(v -> v.name)
.collect(Collectors.toList());
} else {
return variants.stream()
.filter(v -> id.test(new VariantIdentification(toMap(getAttributes(v)))))
.map(VariantResolveMetadata::getName)
.collect(Collectors.toList());
}
}

private static Map<String, String> toMap(AttributeContainer attributes) {
return attributes.keySet().stream().collect(Collectors
.toMap(Attribute::getName, k -> requireNonNull(attributes.getAttribute(k)).toString()));
}

private static AttributeContainer getAttributes(VariantResolveMetadata metadata) {
try {
Method getVariants = VariantResolveMetadata.class.getDeclaredMethod("getAttributes");
return (AttributeContainer) getVariants.invoke(metadata);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

private static List<VariantResolveMetadata> getVariants(ModuleComponentResolveMetadata metadata) {
try {
Method getVariants = ModuleComponentResolveMetadata.class.getDeclaredMethod("getVariants");
@SuppressWarnings("unchecked")
List<VariantResolveMetadata> variants = (List<VariantResolveMetadata>) getVariants.invoke(metadata);
return variants;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

private static ModuleComponentResolveMetadata extractMetadataFromContext(ComponentMetadataContext context) {
try {
Field metadataField = context.getClass().getDeclaredField("metadata");
metadataField.setAccessible(true);
return (ModuleComponentResolveMetadata) metadataField.get(context);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
Loading
Loading