Create a property for build script locations

I have the following code in one build.gradle file. NOTICE the line “classpath ‘org.webpieces:gradle-plugin-htmlcompiler:1.9.98’”

buildscript {

dependencies {
classpath ‘org.webpieces:gradle-plugin-htmlcompiler:1.9.98’
}
}

Then, I have this section in the master build.gradle file which is used by all the jar dependencies.

ext {
webpiecesVersion = ‘1.9.98’

}

How can I create a property that both of those things use so I only have to modify the version in one location?

thanks,
Dean

I believe project properties are available in the buildscript block, so you should be able to add a gradle.properties file to the root directory of your project and place the line foo=1.2.3 in there and then just use $foo where you need it.

nope! doesn’t seem to work. Easy to reproduce the failure though

(RUN exact commands…do NOT rename project)

git clone https://github.com/deanhiller/webpiecesexample-all.git
cd webpiecesexample-all/
git checkout -t origin/testBuildScriptReadProperties
./gradlew build

FAILS with
Could not find org.webpieces:gradle-plugin-htmlcompiler:${webpiecesVersion}

IMPORTANT files
webpiecesexample-all/gradle.properties contains variable
webpiecesexample-all/build.gradle USES variable just fine
webpiecesexample-all/webpiecesexample/build.gradle has buildscript trying to use the variable and fails

bump! oh, but I need 20 characters to bump this thread…grrr.

bump…anyone? would love to get just a little advice on this. @Schalk_Cronje any ideas?

Well this works for me:

buildscript {
        repositories {
                jcenter()
        }

        dependencies {
                classpath "org.jsoup:jsoup:$jsoupVersion"
        }
}
$ gradle tasks -PjsoupVersion=1.10.2

@Schalk_Cronje hmmm, do you have an example with it working in properties files. I mean I am thinking I don’t want to put all property versions on the command line. That would be really annoying for something that never changes. ie.

gradle tasks -PjsoupVersion=1.10.2 -PwebpiecesVersion=1.5 -Pslf4jVersion=1.4

kind of gets a bit unweildy for people that want to build the project, right? (and more importantly, than they need to know all the versions or I need to create a bash script but gradle should really handle that instead so I don’t need a bash script).

thanks!!!
Dean

You can set it by adding a gradle.properties files in the root of the project i.e.

jsoupVersion=1.10.2

Only limitations of the above are:

  • You cannot substitute project properties into the version of a plugin when supplied in the plugins {} block.
  • You can use propertjies from gradle.properties in buildSrc - for that you will need to do some more magic.

@Schalk_Cronje I’m very confused now then. Have you not tried the simple and quick 4 steps above I gave in my example? It doesn’t seem to be working.

They were simply

git clone https://github.com/deanhiller/webpiecesexample-all.git
cd webpiecesexample-all/
git checkout -t origin/testBuildScriptReadProperties
./gradlew build

fails with “Could not find org.webpieces:gradle-plugin-htmlcompiler:${webpiecesVersion}”

thanks,
Dean

@deanhiller The problem is not gradle.properties, but the fact that you used a non-interpolated string in webpiecesexample/build.gradle. Switch to using "org.webpieces:gradle-plugin-htmlcompiler:${webpiecesVersion}" instead.

@Schalk_Cronje thanks so much!!! worked like a charm. sorry, duh me.