Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find tasks of a certain type through all projects?

Tags:

java

gradle

I have a multi-build Java project being built with Gradle.

To hide Illegal reflective access warnings I want to find JavaExec and Test tasks within several projects and provide the required JVM arguments. Rather than add these to each project, I want to apply this to all projects in the root build.gradle.

How do I find certain tasks of a certain type throughout all projects?

My intial attempt uses withType however I want to remove the duplication of repeating this for the JavaExec and Test task types. See below:

Attempt #1

allprojects {
  repositories {
    jcenter()
  }

  afterEvaluate {
    project.tasks.withType(JavaExec) {
       it.jvmArgs "--add-opens=java.base/java.lang=ALL-UNNAMED"
    }

    project.tasks.withType(Test) {
        it.jvmArgs "--add-opens=java.base/java.lang=ALL-UNNAMED"
    }
  }
}

subprojects {
  version = "0.0.1-SNAPSHOT"
}
like image 643
binarycreations Avatar asked Sep 20 '25 21:09

binarycreations


1 Answers

Try this :

afterEvaluate {
    [JavaExec, Test].each {
        project.tasks.withType(it) {
            it.jvmArgs "--add-opens=java.base/java.lang=ALL-UNNAMED"
        }
    }
}
like image 176
ToYonos Avatar answered Sep 22 '25 12:09

ToYonos