Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Failed when specified dependency version using variable

Tags:

java

gradle

I am trying to migrate my maven project to gradle. I specify spring version for all the project in variable springVersion. But from some reason build fails on one particular dependency org.springframework:spring-web:springVersion. When I type the version directly org.springframework:spring-web:3.1.2.RELEASE everything compiles. Here is my build.gradle file:

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'
    }
}

ERROR MESSAGE:

* 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

The same is with org.springframework:spring-test:3.1.2.RELEASE when performing tests.

Whats causing he problem and how to solve it?

like image 638
kamuflage661 Avatar asked Sep 23 '12 11:09

kamuflage661


2 Answers

You are using springVersion as the version, literally. The correct way to declare the dependencies is:

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

This is using Groovy String interpolation, a distinguishing feature of Groovy's double-quoted strings. Or, if you want to do it the Java way:

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

I don't recommend the latter, but it hopefully helps to explain why your code doesn't work.

like image 187
Peter Niederwieser Avatar answered Oct 27 '22 04:10

Peter Niederwieser


Or you can define lib version via variable in dependencies like this:

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'
    }

}
like image 20
Nikita Koksharov Avatar answered Oct 27 '22 04:10

Nikita Koksharov