Is there a way to define teamcity['build.number'] property from command line

Is there a way to define teamcity[‘build.number’] property from command line? I tried -Pteamcity.build.number=1 but it didn’t work.

I have a build.gradle file with this task in it:

distTar {
    baseName = project.name+'.'+
                project.version+'.'+
                System.getProperty("system.rnf.brach_name")+'.'+
                teamcity['build.number']+'.'+
                teamcity['build.vcs.number.1']
      archiveName = baseName+'.tar'
    into(baseName) {
        from '.'
        include 'config/*'
        include 'run-script/*.sh'
    }
  }

It works on the build server, but it drives all the developers crazy, because we don’t have teamcity installed on our machines, and any gradle command gives us an error:

$ gradle tasks
  FAILURE: Build failed with an exception.
  * Where:
Build file '/home/me/work/myproject/build.gradle' line: 31
  * What went wrong:
A problem occurred evaluating root project 'myproject'.
> Could not find property 'teamcity' on task ':MyProject:distTar'.
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED

What is the ‘teamcity’ object? Where is it coming from?

It’s not something that Gradle provides out of the box.

It comes from TeamCity, the software on our build server. But I don’t think it matters what the strings are. How can I define something from the command line or environment variable or grade.properties, that will be accessible by the above cited distTar task as "teamcity[‘build.number’], or x[‘y.z’] ? It seams that I can do: -Py=1 or maybe even -Py.z=2, but I can’t find how to make the “x[‘y.z’]”

But I don’t think it matters what the strings are

This isn’t a simply key/value map. You can’t created nested structures from the command line.

You’ll have to use some indirection:

distTar {
  if (project.hasProperty("teamCity")) {
    } else {
    }
}

We tried that already. It doesn’t work. It does work only in our machine where there’s no teamcity, but it doesn’t enter the if block even on the temacity machine.

Currently I could only do this hack:

in build.gradle I added:

if (hasProperty("dev")) {
    apply from: 'teamcity.gradle'
}

I have this in teamcity.gradle:

task teamcity {
    teamcity['build.number'] = 1
    teamcity['build.vcs.number.1'] = 0
}

And I have this in gradle.properties:

dev=1

gradle.properties and teamcity.gradle is in .gitignore.

Then it’s not being added as a project property at the source.

It comes from TeamCity, the software on our build server

But what exactly? Do you have some kind of plugin installed in TeamCity for this?

yes, it’s a plugin (or built in) in TeamCity

See if this test works:

if (project.extensions.findByName("teamcity") == null) {
  project.ext.teamcity = [:]
}

yes, it works. Thanks

fwiw you can see how TC augments your build by looking for this file in your buildagent install directory

plugins/gradle-runner/scripts/init.gradle

you’ll see it sets project.ext.teamcity in a ProjectEvaluationListener

This is how I got it to detect the case with team-city correctly in the end:

if (project.ext.hasProperty(“teamcity”))

{

// We are being built from the IDE or commandline.

version = “1.0-SNAPSHOT”

releaseURL = “http://bla.bla.co.za:8081/nexus/content/repositories/snapshots

}

else

{

// We are being built by TeamCity.

version = “1.0.” + teamcity[‘build.number’]

releaseURL = “http://bla.bla.co.za:8081/nexus/content/repositories/releases

}

releaseURL is a local variable.

Luke, I think your condition should be the other way around. Either add “!” or change the 2 blocks of code

So sorry. My logical check there is the wrong way around.

So sorry guys. Let me repost that instead.

I made a few errors there: 1) Logic should have been inverted. 2) I was not supposed to look at project.ext but only project for the teamcity property.

if (project.hasProperty("teamcity"))
{
    // We are being built by TeamCity.
    version = "1.0." + teamcity['build.number']
    releaseURL = "http://bla.bla.co.za:8081/nexus/content/repositories/releases"
}
else
{
    // We are being built from the IDE or commandline.
    version = "1.0-SNAPSHOT"
    releaseURL = "http://bla.bla.co.za:8081/nexus/content/repositories/snapshots"
}