Gradle project from scratch

Hey guys,

i want to build a gradle project without any existing file directory like IDE’s do when you create a new gradle project.
Is there a gradle class providing this feature and that lets me configure the build.gradle or init.gradle inside of java code?

I’ve tried it with the tooling api and looked into the sources of gradle to manually modify the init task but didn’t find a way to create the objects gradle uses internally.

Do i have to create the basic files with a standard FileWriter, parse all configurations i wanted and invoke the init task with the tooling api? Couldn’t find the sources of the IDE’s and how they manage the project creation.

Greetings,
Crashkurs

It sounds like the build init plugin would do what you describe. You can use it without a project from the command line, and presumably it should be possible to use it directly from the tooling API.

Hey Dimitar,

thanks for your reply! Unfortunately i already tried it and the tooling api does not provide all command line parameters like the gradle framework does. E.g. it does not know the “–type” parameter used to create a java library. I browsed deep into the gradle code to see where these parameters come from but couldn’t find a way to add those parameters.
You can only call init with the default build.gradle and settings.gradle, but you cant customize it. Maybe im doing something wrong with the init script? I pass the arguments “–init-script” “init.gradle” to the build but the content is not copied into the build.gradle as its just the default one.

The next step im trying is to use maven to create a java project and then convert it. Maybe that will work :wink:
I’ll see what maven offers for project generation.

Greetings,
Crashkurs

These are normal task options. Have a look at the source of org.gradle.api.internal.tasks.options.Option and its usage in org.gradle.buildinit.tasks.InitBuild.

On the other hand, looking at the tooling api, I didn’t see an obvious way to execute a task without a project (as the cmd line client does)…

You can point the tooling API at an empty directory and start executing tasks, for instance init. That’s how the “New Gradle Project” wizard in Buildship is implemented.

Good hint on how the wizards are implemented!
I already did this but the result is the default build.gradle in the empty directory. I want to customize this build.gradle with gradle objects and without writing to the file myself. Guess i have to write my own build init plugin to create build.gradle, apply it to the project and execute it?

That’s true, the init plugin is not yet extensible by third parties, you’ll have to roll you own.

Just found a kind of dirty way to create the project.

NativeServices.initialize(new File(“C:\Users\Username\.gradle”));
ProjectLayoutSetupRegistryFactory factory
= new ProjectLayoutSetupRegistryFactory(new DefaultMavenSettingsProvider(new DefaultMavenFileLocations()),
new DocumentationRegistry(),
new BaseDirFileResolver(NativeServices.getInstance().get(FileSystem.class),
new File(“C:\workspace”)));
ProjectLayoutSetupRegistry registry = factory.createProjectLayoutSetupRegistry();
registry.get(BuildInitTypeIds.JAVA_LIBRARY).generate();

This will access the gradle.build.template inside the gradle-build-init project and create a build.gradle using groovy.
You can use the factories code to create a JavaLibraryProjectInitDescriptor with your own template and fill groovy variables for the content of the build.gradle. However theres still need to create classes who manage the mapping from a project configuration to these variables - not sure if its just better to create a build.gradle parser myself.
I think theres room inside the factory to load services defining a ProjectInitDescriptor and provide some custom project generation

Greetings,
Crashkurs