Build Fails with a Specified Version of Dependencies Using a Variable

I am trying to migrate a maven project to gradle. I am specifying the spring version for the entire project in the springVersion variable. But for some reason, the build fails with one specific 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 

Same thing with org.springframework: spring -test: 3.1.2.RELEASE when running tests.

What causes his problem and how to solve it?

+8
java gradle
source share
2 answers

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

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

Groovy String interpolation is used, a distinctive feature of Groovy double-quoted strings. Or, if you want to do it in a Java way:

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

I do not recommend the latter, but it will hopefully help explain why your code does not work.

+27
source share

Or you can determine the version of lib through a variable in dependencies as follows:

 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' } } 
+3
source share

All Articles