76

I have a couple of subprojects that are part of a multi-project build (flat hierarchy). I want to set the name on them to be different than their folder name. However, in include (settings.gradle) it has to have the folder name otherwise it won't find it (same for the compile project(':ProjectName')).

If I attempt to set project.name it tells me that is read-only. The reason is that we are converting from Ant and would like to keep the same name for Eclipse IDE. As far the artifacts go, we use jar.artifactName to set whatever name we want.

Thank you.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Greg Hill
  • 2,148
  • 2
  • 23
  • 27

2 Answers2

116

Project names can only be changed in settings.gradle. For example:

include "foo" // or `includeFlat`, doesn't matter

// always good to nail down the root project name, because
// the root directory name may be different in some envs (e.g. CI)
// hence the following even makes sense for single-project builds
rootProject.name = "bar" 

// change subproject name
project(":foo").name = "foofoo"

Alternatively, you can use the desired project name in the include statement, and later reconfigure the project directory:

include "foofoo"

project(":foofoo").projectDir = file("foo")

To give some background, the only difference between include and includeFlat is that they use different defaults for the project's projectDir. Otherwise they are the same.

For further information, check out Settings in the Gradle Build Language Reference.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • That works fine. Thank you very much. I have to say I am liking gradle more and more every day as I learn more about it. – Greg Hill Nov 25 '13 at 20:07
  • 5
    What version of Gradle does this work for? I'm using 1.12 and get "Project with path ':foo' could not be found in root project 'bar'." `project(':foo')` definitely exists when inside `build.gradle` and I also have `include "foo"` before `project(":foo")`. – Travis Gockel May 02 '14 at 21:42
  • It should work with pretty much any version. Where exactly do you get this error? What do you mean by "project(':foo') definitely exists when inside build.gradle"? – Peter Niederwieser May 02 '14 at 23:34
  • 1
    I'm running into the same issue as Travis on 1.12 – lessthanoptimal Jul 24 '14 at 16:35
  • 3
    Once you changed the project name to `foofoo`, it's `project(":foofoo")`. – Peter Niederwieser Jul 24 '14 at 16:55
  • 1
    I wish there was a way for it to continue to use the old `:projectOld:project2Old` but create a different module name for IntelliJ. this would work around the issue when you have multiple sub-projects with the same name minus a few characters – Nicholas DiPiazza Nov 01 '18 at 13:31
  • Does this still work? I'm using Gradle v7.6 and even after changing `.name`, I can access the subproject only by its original name. With the `.projectDir` method, I can't access the subproject at all. – Brian White Apr 03 '23 at 18:59
6

This is Kotlin DSL equivalent of Peter Niederwieser's answer.

settings.gradle.kts:

rootProject.name = "MyApp"

include(":Narengi")
project(":Narengi").name = "Bunny"

// OR

include(":Bunny")
project(":Bunny").projectDir = file("Narengi/")
Mahozad
  • 18,032
  • 13
  • 118
  • 133