Skip to content

Commit

Permalink
Ensure all version catalog dependencies are copied
Browse files Browse the repository at this point in the history
Follow up for #23096 to ensure all properties are properly copied
  • Loading branch information
jvandort committed Dec 23, 2022
1 parent ea3081d commit 57e730d
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 67 deletions.
Expand Up @@ -163,6 +163,7 @@ protected void copyTo(AbstractModuleDependency target) {
if (moduleDependencyCapabilities != null) {
target.moduleDependencyCapabilities = moduleDependencyCapabilities.copy();
}
target.endorsing = endorsing;
}

protected boolean isKeyEquals(ModuleDependency dependencyRhs) {
Expand Down
Expand Up @@ -506,53 +506,4 @@ task checkArtifacts {
result.assertTasksExecutedInOrder(":a:checkArtifacts", ":a:jar", ":b:checkArtifacts")
}

def 'artifacts are copied when declaring dependency on existing version catalog dependency with artifact'() {
given:
buildFile << """
configurations {
implementation
destination1
destination2
}
dependencies {
implementation(libs.test) {
artifact {
classifier = "alternative"
}
}
}
task copyAndPrintDependencies {
configurations.implementation.dependencies.each {
project.dependencies.add("destination1", it)
configurations.destination2.dependencies.add(it)
}
doLast {
configurations.implementation.dependencies.each {
println("implementation " + it + " " + it.artifacts*.classifier)
}
configurations.destination1.dependencies.each {
println("destination1 " + it + " " + it.artifacts*.classifier)
}
configurations.destination2.dependencies.each {
println("destination2 " + it + " " + it.artifacts*.classifier)
}
}
}
"""
file("gradle/libs.versions.toml") << """[libraries]
test = { module = 'org:test', version = '1.0' }
"""

when:
succeeds "copyAndPrintDependencies"

then:
outputContains("""implementation org:test:1.0 [alternative]
destination1 org:test:1.0 [alternative]
destination2 org:test:1.0 [alternative]""")
}

}
Expand Up @@ -2306,4 +2306,130 @@ Second: 1.1"""
expect:
succeeds ':help'
}
@Issue("https://github.com/gradle/gradle/issues/23096")
def 'all properties of version catalog dependencies are copied when the dependency is copied'() {
given:
buildFile << """
configurations {
implementation
destination1
destination2
}

dependencies {
implementation(libs.test) {
because("reason1")

exclude(group: "test-group", module: "test-module")
artifact {
name = "test-name"
classifier = "test-classifier"
extension = "test-ext"
type = "test-type"
url = "test-url"
}
transitive = true
endorseStrictVersions()

version {
branch = "branch"
strictly("123")
prefer("789")
reject("aaa")
}

changing = true
}
implementation(libs.test) {
transitive = false
targetConfiguration = "abc"
doNotEndorseStrictVersions()

version {
require("456")
}

changing = false
}
implementation(libs.test) {
attributes {
attribute(Attribute.of('foo', String), 'bar')
}
capabilities {
requireCapability("org:test-cap:1.1")
}
}
}

def printDep(dep) {
// Dependency
println("Dependency: " + dep.group + " " + dep.name + " " + dep.version + " " + dep.reason)

// ModuleDependency
dep.excludeRules.each { println("Exclude rule: " + it.group + " " + it.module) }
dep.artifacts.each { println("Artifact: " + it.name + " " + it.type + " " + it.extension + " " + it.classifier + " " + it.url) }
println("ModuleDependency: " + dep.transitive + " " + dep.targetConfiguration + " " + dep.attributes + " " + dep.requestedCapabilities + " " + dep.isEndorsingStrictVersions())

// ExternalDependency + ExternalModuleDependency
println("External: " + dep.versionConstraint + " " + dep.isChanging())
}

task copyAndPrintDependencies {
configurations.implementation.dependencies.each {
project.dependencies.add("destination1", it)
configurations.destination2.dependencies.add(it)
}

doLast {
println("implementation:")
configurations.implementation.dependencies.each {
printDep(it)
}

println()
println("destination1:")
configurations.destination1.dependencies.each {
printDep(it)
}

println()
println("destination2:")
configurations.destination2.dependencies.each {
printDep(it)
}

println()
println("copy:")
configurations.implementation.copy().dependencies.each {
printDep(it)
}
}
}
"""
file("gradle/libs.versions.toml") << """[libraries]
test = { module = 'org:test', version = '1.0' }
"""
when:
succeeds "copyAndPrintDependencies"
then:
String expectedContent = """Dependency: org test 123 reason1
Exclude rule: test-group test-module
Artifact: test-name test-type test-ext test-classifier test-url
ModuleDependency: true null {} [] true
External: {strictly 123; prefer 789; reject aaa; branch branch} true
Dependency: org test 456 null
ModuleDependency: false abc {} [] false
External: 456 false
Dependency: org test 1.0 null
ModuleDependency: true null {foo=bar} [capability group='org', name='test-cap', version='1.1'] false
External: 1.0 false"""
outputContains("implementation:\n" + expectedContent)
outputContains("destination1:\n" + expectedContent)
outputContains("destination2:\n" + expectedContent)
outputContains("copy:\n" + expectedContent)
}
}
Expand Up @@ -45,8 +45,8 @@ public AbstractExternalModuleDependency(ModuleIdentifier module, String version,
this.versionConstraint = new DefaultMutableVersionConstraint(version);
}

public AbstractExternalModuleDependency(ModuleIdentifier module, MutableVersionConstraint version) {
super(null);
public AbstractExternalModuleDependency(ModuleIdentifier module, MutableVersionConstraint version, @Nullable String configuration) {
super(configuration);
if (module == null) {
throw new InvalidUserDataException("Module must not be null!");
}
Expand Down
Expand Up @@ -21,18 +21,20 @@
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.MutableVersionConstraint;

import javax.annotation.Nullable;

public class DefaultExternalModuleDependency extends AbstractExternalModuleDependency implements ExternalModuleDependency {

public DefaultExternalModuleDependency(String group, String name, String version) {
this(group, name, version, null);
}

public DefaultExternalModuleDependency(String group, String name, String version, String configuration) {
public DefaultExternalModuleDependency(String group, String name, String version, @Nullable String configuration) {
super(assertModuleId(group, name), version, configuration);
}

public DefaultExternalModuleDependency(ModuleIdentifier id, MutableVersionConstraint versionConstraint) {
super(id, versionConstraint);
public DefaultExternalModuleDependency(ModuleIdentifier id, MutableVersionConstraint versionConstraint, @Nullable String configuration) {
super(id, versionConstraint, configuration);
}

@Override
Expand Down
Expand Up @@ -22,7 +22,7 @@

public class DefaultMinimalDependency extends DefaultExternalModuleDependency implements MinimalExternalModuleDependencyInternal, Serializable {
public DefaultMinimalDependency(ModuleIdentifier module, MutableVersionConstraint versionConstraint) {
super(module, versionConstraint);
super(module, versionConstraint, null);
}

@Override
Expand All @@ -48,7 +48,7 @@ public void copyTo(AbstractExternalModuleDependency target) {
// Intentionally changes to the mutable version.
@Override
public DefaultMutableMinimalDependency copy() {
DefaultMutableMinimalDependency dependency = new DefaultMutableMinimalDependency(getModule(), new DefaultMutableVersionConstraint(getVersionConstraint()));
DefaultMutableMinimalDependency dependency = new DefaultMutableMinimalDependency(getModule(), new DefaultMutableVersionConstraint(getVersionConstraint()), getTargetConfiguration());
copyTo(dependency);
return dependency;
}
Expand Down
Expand Up @@ -39,7 +39,7 @@ public DefaultMinimalDependencyVariant(MinimalExternalModuleDependency delegate,
@Nullable String classifier,
@Nullable String artifactType
) {
super(delegate.getModule(), new DefaultMutableVersionConstraint(delegate.getVersionConstraint()));
super(delegate.getModule(), new DefaultMutableVersionConstraint(delegate.getVersionConstraint()), delegate.getTargetConfiguration());

attributesMutator = GUtil.elvis(attributesMutator, Actions.doNothing());
capabilitiesMutator = GUtil.elvis(capabilitiesMutator, Actions.doNothing());
Expand All @@ -64,12 +64,13 @@ public DefaultMinimalDependencyVariant(MinimalExternalModuleDependency delegate,
private DefaultMinimalDependencyVariant(
ModuleIdentifier id,
MutableVersionConstraint versionConstraint,
@Nullable String configuration,
Action<? super AttributeContainer> attributesMutator,
Action<? super ModuleDependencyCapabilitiesHandler> capabilitiesMutator,
@Nullable String classifier,
@Nullable String artifactType
) {
super(id, versionConstraint);
super(id, versionConstraint, configuration);
this.attributesMutator = attributesMutator;
this.capabilitiesMutator = capabilitiesMutator;
this.classifier = classifier;
Expand Down Expand Up @@ -97,7 +98,7 @@ public void copyTo(AbstractExternalModuleDependency target) {
@Override
public MinimalExternalModuleDependency copy() {
DefaultMinimalDependencyVariant dependency = new DefaultMinimalDependencyVariant(
getModule(), new DefaultMutableVersionConstraint(getVersionConstraint()),
getModule(), new DefaultMutableVersionConstraint(getVersionConstraint()), getTargetConfiguration(),
attributesMutator, capabilitiesMutator, classifier, artifactType
);
copyTo(dependency);
Expand Down
Expand Up @@ -18,16 +18,17 @@
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.MutableVersionConstraint;

import javax.annotation.Nullable;
import java.io.Serializable;

public class DefaultMutableMinimalDependency extends DefaultExternalModuleDependency implements MinimalExternalModuleDependencyInternal, Serializable {
public DefaultMutableMinimalDependency(ModuleIdentifier module, MutableVersionConstraint versionConstraint) {
super(module, versionConstraint);
public DefaultMutableMinimalDependency(ModuleIdentifier module, MutableVersionConstraint versionConstraint, @Nullable String configuration) {
super(module, versionConstraint, configuration);
}

@Override
public DefaultMutableMinimalDependency copy() {
DefaultMutableMinimalDependency dependency = new DefaultMutableMinimalDependency(getModule(), new DefaultMutableVersionConstraint(getVersionConstraint()));
DefaultMutableMinimalDependency dependency = new DefaultMutableMinimalDependency(getModule(), new DefaultMutableVersionConstraint(getVersionConstraint()), getTargetConfiguration());
copyTo(dependency);
return dependency;
}
Expand Down
Expand Up @@ -34,22 +34,23 @@ public class DefaultMutableVersionConstraint extends AbstractVersionConstraint i
private final List<String> rejectedVersions = Lists.newArrayListWithExpectedSize(1);

public DefaultMutableVersionConstraint(VersionConstraint versionConstraint) {
this(versionConstraint.getPreferredVersion(), versionConstraint.getRequiredVersion(), versionConstraint.getStrictVersion(), versionConstraint.getRejectedVersions());
this(versionConstraint.getPreferredVersion(), versionConstraint.getRequiredVersion(), versionConstraint.getStrictVersion(), versionConstraint.getRejectedVersions(), versionConstraint.getBranch());
}

public DefaultMutableVersionConstraint(String version) {
this(null, version, null);
}

private DefaultMutableVersionConstraint(@Nullable String preferredVersion, String requiredVersion, @Nullable String strictVersion) {
this(preferredVersion, requiredVersion, strictVersion, Collections.emptyList());
this(preferredVersion, requiredVersion, strictVersion, Collections.emptyList(), null);
}

private DefaultMutableVersionConstraint(@Nullable String preferredVersion, String requiredVersion, @Nullable String strictVersion, List<String> rejects) {
private DefaultMutableVersionConstraint(@Nullable String preferredVersion, String requiredVersion, @Nullable String strictVersion, List<String> rejects, @Nullable String branch) {
updateVersions(preferredVersion, requiredVersion, strictVersion);
for (String reject : rejects) {
this.rejectedVersions.add(nullToEmpty(reject));
}
this.branch = branch;
}

private void updateVersions(@Nullable String preferredVersion, @Nullable String requiredVersion, @Nullable String strictVersion) {
Expand Down
Expand Up @@ -140,7 +140,7 @@ public MinimalExternalDependencyNotationConverter(Instantiator instantiator) {

@Override
public void convert(MinimalExternalModuleDependency notation, NotationConvertResult<? super MinimalExternalModuleDependency> result) throws TypeConversionException {
DefaultMutableMinimalDependency moduleDependency = instantiator.newInstance(DefaultMutableMinimalDependency.class, notation.getModule(), notation.getVersionConstraint());
DefaultMutableMinimalDependency moduleDependency = instantiator.newInstance(DefaultMutableMinimalDependency.class, notation.getModule(), notation.getVersionConstraint(), notation.getTargetConfiguration());
MinimalExternalModuleDependencyInternal internal = (MinimalExternalModuleDependencyInternal) notation;
internal.copyTo(moduleDependency);
result.converted(moduleDependency);
Expand Down
Expand Up @@ -144,7 +144,7 @@ class DefaultImmutableVersionConstraintTest extends Specification {

def "can convert mutable version constraint to immutable version constraint"() {
given:
def v = new DefaultMutableVersionConstraint('1.0', '2.0', '3.0', ['1.1', '2.0'])
def v = new DefaultMutableVersionConstraint('1.0', '2.0', '3.0', ['1.1', '2.0'], null)

when:
def c = DefaultImmutableVersionConstraint.of(v)
Expand Down

0 comments on commit 57e730d

Please sign in to comment.