78

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'
pat.inside
  • 1,724
  • 4
  • 17
  • 25
  • Have you tried with adding system property (`-D`) instead of project property (`-P`)? – Misa Lazovic Sep 22 '15 at 12:50
  • @MisaLazovic It did not work either. – pat.inside Sep 22 '15 at 12:57
  • 1
    Stupid thing, but try with format `gradle [option] [task]`, not `gradle [task] [option]`, i.e. try `gradle -Pversion=1.0 build`. Any luck? – Misa Lazovic Sep 22 '15 at 12:59
  • @MisaLazovic Did not work. I do not think reading project or system property is the problem, because it can be read in my other tasks. I think there is something wrong when the build task executing, maybe the build task cannot dynamic read the version? – pat.inside Sep 22 '15 at 13:13
  • @MisaLazovic Sorry I try system property again, it works! But why project property does not work? – pat.inside Sep 22 '15 at 13:28

7 Answers7

134

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
TuringTux
  • 559
  • 1
  • 12
  • 26
kindagonzo
  • 1,468
  • 2
  • 10
  • 8
  • 2
    This is the best answer, easily reproducible without cluttering every build.gradle script. – thecodesmith_ Nov 03 '16 at 18:00
  • 3
    This answer is actually duplicate of the one by Opal on September 2015. I guess this one gets more upvotes because `org.gradle.project.version` is too long. – Franklin Yu Mar 01 '19 at 14:39
  • 3
    @FranklinYu This answer is actually more complete: it documents how to write the `version` in the `gradle.properties` (no need to test for validity of `version: 1.0.12`, or `version = "1.0.12"` or any other variants you could come up with in a hurry). – Alberto Sep 17 '19 at 20:30
44

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

Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • 6
    or: `project.version = project.findProperty('projVersion') ?: '10.0.0'` – Ajax Jan 27 '18 at 08:44
  • 2
    or `version = findProperty 'projVersion' ?: '10.0.0'` if you value brevity – Ajax Jan 27 '18 at 08:45
  • or, as the answer I posted: `version = (findProperty('version') == 'unspecified') ? '10.0.0' : version` – Ajax Jan 27 '18 at 10:10
  • "You are not able to override existing project properties from command line" This is plain wrong. – Alex Sep 22 '21 at 12:55
18

If you move version entry to gradle.properties file you can also:

gradle clean build -Dorg.gradle.project.version=1.1
Opal
  • 81,889
  • 28
  • 189
  • 210
  • 9
    `-Dorg.gradle.project.version` is way to long :-) It's actually sufficient to supply `-Pversion=1.1`. – thokuest Sep 24 '15 at 18:28
10

You can pass the project version on cli with -Pversion=... as long as you don't set it in build.gradle. If you need a custom default value for when no version is passed on the cli, use gradle.properties file like so: version=...

TL;DR: Don't set the version in build.gradle file if you want to change it later on via cli.

Alex
  • 1,313
  • 14
  • 28
  • 1
    nice, this caught me off guard, the whole point of passing at runtime is supposed to act as an override at runtime, but that's not the case it seems, gradle does not even warn if build.gradle already has a hardcoded version!! – kisna Nov 05 '19 at 22:22
  • I agree it's unintuitive. Yet again, I'm biased against gradle in general and do not miss it at all on any maven project. – Alex Jan 14 '20 at 17:49
  • In build.gradle, you're programmatically calling org.gradle.api.Project.setVersion(xxx) with your hardcoded version. If you want to state it as a property that can be overridden, use gradle.properties. – Adrian Baker Sep 28 '20 at 22:19
9

If you need a default version other than 'unspecified':

version = "${version != 'unspecified' ? version : 'your-default-version'}"

Pass version via command line:

gradle build -P version=1.0
Bian Jiaping
  • 946
  • 8
  • 20
5

version = (findProperty('version') == 'unspecified') ? '0.1' : version

Ajax
  • 2,465
  • 23
  • 20
2

I've found this to be the easiest and cleanest way, without requiring a gradle.properties file or changing the version variable name.

In build.gradle:

// Note - there is intentionally no equals sign here
version project.hasProperty('version') ? version : '1.0.0'

From the command line:

./gradlew -Pversion=1.5.2 build
blacktide
  • 10,654
  • 8
  • 33
  • 53