diff --git a/buildSrc/src/main/kotlin/io/spine/internal/gradle/report/pom/DependencyWriter.kt b/buildSrc/src/main/kotlin/io/spine/internal/gradle/report/pom/DependencyWriter.kt index fb1920b4..fe72bd06 100644 --- a/buildSrc/src/main/kotlin/io/spine/internal/gradle/report/pom/DependencyWriter.kt +++ b/buildSrc/src/main/kotlin/io/spine/internal/gradle/report/pom/DependencyWriter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023, TeamDev. All rights reserved. + * Copyright 2024, TeamDev. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import java.io.Writer import java.util.* import kotlin.reflect.full.isSubclassOf import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.Dependency import org.gradle.api.internal.artifacts.dependencies.AbstractExternalModuleDependency import org.gradle.kotlin.dsl.withGroovyBuilder @@ -126,13 +127,34 @@ private fun Project.depsFromAllConfigurations(): Set { } configuration.dependencies.filter { it.isExternal() } .forEach { dependency -> - val moduleDependency = ModuleDependency(project, configuration, dependency) + val forcedVersion = configuration.forcedVersionOf(dependency) + val moduleDependency = + if (forcedVersion != null) { + ModuleDependency(project, configuration, dependency, forcedVersion) + } else { + ModuleDependency(project, configuration, dependency) + } result.add(moduleDependency) } } return result } +/** + * Searches for a forced version of given [dependency] in this [Configuration]. + * + * Returns `null`, if it wasn't forced. + */ +private fun Configuration.forcedVersionOf(dependency: Dependency): String? { + val forcedModules = resolutionStrategy.forcedModules + val maybeForced = forcedModules.firstOrNull { + it.group == dependency.group + && it.name == dependency.name + && it.version != null + } + return maybeForced?.version +} + /** * Tells whether the dependency is an external module dependency. */ @@ -160,7 +182,7 @@ private fun Project.deduplicate(dependencies: Set): List - group.value.maxByOrNull { dep -> dep.version!! }!! + group.value.maxByOrNull { dep -> dep.version }!! } return filtered } @@ -177,9 +199,9 @@ private fun Project.logDuplicate(dependency: String, versions: List { companion object { - private val COMPARATOR = compareBy { it.module } + private val COMPARATOR = compareBy { it.project } .thenBy { it.configuration.name } .thenBy { it.group } .thenBy { it.name } - .thenBy { it.version } + .thenBy { it.factualVersion } } + override fun getVersion(): String = factualVersion + /** * A project dependency with its [scope][DependencyScope]. * * Doesn't contain any info about an origin module and configuration. */ - val scoped = ScopedDependency.of(dependency, configuration) + val scoped = ScopedDependency.of(this, configuration) /** * GAV coordinates of this dependency. * * Gradle's [Dependency] is a mutable object. Its properties can change their - * values with time. In parcticular, the version can be changed as more + * values with time. In particular, the version can be changed as more * configurations are getting resolved. This is why this property is calculated. */ val gav: String - get() = "$group:$name:$version" + get() = "$group:$name:$factualVersion" override fun compareTo(other: ModuleDependency): Int = COMPARATOR.compare(this, other) @@ -76,17 +79,17 @@ internal class ModuleDependency( other as ModuleDependency - if (module != other.module) return false + if (project != other.project) return false if (configuration != other.configuration) return false - if (dependency != other.dependency) return false + if (gav != other.gav) return false return true } override fun hashCode(): Int { - var result = module.hashCode() + var result = project.hashCode() result = 31 * result + configuration.hashCode() - result = 31 * result + dependency.hashCode() + result = 31 * result + gav.hashCode() return result } -} +} \ No newline at end of file