8

我正在尝试将我的 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'
    }
}

错误信息:

* 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

执行测试时 org.springframework:spring-test:3.1.2.RELEASE 也是如此。

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

4

2 回答 2

29

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

// notice the double quotes and dollar sign
compile "org.springframework:spring-context:$springVersion"

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

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

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

于 2012-09-23T14:51:08.130 回答
3

或者您可以像这样通过变量定义 lib 版本dependencies

dependencies {

    def tomcatVersion = '7.0.57'

    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
           exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
    }

}
于 2014-12-18T12:45:35.103 回答