使用变量指定依赖项版本时构建失败

kam*_*661 8 java gradle

我正在尝试将我的maven项目迁移到gradle.我为变量springVersion中的所有项目指定了spring版本.但是由于某种原因,构建失败了一个特定的依赖org.springframework:spring-web:springVersion.当我直接输入版本org.springframework:spring-web:3.1.2.RELEASE一切都编译完成.这是我的build.gradle文件:

subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse-wtp'

    ext {    
        springVersion = "3.1.2.RELEASE"
    }
    repositories {
       mavenCentral()
    }

    dependencies {
        compile 'org.springframework:spring-context:springVersion'
        compile 'org.springframework:spring-web:springVersion'
        compile 'org.springframework:spring-core:springVersion'
        compile 'org.springframework:spring-beans:springVersion'

        testCompile 'org.springframework:spring-test:3.1.2.RELEASE'
        testCompile 'org.slf4j:slf4j-log4j12:1.6.6'
        testCompile 'junit:junit:4.10'
    }

    version = '1.0'

    jar {
        manifest.attributes provider: 'gradle'
    }
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

* What went wrong:
Could not resolve all dependencies for configuration ':hi-db:compile'.
> Could not find group:org.springframework, module:spring-web, version:springVersion.
  Required by:
      hedgehog-investigator-project:hi-db:1.0
Run Code Online (Sandbox Code Playgroud)

与org.springframework相同:spring-test:3.1.2.RELEASE执行测试时.

什么导致他的问题以及如何解决?

Pet*_*ser 29

从字面上看springVersion,您正在使用该版本.声明依赖项的正确方法是:

// notice the double quotes and dollar sign
compile "org.springframework:spring-context:$springVersion"
Run Code Online (Sandbox Code Playgroud)

这是使用Groovy字符串插值,这是Groovy的双引号字符串的一个显着特征.或者,如果您想以Java方式执行此操作:

// could use single-quoted strings here
compile("org.springframework:spring-context:" + springVersion)
Run Code Online (Sandbox Code Playgroud)

我不推荐后者,但希望有助于解释为什么你的代码不起作用.

  • 它在遇到的第一个错误时停止了. (2认同)