Skip to content

Commit

Permalink
Add support for exclude rules
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielThomas committed Apr 28, 2016
1 parent 2b46116 commit 953422e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ This rule has different options depending on your use case. `includes` and `excl
}
```

## Exclude

Exclude rules excludes a dependency completely, similar to deny, but does not support a version and silently removes the dependency, rather than causing an error.

# Consuming rules

Dependency rules are read from the `resolutionRules` configuration. Zip and jar archives are supported, as are flat JSON files. JSON files within archives can at any directory level, and more than one json file can be provided.
Expand Down Expand Up @@ -283,6 +287,14 @@ Prefix the rule filename with `optional-` to make a rules filename optional, and
"author": "Example Person <person@example.org>",
"date": "2015-10-08T20:15:14.321Z"
}
],
"exclude": [
{
"module": "io.netty:netty-all",
"reason": "Bundle dependencies are harmful, they do not conflict resolve with the non-bundle dependencies",
"author" : "Danny Thomas <dmthomas@gmail.com>",
"date" : "2015-10-07T20:21:20.368Z"
}
]
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ class PluginFunctionalTest extends IntegrationSpec {
"date" : "2015-10-07T20:21:20.368Z"
}
],
"align": []
"exclude": [
{
"module": "io.netty:netty-all",
"reason": "Bundle dependencies are harmful, they do not conflict resolve",
"author" : "Danny Thomas <dmthomas@gmail.com>",
"date" : "2015-10-07T20:21:20.368Z"
}
]
}
""".stripIndent()

Expand Down Expand Up @@ -223,6 +230,24 @@ class PluginFunctionalTest extends IntegrationSpec {
result.standardOutput.contains('bouncycastle:bcprov-jdk15:140 -> org.bouncycastle:bcprov-jdk15:latest.release')
}

def 'exclude dependency'() {
given:
buildFile << """
dependencies {
compile 'io.netty:netty-all:5.0.0.Alpha2'
}
""".stripIndent()

when:
def result = runTasksSuccessfully('dependencies', '--configuration', 'compile')

then:
result.standardOutput.contains("""\
compile - Dependencies for source set 'main'.
No dependencies
""")
}

def 'optional rules are not applied by default'() {
given:
buildFile << """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package nebula.plugin.resolutionrules

import com.google.common.io.Files
import groovy.json.JsonSlurper
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -40,7 +39,7 @@ class ResolutionRulesPlugin implements Plugin<Project> {

project.gradle.projectsEvaluated {
Rules rules = rulesFromConfiguration(configuration, extension)
project.configurations.all ( { Configuration config ->
project.configurations.all({ Configuration config ->
if (config.name == configurationName) {
return
}
Expand All @@ -59,7 +58,7 @@ class ResolutionRulesPlugin implements Plugin<Project> {
rule.apply(project, rs, config, extension)
}
}
} )
})
rules.projectRules().each { ProjectRule rule -> rule.apply(project) }
}
}
Expand Down Expand Up @@ -109,25 +108,22 @@ class ResolutionRulesPlugin implements Plugin<Project> {
return false
}

static Rules parseJsonFile(File file) {
private static Rules parseJsonFile(File file) {
rulesFromJson(new JsonSlurper().parse(file) as Map)
}

static Rules parseJsonText(String json) {
rulesFromJson(new JsonSlurper().parseText(json) as Map)
}

static Rules parseJsonStream(InputStream stream) {
private static Rules parseJsonStream(InputStream stream) {
rulesFromJson(new JsonSlurper().parse(stream) as Map)
}

static Rules rulesFromJson(Map json) {
private static Rules rulesFromJson(Map json) {
Rules rules = new Rules()
rules.replace = json.replace.collect { new ReplaceRule(it) }
rules.substitute = json.substitute.collect { new SubstituteRule(it) }
rules.reject = json.reject.collect { new RejectRule(it) }
rules.deny = json.deny.collect { new DenyRule(it) }
rules.align = json.align.collect { new AlignRule(it) }
rules.exclude = json.exclude.collect { new ExcludeRule(it) }

rules
}
Expand All @@ -138,6 +134,8 @@ class ResolutionRulesPlugin implements Plugin<Project> {
List<RejectRule> reject = rules.collectMany { it.reject }.flatten() as List<RejectRule>
List<DenyRule> deny = rules.collectMany { it.deny }.flatten() as List<DenyRule>
List<AlignRule> align = rules.collectMany { it.align }.flatten() as List<AlignRule>
return new Rules(replace: replace, substitute: substitute, reject: reject, deny: deny, align: align)
List<ExcludeRule> exclude = rules.collectMany { it.exclude }.flatten() as List<ExcludeRule>

new Rules(replace: replace, substitute: substitute, reject: reject, deny: deny, align: align, exclude: exclude)
}
}
17 changes: 16 additions & 1 deletion src/main/groovy/nebula/plugin/resolutionrules/Rules.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ public class Rules {
List<RejectRule> reject
List<DenyRule> deny
List<AlignRule> align
List<ExcludeRule> exclude

public List<ProjectRule> projectRules() {
return replace
}

public List<ConfigurationRule> configurationRules() {
return deny
return [deny, exclude].flatten()
}

public List<ResolutionRule> resolutionRules() {
Expand Down Expand Up @@ -247,6 +248,20 @@ class AlignRules implements ProjectConfigurationRule {
}
}

class ExcludeRule extends BaseRule implements ConfigurationRule {
ModuleIdentifier moduleId

ExcludeRule(Map map) {
super(map)
moduleId = ModuleIdentifier.valueOf(map.module as String)
}

@Override
public void apply(Configuration configuration) {
configuration.exclude(group: moduleId.organization, module: moduleId.name)
}
}

public class DependencyDeniedException extends Exception {
public DependencyDeniedException(ModuleVersionIdentifier moduleId, DenyRule rule) {
super("Dependency ${moduleId} denied by dependency rule: ${rule.reason}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class RulesTest extends Specification {
"substitute": [],
"reject": [],
"deny": [],
"align": []
"align": [],
"exclude": []
}"""


Expand Down

0 comments on commit 953422e

Please sign in to comment.