101

I intend to generalize the use of gradle for my projects and would like to reuse the same build file everywhere. Unfortunately, I have trouble trying to define the properties mentioned in $subject in a single file, in order to ease the migration.

This is gradle 1.6.

What I have tried, failing at all attempts:

  • gradle.properties: cannot modify name (read only, have to use a settings.gradle and override the root project name!); {source,target}Compatibility not taken into account;
  • settings.gradle: {source,target}Compatibility not taken into account either!

So, what is the correct method to achieve this? What I have tried so far in gradle.properties:

group = something
name = whatever  # cannot do!
version = whatever
sourceCompatibility = whatever # not taken into account!

And in settings.gradle:

sourceCompatibility = "whatever";  # not taken into account!

EDIT Well, the "name" problem just cannot be solved; for the rest, I have used another file which I apply in the build file. The "name" handling really isn't right :/

EDIT 2 This is now 2014 and gradle 1.12, and the problem still is not solved...

1

5 Answers 5

146

gradle.properties:

theGroup=some.group
theName=someName
theVersion=1.0
theSourceCompatibility=1.6

settings.gradle:

rootProject.name = theName

build.gradle:

apply plugin: "java"

group = theGroup
version = theVersion
sourceCompatibility = theSourceCompatibility
12
  • So you want to reuse across builds? You can do apply from: "other.gradle" or apply from: "http://my.server.com/other.gradle". Note that the latter isn't currently cached. To get caching, you'd have to write a binary plugin. Commented Jun 23, 2013 at 18:26
  • Yes, I use the apply from: solution; however this does not solve the name problem :(
    – fge
    Commented Jun 23, 2013 at 18:30
  • 1
    I think in latest Gradle you can apply a plugin in settings.gradle as well. However, it seems a bit odd to use a plugin to set the same project name across builds. Commented Jun 23, 2013 at 18:33
  • Note that it's also possible to set all these properties from an init.gradle in your user home (see user guide). Commented Jun 23, 2013 at 18:36
  • 1
    You don't have to define variables in gradle.properties and use in settings.gradle . You can just mention in settings.gradle rootProject.name = "someName". Make sure to specify it in double quotes in settings.gradle. Commented Dec 12, 2013 at 0:23
27

I found the solution to a similar problem. I am using Gradle 1.11 (as April, 2014). The project name can be changed directly in settings.gradle file as following:

  rootProject.name='YourNewName'

This takes care of uploading to repository (Artifactory w/ its plugin for me) with the correct artifactId.

3
  • 3
    This solution is less than ideal for multi-project builds.
    – asandroq
    Commented Oct 28, 2015 at 10:51
  • @asandroq, as you can only have a single settings file, you will have to rename sub module from there. It's central, but it works.
    – Snicolas
    Commented Nov 29, 2017 at 23:34
  • Renaming subprojects looks like project(":lib").name = "shared", and then you need to use the new name in sister projects e.g. implementation(project(":shared")). Weirdly, the old name can't be used for e.g. gradle :lib:build on the command-line, but IDEA won't let you use the new name. Commented Jun 8, 2023 at 10:26
20

I set the artifact baseName so it is independent of the build project name, which allows me to achieve what you want:

jar {
    baseName "core"
}

With this property set, even if my project name is "foo", when I run gradle install, the artifact is published with the name core instead of foo.

1
  • 2
    Does not help all other tasks that would be using the project name, and may create additional confusion over the clashing names...
    – Quartz
    Commented May 28, 2018 at 17:58
6

Apparently this would be possible in settings.gradle with something like this.

rootProject.name = 'someName'
gradle.rootProject {
    it.sourceCompatibility = '1.7'
}

I recently received advice that a project property can be set by using a closure which will be called later when the Project is available.

0

use buildSrc with Gradle Kotlin DSL see full worked example here: GitHub daggerok/spring-fu-jafu-example buildSrc/src/main/java/Globals.kt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.