Trying to define global ext properties version for dependencies

Hello

If I have the following from Maven (it works fine)

        <properties>
		<maven.test.failure.ignore>true</maven.test.failure.ignore>
		<logback.version>1.1.2</logback.version>
		<slf4j.version>1.7.10</slf4j.version>
	</properties>	
	<dependencies>
		<!-- Logging -->
    	<dependency>
    		<groupId>org.slf4j</groupId>
    		<artifactId>slf4j-api</artifactId>
    		<version>${slf4j.version}</version>
		</dependency>    		
     	<dependency>
    		<groupId>org.slf4j</groupId>
    		<artifactId>jcl-over-slf4j</artifactId>
    		<version>${slf4j.version}</version>
    	</dependency>
    	<dependency>
    		<groupId>ch.qos.logback</groupId>
    		<artifactId>logback-classic</artifactId>
    		<version>${logback.version}</version>
		</dependency>
	</dependencies>

I have tried the following through Gradle

First:

ext {
	slf4jVersion = '1.7.10'
	logbackVersion = '1.1.2'
}

Until here I did realize I am not able to define slf4j.version, it fails if I use a dot. Therefore I must use in this way: slf4jVersion. No use a dot, and use an uppercase for a better human reading.

Question One: Is there a legal syntax way to keep the dot?

Second:

dependencies {

	compile 'org.slf4j:slf4j-api:' + slf4jVersion,
			'org.slf4j:jcl-over-slf4j:' + slf4jVersion,
			'ch.qos.logback:logback-classic:' + logbackVersion
	
}

It works only in that way. Just curious if exists other way to avoid the +.

I have tried

dependencies {

	compile 'org.slf4j:slf4j-api:slf4jVersion',
		    'org.slf4j:jcl-over-slf4j:slf4jVersion',
		     'ch.qos.logback:logback-classic:logbackVersion'
	
}

and fails. Even using double quotes “”, it fails. I am not an expert in Groovy syntax.

Question two: what could be the elegant way to accomplish this approach without +. Pls I don’t want use the group, name and version attributes.

Thanks in advanced.

There a few ways to do this.

  1. Use the map syntax: ext['slf4j.version'] = '1.7.10'
  2. Explicitly call the set() method: ext { set('slf4j.version', '1.7.10') }
  3. Put the properties in a gradle.properties file slf4j.version=1.7.10

Double-quotes with the property syntax ${}.

compile "org.slf4j:slf4j-api:${slf4jVersion}"

Note, if you end up going with the “dot” syntax you’ll have to do something a bit different. Best to just use the property() method.

compile "org.slf4j:slf4j-api:${property('slf4j.version')}"

Hello Mark

Thanks a lot by the support.

Seems use the dot complicates a little more the syntax unnecessarily.

Finally I use this approach:

ext {
	slf4jVersion = '1.7.10'
	logbackVersion = '1.1.2'	
}

dependencies {

	compile "org.slf4j:slf4j-api:${slf4jVersion}",
		    "org.slf4j:jcl-over-slf4j:${slf4jVersion}",
		    "ch.qos.logback:logback-classic:${logbackVersion}"
	
}

I am almost sure that kind of example is not available through Gradle’s documentation.
Correct me if I am wrong.

Thanks by your support!

This is really just the nature of Groovy rather than being anything Gradle specific. Dot notation is used to access object properties in Groovy. If you want to use a dot in the name of a symbol you have to effectively escape it by using a string literal.

Hello Mark

Thanks again by the clarification. Understood.