How to get the project Extensions in gradle tooling? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to get the project Extensions in gradle tooling? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m using gradle tooling and using projectConnection I’m able to get connected to gradle from java. And by using the ModelBuilder I’m able to get the project object also in my java class.

ModelBuilder gradleBuilder = connection.model(GradleProject.class); 

But this project object has accessibility to Tasks only. (i.e) project.getTasks() is available. But not project.getExtensions(). How to access these extensions?? Is there any other way to access the gradle Extensions from Java?

Edit after your response :

When I run a gradle task it will read the closures in the given file and update the values read in a static java class (deployGradleReaderExtension.java). Now I’m trying to read the value from extensions by

deployGradleReaderExtension ext = (deployGradleReaderExtension) buildResult.getGradle().getRootProject().getExtensions().getByName("serviceConf");

Now I’m getting the casting exception. Because gradle and java are using diff ClassLoaders.

Is there any way to overcome this?

Answer

It’s possible to access extensions from, say, a plugin written in Java, but not from the tooling API. To achieve strong forward and backwards compatibility, the tooling API only offers a simplified view on the Gradle object model, designed to meet the needs of integrators like IDEs.

We are here to answer your question about How to get the project Extensions in gradle tooling? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji