Skip to content

Commit 3b2eda8

Browse files
committed
NebulaResolutionRulesExtension: allow to configure include,exclude and optional through project properties
1 parent 3f80c51 commit 3b2eda8

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

src/integTest/groovy/nebula/plugin/resolutionrules/ResolutionRulesPluginSpec.groovy

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,28 @@ class ResolutionRulesPluginSpec extends AbstractIntegrationTestKitSpec {
330330
result.output.contains '\\--- org.slf4j:slf4j-api:1.7.21\n'
331331
}
332332

333+
def 'optional rules are applied when specified through project properties'() {
334+
given:
335+
buildFile << """
336+
dependencies {
337+
resolutionRules files("$optionalRulesJsonFile")
338+
339+
implementation 'log4j:log4j:1.2.17'
340+
implementation 'org.slf4j:jcl-over-slf4j:1.7.0'
341+
}
342+
""".stripIndent()
343+
344+
345+
when:
346+
def result = runTasks('dependencies', '--configuration', 'compileClasspath', "-PnebulaResolutionRules.optional=${moduleName}")
347+
348+
then:
349+
result.output.contains '+--- log4j:log4j:1.2.17 -> org.slf4j:log4j-over-slf4j:1.7.21\n'
350+
result.output.contains '| \\--- org.slf4j:slf4j-api:1.7.21\n'
351+
result.output.contains '\\--- org.slf4j:jcl-over-slf4j:1.7.0 -> 1.7.21\n'
352+
result.output.contains '\\--- org.slf4j:slf4j-api:1.7.21\n'
353+
}
354+
333355
def 'only included rules are applied'() {
334356
given:
335357
def otherRulesFile = new File(projectDir, "other-${moduleName}.json")
@@ -369,6 +391,41 @@ class ResolutionRulesPluginSpec extends AbstractIntegrationTestKitSpec {
369391
!result.output.contains('asm:asm:3.3.1 -> org.ow2.asm:asm:5.0.4')
370392
}
371393

394+
def 'only included rules are applied via project properties'() {
395+
given:
396+
def otherRulesFile = new File(projectDir, "other-${moduleName}.json")
397+
otherRulesFile << """
398+
{
399+
"substitute" : [
400+
{
401+
"module" : "log4j:log4j",
402+
"with" : "org.slf4j:log4j-over-slf4j:1.7.21",
403+
"reason" : "SLF4J bridge replacement",
404+
"author" : "Danny Thomas <dmthomas@gmail.com>",
405+
"date" : "2015-10-07T20:21:20.368Z"
406+
}
407+
]
408+
}
409+
"""
410+
buildFile << """
411+
dependencies {
412+
resolutionRules files("$optionalRulesJsonFile", "$otherRulesFile")
413+
414+
implementation 'log4j:log4j:1.2.17'
415+
implementation 'asm:asm:3.3.1'
416+
implementation 'org.ow2.asm:asm:5.0.4'
417+
}
418+
""".stripIndent()
419+
420+
421+
when:
422+
def result = runTasks('dependencies', '--configuration', 'compileClasspath', "-PnebulaResolutionRules.include=other-${moduleName}")
423+
424+
then:
425+
result.output.contains('log4j:log4j:1.2.17 -> org.slf4j:log4j-over-slf4j:1.7.21')
426+
!result.output.contains('asm:asm:3.3.1 -> org.ow2.asm:asm:5.0.4')
427+
}
428+
372429
@Unroll
373430
def "deny dependency"() {
374431
given:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package nebula.plugin.resolutionrules
22

3+
import org.gradle.api.Project
4+
35
inline fun <T, R> Collection<T>.mapToSet(transform: (T) -> R): Set<R> {
46
return mapTo(LinkedHashSet<R>(size), transform)
57
}
8+
9+
fun Project.findStringProperty(name: String): String? = if (hasProperty(name)) property(name) as String? else null
10+
11+
fun parseRuleNames(ruleNames: String): Set<String> =
12+
ruleNames.split(",").map { it.trim() }.filter { it.isNotEmpty()} .toSet()

src/main/kotlin/nebula/plugin/resolutionrules/plugin.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,17 @@ class ResolutionRulesPlugin : Plugin<Project> {
6868
const val BOOT_ARCHIVES_CONFIGURATION_NAME = "bootArchives"
6969
const val ARCHIVES_CONFIGURATION_NAME = "archives"
7070
const val OPTIONAL_PREFIX = "optional-"
71+
const val OPTIONAL_RULES_PROJECT_PROPERTY = "nebulaResolutionRules.optional"
72+
const val INCLUDE_RULES_PROJECT_PROPERTY = "nebulaResolutionRules.include"
73+
const val EXCLUDE_RULES_PROJECT_PROPERTY = "nebulaResolutionRules.exclude"
7174
}
7275

7376
override fun apply(project: Project) {
7477
this.project = project
7578
configurations = project.configurations
7679
extension =
7780
project.extensions.create("nebulaResolutionRules", NebulaResolutionRulesExtension::class.java, project)
78-
81+
addRulesFromProjectProperties(project, extension)
7982
val rootProject = project.rootProject
8083
val configuration = project.configurations.maybeCreate(RESOLUTION_RULES_CONFIG_NAME)
8184
if (project != rootProject) {
@@ -126,6 +129,22 @@ class ResolutionRulesPlugin : Plugin<Project> {
126129
}
127130
}
128131

132+
/**
133+
* Search for optional, include and exclude rules in project properties
134+
* Add them to the extension if found
135+
*/
136+
private fun addRulesFromProjectProperties(
137+
project: Project,
138+
extension: NebulaResolutionRulesExtension
139+
) {
140+
val optionalRules = project.findStringProperty(OPTIONAL_RULES_PROJECT_PROPERTY)
141+
optionalRules?.let { rules -> parseRuleNames(rules).forEach { extension.optional.add(it) } }
142+
val includeRules = project.findStringProperty(INCLUDE_RULES_PROJECT_PROPERTY)
143+
includeRules?.let { rules -> parseRuleNames(rules).forEach { extension.include.add(it) } }
144+
val excludeRules = project.findStringProperty(EXCLUDE_RULES_PROJECT_PROPERTY)
145+
excludeRules?.let { rules -> parseRuleNames(rules).forEach { extension.exclude.add(it) } }
146+
}
147+
129148
}
130149

131150
abstract class NebulaResolutionRulesService : BuildService<NebulaResolutionRulesService.Params> {

0 commit comments

Comments
 (0)