0

I'm using the Gradle connector with a Gradle project that is downloaded from a service. I want to do some Gradle operations on that project but need some information from the project.

The downloaded project's build.gradle has some properties that I would like to extract :

group = "value0"
archivesBaseName = "value1"
version = "value2"

If I use

ProjectConnection.getModel(GradleProject.class)

I can get some values from the model but not those ones that I want (perhaps I am using it wrong?). Is there a way to extract those specific values out of the project (perhaps a different model)? I can also just do some text parsing on the build file, but I'd like that to be my last option.

loosebazooka
  • 2,363
  • 1
  • 21
  • 32

1 Answers1

0

The Gradle tooling API only exposes a subset of the build script information, using its own models. As far as I can tell, the properties that you are interested in are not exposed by default. However, you can expose your own custom model. For an example, see samples/toolingApi/customModel in the full Gradle distribution.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Would I be able to register the Model on the fly with a Project? From what I understand, Models need to have their ToolingModelBuilder registered with the ToolingModelBuilderRegistry at some point (usually in a plugin's apply implementation). I don't have control over the service generating the code, so I'd need to do this stuff against whatever is generated. I need this mainly to implement an AbstractArchiveTask so I can create an artifact from this generated code that depends on this task creating an archive. – loosebazooka Apr 28 '14 at 21:16
  • Not sure what you mean by "register the Model on the fly". You do have to register the model as you described. – Peter Niederwieser Apr 28 '14 at 21:19
  • I mean without explicitly registering it in the context of some plugin. I guess I can't though. My situation is a little strange. The plugin I have downloads another project and runs tasks on it using a GradleConnector, I'm trying to extract information from the project. – loosebazooka Apr 28 '14 at 22:05
  • A plugin is essentially the same as a build script, except that with the latter, you'll have to use the internal `getServices().get(ServiceType)` to get at a service. You might be able to register the model by injecting an init script, but I'm not sure. – Peter Niederwieser Apr 28 '14 at 23:03
  • As Peter wrote: register it in the init script. This is what IDEs do to inject their `ToolingModelBuilder`s. – Radim Apr 29 '14 at 06:05