Gradle Dependency版本属性

时间:2018-11-30 11:31:44

标签: maven gradle

我有多个具有相同组ID和版本ID的依赖项。 例如,

dependencies {
    compile "org.springframework.boot:spring-boot-starter-jetty:2.1.0.RELEASE"
    compile "org.springframework.boot:spring-boot-starter:2.1.0.RELEASE"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

我想将2.1.0.RELEASE移到一次定义的属性中。 这是example,我将在Maven中进行。

<properties>
    <spring.boot.version>2.1.0.RELEASE</spring.boot.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
</dependencies>

我想用gradle获得类似的方法。到目前为止,我试图拥有一个 gradle.properties 文件,并在该文件中拥有一个属性。

springBootVersion=2.1.0.RELEASE

build.gradle 将是

dependencies {
    compile "org.springframework.boot:spring-boot-starter:${springBootVersion}"
    compile "org.springframework.boot:spring-boot-starter-jetty:${springBootVersion}"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

如果我在属性名称中使用点.,它将无法构建。 同样,gradle.properties中的 spring.boot.version 属性失败,并显示错误:

Could not get unknown property 'spring' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

1 个答案:

答案 0 :(得分:1)

简而言之,您不能直接使用spring.boot.version来引用用相同名称定义的属性,因为在Groovy it actually means reference the property called **version** in the object called **boot** which is a property of object called **spring**中可以这样做

...-boot-starter: ${project.ext["spring.Boot.Version"]}"

这将允许您添加带有任意数量的句点的属性。例如,在您的gradle.properties

hello.world=heelloooo

那你就可以做

print "The world of stackoverflow says ${project.ext["hello.world"]}"在您的构建脚本中。

相关问题