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'
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With