Commit 050e9198 authored by Jamie Tanna's avatar Jamie Tanna

Merge branch 'article/gradle-list-all-dependencies' into 'master'

Blogument global tasks in Gradle, and `allDeps` globally

See merge request !813
parents d9d99f09 5877a6fb
---
title: "Configure Gradle to Configure Tasks Globally with an initscript"
description: "How to use Gradle's initialization scripts to globally configure tasks across all of your projects."
tags:
- blogumentation
- gradle
- java
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2020-07-27T13:14:09+0100
slug: "global-gradle-task"
image: https://media.jvt.me/44d58db1ce.png
---
As part of [_Configure Gradle to Allow Listing All Subproject Dependencies_]({{< ref 2020-07-27-gradle-list-all-dependencies >}}), I wanted to utilise [Gradle initscripts](https://docs.gradle.org/current/userguide/init_scripts.html) to provide a global task across all my projects.
Unfortunately our setup from [_Running Spotless Automagically with Gradle_]({{< ref 2020-05-15-gradle-spotless >}}) doesn't quite work, but I was able to adapt [the general set up from Marcin's article _My Gradle init script_](http://blog.proxerd.pl/article/my-gradle-init-script) which allows us to do this.
We can create a file i.e. `~/.gradle/init.d/hello.gradle`:
```groovy
projectsEvaluated {
rootProject.allprojects { project ->
if (!tasks.findByName('hello')) {
task hello() {
println('Hey from ' + project.name)
}
}
}
}
```
Which allows us to run `gradle hello` and it'll tell us which project it's executing in.
---
title: "Configure Gradle to Allow Listing All Subproject Dependencies"
description: "How to set Gradle configuration globally to add a task to list all your dependencies."
tags:
- blogumentation
- gradle
- java
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2020-07-27T13:14:09+0100
slug: "gradle-list-all-dependencies"
image: https://media.jvt.me/44d58db1ce.png
---
Every so often, I need to list the dependency tree for my Gradle projects, which doesn't work out-of-the-box when using subprojects. I [bookmarked](/mf2/2020/05/3clzz/) the great post [_Gradle tricks – display dependencies for all subprojects in multi-project build_](https://solidsoft.wordpress.com/2014/11/13/gradle-tricks-display-dependencies-for-all-subprojects-in-multi-project-build/) as I so regularly come back to it, as it solves the issue for us.
However, I wondered if there was a better way to do this, as I didn't want to commit this task into each project, but I also didn't have to keep locally adding it to each project, and then removing it before committing.
Fortunately, we can follow [_Configure Gradle to Configure Tasks Globally with an initscript_]({{< ref 2020-07-27-global-gradle-task >}}) and create a file i.e. `~/.gradle/init.d/allDeps.gradle`:
```groovy
projectsEvaluated {
rootProject.allprojects {
if (!tasks.findByName('allDeps')) {
task allDeps(type: DependencyReportTask) {}
}
}
}
```
This will then allow you to run `gradle allDeps` in any of your projects and get your full dependency tree, only if if that task isn't already defined (so we don't overwrite something useful).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment