Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set project.version by passing version property on gradle command line?

I want to build JAR with self-defined version passed via command line, such as:

When I execute gradle build task like this:

gradle build -Pversion=1.0 

myproject-1.0.jar should be generated.

I have tried adding the line below to the build.gradle, but it did not work:

version = project.hasProperty('version') ? project['version'] : '10.0.0' 
like image 884
pat.inside Avatar asked Sep 22 '15 12:09

pat.inside


People also ask

How do I pass project property in Gradle?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle. properties files with the prefix systemProp.

How do I specify the Gradle version?

You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or update your Gradle version using the command line. The preferred way is to use the Gradle Wrapper command line tool, which updates the gradlew scripts. The following example sets the Gradle version to 7.5.

How do I set system property in Gradle?

To define system properties for our Gradle build we can use the command line option --system-prop or -D . But we can also add the values for system properties in the gradle. properties file of our project. This file contains project properties we want to externalized, but if we prefix the property name with systemProp.


2 Answers

Set the property only in the gradle.properties file (i.e. remove it from build.gradle). Also make sure the options come before the command (as mentioned above).

gradle.properties contents:

version=1.0.12 

Version can then be overridden on the command line with:

gradle -Pversion=1.0.13 publish 
like image 54
kindagonzo Avatar answered Oct 14 '22 21:10

kindagonzo


You are not able to override existing project properties from command line, take a look here. So try to rename a version variable to something differing from version and set it with -P flag before command, like:

gradle -PprojVersion=10.2.10 build  

And then in your build.gradle

if (project.hasProperty('projVersion')) {   project.version = project.projVersion } else {   project.version = '10.0.0' } 

Or as you did with ?: operator

like image 42
Stanislav Avatar answered Oct 14 '22 20:10

Stanislav