Project property command line overrides broken?


(James Martin) #1

When I provide a value on the command line from our continuous integration build server for the project version property (or any project property), it does not override the default value of the ‘version’ property in build.gradle. Is this a bug or known issue? I can’t find any documentation indicating that project properties are exempt from the typical property precedence rules.


(Peter Niederwieser) #2

You can only set new (extra) properties from the command line, not set existing ones like ‘project.version’.


(James Martin) #3

I don’t think that statement is true in the same sense that my question is asking, which is about overriding from the command line. As far as I can tell, there is no such thing as overriding property values from the command line, regardless of whether they are built-in properties or extra properties.

It seems to me that it is only possible to give properties an initial value from the command line. Any values provided from the command line will get overridden by assignments to that same property within the build script if the values are not checked first.

It seems to me that it would be helpful, particularly for people coming to gradle from other build systems, for the documentation to make this treatment of properties obvious early on in the ‘basics’ section of the guide or somewhere. I came at it with an assumption that gradle properties might work something like ant properties, which was obviously an incorrect assumption, but there isn’t any information I could find that explicitly described the behavior.

The command line reference describes how to set project properties like this:

-P, --project-prop > Sets a project property of the root project, for example -Pmyprop=myvalue. > See Section 14.2, “Gradle properties and system properties”.

…and the “this-and-that” documentation (section 14.2 in the above snippet) on gradle properties uses the language of “adding” properties, which kind of sounds consistent with the actual behavior, but nowhere does it say that initial values for properties provided on the command line will get overwritten by unchecked assignments in the build script.

Did I miss something in the guide? Is there a section which more explicitly describes property value handling in gradle?


(James Martin) #4

Just some simple examples would make it clear:

task printVersion {
        println 'version = ' + version
    }

…with the above script, this command line:

gradle printVersion

…produces this output:

version = unspecified

…and this command line:

gradle -Pversion=command.line.version printVersion

…produces this output:

version = command.line.version

version = 'build.script.version'
    task printVersion {
        println 'version = ' + version
    }

…with this build script, this command line:

gradle printVersion

…produces this output:

version = build.script.version

…and this command line:

gradle -Pversion=command.line.version printVersion

…produces the same output:

version = build.script.version


(Peter Niederwieser) #5

That’s what I was trying to convey. ‘-P’ creates a new (extra) property, before any build script is evaluated. You wouldn’t typically have build scripts assign new values to this property, except perhaps to set a default value if the property wasn’t set from the command line.

Another way to frame this is that ‘-P’ isn’t all that different from ‘-D’, except that the former sets extra properties on the ‘Project’ object, whereas the latter sets Java system properties.

If you want to get involved in improving the documentation, we are always grateful for pull requests.


(James Martin) #6

If it’s not currently in the guides, I might take you up on making changes - if it would help others with similar expectations, it’s probably worth it.

Also, maybe this is a nit, but -P doesn’t necessarily create new properties - from what I can see, it can be used to set initial values for any gradle property, whether it’s built-in or an ext.

For example, you can provide -Pversion=1.2.3.4 on the command line, and that value will propagate to the project object at evaluation time. If you don’t set it in the build script, it will use the command line value during build scrip execution.


(James Martin) #7

Also - thank you for the reply and clarification.


(Peter Niederwieser) #8

Also, maybe this is a nit, but -P doesn’t necessarily create new properties

It does create a new property. If you set ‘-Pversion’, you’ll end up with an extra property that has the same name as the statically declared ‘Project#version’ property, which is probably not what you want.


(James Martin) #9

Oh - yikes! Good to know.