Project property command line overrides broken?

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.

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

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?

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

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.

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.

Also - thank you for the reply and clarification.

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.

1 Like

Oh - yikes! Good to know.