如何在同一个文件中设置项目名/组/版本,以及{ source,target }兼容性?

我打算为我的项目推广使用 gradle,并希望在任何地方重用相同的构建文件。不幸的是,为了简化迁移,我在尝试在单个文件中定义 $subject 中提到的属性时遇到了麻烦。

这是1.6级。

我所做的尝试,无论如何都失败了:

  • 无法修改名称(只能读,必须使用 settings.gradle并覆盖 项目名称!); 未考虑 {source,target}Compatibility;
  • 也没有考虑到 {source,target}Compatibility

那么,实现这一目标的正确方法是什么呢:

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

settings.gradle:

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

EDIT 好吧,“ name”问题就是不能解决; 对于其他的,我使用了另一个文件,我在构建文件中应用了它。“ name”处理确实不正确:/

现在是2014年级和1.12年级,问题仍然没有解决..。

120666 次浏览

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

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.

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.

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.

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