Apply dependency constraints from Java plugin

I am writing a plugin, in Java. I’d like to be able to enforce a number of dependency constraints by adding DependencyConstraint references from the plugin.

I have the following:

		final DependencyHandler dependencyHandler = project.getDependencies();

		dependencyHandler.constraints(
				constraintHandler -> {
					final DependencyConstraint dependencyConstraint = constraintHandler.create( "com.github.ben-manes.caffeine:caffeine:2.8.5" );
					dependencyConstraint.because( "test" );
					configuration.getDependencyConstraints().add( dependencyConstraint );
				}
		);

however, this always leads to the following even when trying to use a ResolutionStrategy instead:

   > Could not resolve com.github.ben-manes.caffeine:caffeine:2.6.2.
     Required by:
         project : > io.quarkus:quarkus-hibernate-orm:1.7.1.Final > org.hibernate:quarkus-local-cache:0.1.0
      > Cannot choose between the following variants of com.github.ben-manes.caffeine:caffeine:2.8.5:
          - runtimeElements
          - shadowRuntimeElements
        All of them match the consumer attributes:
          - Variant 'runtimeElements' capability com.github.ben-manes.caffeine:caffeine:2.8.5:
              - Unmatched attributes:
                  - Found org.gradle.category 'library' but wasn't required.
                  - Found org.gradle.dependency.bundling 'external' but wasn't required.
                  - Found org.gradle.jvm.version '8' but wasn't required.
                  - Found org.gradle.libraryelements 'jar' but wasn't required.
                  - Found org.gradle.status 'release' but wasn't required.
                  - Found org.gradle.usage 'java-runtime' but wasn't required.
          - Variant 'shadowRuntimeElements' capability com.github.ben-manes.caffeine:caffeine:2.8.5:
              - Unmatched attributes:
                  - Found org.gradle.category 'library' but wasn't required.
                  - Found org.gradle.dependency.bundling 'embedded' but wasn't required.
                  - Found org.gradle.jvm.version '8' but wasn't required.
                  - Found org.gradle.libraryelements 'jar' but wasn't required.
                  - Found org.gradle.status 'release' but wasn't required.
                  - Found org.gradle.usage 'java-runtime' but wasn't required.

What am I missing? How to apply this constraint?

1 Like

I bumped into almost the same problem. Strangely, it works fine for Caffeine 2.8.4, but I get the exact same problem you have for every version after that.

configurations {
	compileGitBookClasspath {
		// my latest attempt, doesn't work
		resolutionStrategy.dependencySubstitution {
			substitute variant(module("com.github.ben-manes.caffeine:caffeine:$VER_CAFFEINE")) {
				attributes {
					attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.EXTERNAL))
				}
			} using module("com.github.ben-manes.caffeine:caffeine:$VER_CAFFEINE")
		}
	}
}
dependencies {
	compileGitBookClasspath project(':projectWhichDependsOnCaffeine')
}