forked from abayer/gradle-hpi-plugin
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ResolvedDependencySelector to use resolved versions for gen…
…erated POM
- Loading branch information
Showing
3 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/groovy/org/jenkinsci/gradle/plugins/jpi/ResolvedDependencySelector.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.jenkinsci.gradle.plugins.jpi | ||
|
||
import groovy.transform.Memoized | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.Configuration | ||
import org.gradle.api.artifacts.ConfigurationContainer | ||
import org.gradle.api.artifacts.ModuleVersionIdentifier | ||
import org.gradle.api.artifacts.component.ModuleComponentSelector | ||
import org.gradle.api.artifacts.result.ResolvedDependencyResult | ||
|
||
class ResolvedDependencySelector { | ||
static ModuleVersionIdentifier selectedModuleVersion(Project project, String scope, String group, String name) { | ||
ResolvedDependencyResult result = lookupDependency(project, scope, group, name) | ||
|
||
Map<String, String> scoping = [compile: 'runtime', provided: 'compileOnly'] | ||
|
||
if (!result && scoping[scope]) { | ||
result = lookupDependency(project, scoping[scope], group, name) | ||
} | ||
|
||
result?.selected?.moduleVersion | ||
} | ||
|
||
private static ResolvedDependencyResult lookupDependency(Project project, String scope, String group, String name) { | ||
def resolvedDependencies = resolvedDependencyByConfiguration(project)[scope] | ||
def result = resolvedDependencies.find { r -> | ||
def requested = r.requested | ||
(requested instanceof ModuleComponentSelector) && | ||
(requested.group == group) && | ||
(requested.module == name) | ||
} | ||
result | ||
} | ||
|
||
@Memoized | ||
static Map<String, Set<? extends ResolvedDependencyResult>> resolvedDependencyByConfiguration(Project project) { | ||
ConfigurationContainer configurations = project.configurations | ||
Map<String, Set<? extends ResolvedDependencyResult>> dependencyMap = [:] | ||
dependencyMap['runtime'] = resolvedDependencies(configurations.runtimeClasspath) | ||
dependencyMap['compile'] = resolvedDependencies(configurations.compileClasspath) | ||
dependencyMap['compileOnly'] = resolvedDependencies(configurations.compileOnly) | ||
dependencyMap['test'] = resolvedDependencies(configurations.testRuntimeClasspath) | ||
dependencyMap | ||
} | ||
|
||
static Set<? extends ResolvedDependencyResult> resolvedDependencies(Configuration configuration) { | ||
configuration.incoming.resolutionResult.allDependencies.findAll { | ||
it instanceof ResolvedDependencyResult | ||
} as Set<? extends ResolvedDependencyResult> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters