# Custom plugin to enforce major compatibility

**URL:** <https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419>\
**Category:** Help/Discuss\
**Created:** [March 25, 2021, 9:21am UTC](https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419 "2021-03-25T09:21:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fransguelinckx](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/fransguelinckx/32/9165_2.png) [@fransguelinckx](https://discuss.gradle.org/u/fransguelinckx)\
**Post date:** [March 25, 2021, 9:21am UTC](https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419/1 "2021-03-25T09:21:42Z")

</div>

Hi,

Im trying to write a plugin that does the same as `failOnVersionConflict()`, but only when the major version differs. To achieve this I am looking for a way to obtain the information that `gradle dependencies` gives us, for example: `org.junit.jupiter:junit-jupiter-api:5.4.2 -> 5.7.1`. Especially the fact that 5.7.1 was selected over 5.4.2. Do you have any pointers on how to obtain this information?

Kr  
Frans

---

<div class="post-metadata">

**Author:** ![alvesfc](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/alvesfc/32/9163_2.png) [@alvesfc](https://discuss.gradle.org/u/alvesfc)\
**Post date:** [March 25, 2021, 1:46pm UTC](https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419/2 "2021-03-25T13:46:53Z")

</div>

Hi, You can obtain this information using the command below.

```
tasks.create("example"){
  project.configurations.getByName("implementation").dependencies.forEach {
    println("${it.group} - ${it.name} - ${it.version}")
  }
}
```

---

<div class="post-metadata">

**Author:** ![fransguelinckx](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/fransguelinckx/32/9165_2.png) [@fransguelinckx](https://discuss.gradle.org/u/fransguelinckx)\
**Post date:** [March 25, 2021, 3:30pm UTC](https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419/3 "2021-03-25T15:30:04Z")

</div>

Thanks!  
I tried that, but it only gives me the dependencies I explicitly declared in my build.gradle. I also need all transitive dependencies and whether Gradle did some conflict resolution on them (and with which versions).

---

<div class="post-metadata">

**Author:** ![alvesfc](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/alvesfc/32/9163_2.png) [@alvesfc](https://discuss.gradle.org/u/alvesfc)\
**Post date:** [March 29, 2021, 1:40pm UTC](https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419/4 "2021-03-29T13:40:27Z")

</div>

I did a little more research and found this solution, but I still have doubts if this is the right strategy.

```
tasks.create("printAllDependencies"){

project.configurations
    .filter { it.isCanBeResolved }
    .forEach {
        println("Configuration : ${it.name}")
        it.resolvedConfiguration.firstLevelModuleDependencies.forEach { r ->
            println("Dependency : ${r.module.id.name} ${r.module.id.version}")
            r.children.forEach { m ->
                println(" - ${m.module.id.name} ${m.module.id.version}")
            }
        }
    }

```

}

---

<div class="post-metadata">

**Author:** ![Chris\_Dore](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/chris_dore/32/7592_2.png) [@Chris\_Dore](https://discuss.gradle.org/u/Chris_Dore)\
**Post date:** [March 31, 2021, 4:46am UTC](https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419/5 "2021-03-31T04:46:00Z")

</div>

Using rich version declarations/constraints ([Declaring Rich Versions](https://docs.gradle.org/current/userguide/rich_versions.html)) might also be something you’re interested in, but here’s some code that may help:

```
dependencies {
    implementation 'junit:junit:4.+'
    implementation 'org.hamcrest:hamcrest-core:0.1'
}
configurations.all { conf ->
    conf.incoming.afterResolve { resolvableDeps ->
        resolvableDeps.resolutionResult.allDependencies.findAll {
            it instanceof ResolvedDependencyResult && it.requested instanceof ModuleComponentSelector
        }.each {
            def req = it.requested.version
            def res = it.selected.moduleVersion.version
            // compare version strings and throw exception as needed
            println "${req} -> ${res}"
        }
    }
}

```

Results:

```
$ ./gradlew build
...
> Task :compileJava UP-TO-DATE
4.+ -> 4.13.2
1.3 -> 1.3
0.1 -> 1.3
...
```

---

<div class="post-metadata">

**Author:** ![Chris\_Dore](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/chris_dore/32/7592_2.png) [@Chris\_Dore](https://discuss.gradle.org/u/Chris_Dore)\
**Post date:** [March 31, 2021, 4:49am UTC](https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419/6 "2021-03-31T04:49:27Z")

</div>

Dependency/module substitutions might make things messy as well, so you might need to handle cases where the requested module is different than the selected. I didn’t test this out so I’m not sure how that impacts my code.

---

<div class="post-metadata">

**Author:** ![fransguelinckx](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/fransguelinckx/32/9165_2.png) [@fransguelinckx](https://discuss.gradle.org/u/fransguelinckx)\
**Post date:** [March 31, 2021, 6:07am UTC](https://discuss.gradle.org/t/custom-plugin-to-enforce-major-compatibility/39419/7 "2021-03-31T06:07:32Z")

</div>

Thanks Chris, that’s exactly what I was looking for. I will also keep the dependency/module substitutions in mind!
