Dim
Dim

Reputation: 4807

How to know which project is running in gradle

I have multiple projects and one trunk...

Some times I need to run via cmd:

gradlew Proj1:assembleRelease

And other times:

gradlew Proj2:assembleRelease

How can I in gradle file know what is building Proj1 or Proj2?

Upvotes: 3

Views: 111

Answers (1)

wakjah
wakjah

Reputation: 4551

You can access this information via the gradle property on any instance of Project. From the docs:

getGradle()
Returns the Gradle invocation which this project belongs to.

The instance of Gradle provides the startParameter property, which contains the information you want. For example, in the root scope:

print project.gradle.startParameter

For my particular invocation, I could print the name of the first task request via

print project.gradle.startParameter.taskRequests[0].args[0]

However, the above is probably quite fragile and will surely fail in a number of important cases.

There are also many other things you may find useful in the Gradle instance, if the above doesn't do exactly what you want, such as the task execution graph and root project for this build. Note that all the above information is available by following links from the documentation for the Project class, which I linked above. I strongly recommend that you peruse this documentation.

On the other hand, why do you want to do this? Usually you would just add configuration to the individual projects themselves and let the task runner take care of running your specific implementations, rather than saying if task1 is running then x else y

Upvotes: 1

Related Questions