Is it possible to obtain Gradle's module name programmatically in Java code?
I need it to access module's build files under the MODULE_NAME/build/intermediates
directory, but hardcoding module name is a bad idea.
To get the current's module name you can just access Gradle's project
object
project.name
Will return, for the module app
app
See: https://docs.gradle.org/current/userguide/writing_build_scripts.html
You can access project
variable inside your build script that is instance of Project. It has various getters like projectDir
or buildDir
and since this is quite common to use them it is documented in user guide.
Alternatively you can find this path if you look for an output of a task that generates files there.
If you need to know this location in your test you can pass it from build script using system property like this:
test {
systemProperty "buildFolder", project.buildDir.absolutePath
}
BTW: https://github.com/gradle/gradle/blob/master/gradle/idea.gradle#L192 has a trick how to update run configurations in IntelliJ's workspace with properties that you need for test execution. This can be useful if you run JUnit test inside IJ (without delegating to Gradle).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With