# Creating sbprojects dynamically without having a settings.gradle

**URL:** https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459
**Category:** Old Forum Archive
**Created:** [June 17, 2013, 8:34pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459 "2013-06-17T20:34:00Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![mrampson](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mrampson](https://discuss.gradle.org/u/mrampson)
#### Post date: [June 17, 2013, 8:34pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/1 "2013-06-17T20:34:00Z")

</div>

How do you dynamically add subprojects to a project based off a map that is in the build.gradle. I can create all of the projects on the fly, but it looks like I need a settings.gradle to do the include of all of them. Can’t you do this programmatically?

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [June 17, 2013, 10:58pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/2 "2013-06-17T22:58:00Z")

</div>

‘settings.gradle’ gets evaluated before ‘build’.gradle. You can declare the map in ‘settings.gradle’ and access it from ‘build.gradle’ (if you associate it with the ‘gradle’ object), but not the other way around.

---

<div class="post-metadata">

### Author: ![mrampson](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mrampson](https://discuss.gradle.org/u/mrampson)
#### Post date: [June 18, 2013, 3:42pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/3 "2013-06-18T15:42:00Z")

</div>

It would be nice to be able to programmatically manipulate settings so that children projects can be dynamically defined. I have a build.gradle that creates the children projects based on a map of information in build.gradle so that you only have to define these things in one place. The map is actually the dependency string as the key and a url as a value for the healthcheck (this used to be in a yaml file). Seems like lately, people are creating more multiproject builds dynamically and this issue issue with the settings.gradle keeps coming up

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [June 18, 2013, 5:38pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/4 "2013-06-18T17:38:00Z")

</div>

What exactly is the issue? You can declare the necessary information in ‘settings.gradle’. It isn’t possible to do so in ‘build.gradle’ as the latter is evaluated in a later phase (configuration rather than initialization phase).

---

<div class="post-metadata">

### Author: ![Max\_Grigorev](https://avatars.discourse-cdn.com/v4/letter/m/b3f665/32.png) [@Max\_Grigorev](https://discuss.gradle.org/u/Max_Grigorev)
#### Post date: [June 20, 2013, 3:14pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/5 "2013-06-20T15:14:00Z")

</div>

The issue (mrampson will correct me if I’m wrong) is that enumerating the projects paths in settings.gradle is just duplication of information that’s available somewhere already. And duplication is bad.

For instance we, by convention, always have a build.gradle file in every subproject’s folder, even if it is to be empty (so far it never is).

I too am looking at the same thing as mrampson, but for now the only way I see is just generating settings.gradle with an external script, which, quite obviously, suboptimal.

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [June 20, 2013, 3:17pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/6 "2013-06-20T15:17:00Z")

</div>

Where is the information available? Why not leverage that information in settings.gradle?

---

<div class="post-metadata">

### Author: ![Max\_Grigorev](https://avatars.discourse-cdn.com/v4/letter/m/b3f665/32.png) [@Max\_Grigorev](https://discuss.gradle.org/u/Max_Grigorev)
#### Post date: [June 20, 2013, 3:40pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/7 "2013-06-20T15:40:00Z")

</div>

> Where is the information available?

Just like I described, we do have a build.gradle file in every subproject folder. Just traversing the directory tree and registering every folder with build.gradle (or even just any directory with matching sourceset) as a subproject would be sufficient. That’s exactly what I’m doing in my 5 line ruby script right now.

> Why not leverage that information in settings.gradle?

Because this is duplication of settings. Every time someone adds or removes a subproject, they need to remember to modify settings.gradle - while this could be quite easily automated.

Convention over configuration, right?

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [June 20, 2013, 3:58pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/8 "2013-06-20T15:58:00Z")

</div>

It isn’t necessary to duplicate any information. Instead you can provide the information right in ‘settings.gradle’, or script ‘settings.gradle’ to get the information from elsewhere (e.g. by scanning the file system). However, keep in mind that ‘settings.gradle’ will be evaluated for each Gradle invocation, and thus has to be fast to evaluate.

---

<div class="post-metadata">

### Author: ![Max\_Grigorev](https://avatars.discourse-cdn.com/v4/letter/m/b3f665/32.png) [@Max\_Grigorev](https://discuss.gradle.org/u/Max_Grigorev)
#### Post date: [June 20, 2013, 5:39pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/9 "2013-06-20T17:39:00Z")

</div>

> or script settings.gradle to get the information from elsewhere

Ah, got it, perfect.

Thanks!

---

<div class="post-metadata">

### Author: ![mrampson](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mrampson](https://discuss.gradle.org/u/mrampson)
#### Post date: [June 20, 2013, 9:21pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/10 "2013-06-20T21:21:00Z")

</div>

I suppose it is a matter of style, but I would rather just have a top level build.gradle and forego the settings.gradle by being able to create the subprojects on the fly and be able to manipulate the Settings.include programatically.

I’m having issues making the map available to build.gradle from settings.gradle. Is there a particular namespace that is needs to be in?

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [June 20, 2013, 9:55pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/11 "2013-06-20T21:55:00Z")

</div>

It’s not possible to forego the ‘settings.gradle’, as this information has to be provided in the initialization phase, not the configuration phase. It determines which build scripts will be evaluated in the first place.

Only the ‘gradle’ object is shared between settings script and build scripts. ‘gradle.ext.foo = “bar”’ and ‘println gradle.foo’ should work.

---

<div class="post-metadata">

### Author: ![Max\_Grigorev](https://avatars.discourse-cdn.com/v4/letter/m/b3f665/32.png) [@Max\_Grigorev](https://discuss.gradle.org/u/Max_Grigorev)
#### Post date: [June 21, 2013, 11:09am UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/12 "2013-06-21T11:09:00Z")

</div>

This is what I managed to build. Works like a charm.

import static groovy.io.FileVisitResult.\*

import static groovy.io.FileType.\*

def skipDirs = ~/^(build|…\*|src|out)/

def preDir = {

if (skipDirs.matcher([it.name](http://it.name)).matches())

return SKIP\_SUBTREE

}

def getProjectName(String dir) {

return dir.minus(rootDir.toString() + “/”).replaceAll("/", “:”)

}

rootDir.traverse(

type: DIRECTORIES,

preDir: preDir) { dir-\>

def dstr = dir.toString()

if ((!skipDirs.matcher([dir.name](http://dir.name))) &&

new File(dstr + “/build.gradle”).exists()) {

include getProjectName(dstr)

}

}

Thanks for your help Peter. I think this might be closed as answered.

---

<div class="post-metadata">

### Author: ![mrampson](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mrampson](https://discuss.gradle.org/u/mrampson)
#### Post date: [June 21, 2013, 5:15pm UTC](https://discuss.gradle.org/t/creating-sbprojects-dynamically-without-having-a-settings-gradle/7459/13 "2013-06-21T17:15:00Z")

</div>

Thanks Peter. I was missing the ext part. I have this working now and it works fine.
