How to set the project name/group/version, plus {source,target} compatibility in the same file?

JavaGradle

Java Problem Overview


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...

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfgeView Question on Stackoverflow
Solution 1 - JavaPeter NiederwieserView Answer on Stackoverflow
Solution 2 - JavazggameView Answer on Stackoverflow
Solution 3 - JavaJeffView Answer on Stackoverflow
Solution 4 - JavaEero AaltonenView Answer on Stackoverflow
Solution 5 - JavaMaksim KostrominView Answer on Stackoverflow