5

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
Gavriel
  • 18,880
  • 12
  • 68
  • 105

4 Answers4

8

Given the scenario you describe - allowing developers to run a build on their local machine which also needs to run in TeamCity - I found this worked for me (TeamCity 7):

if (hasProperty("teamcity")) {
  version = teamcity["build.number"]
} else {
  version = '0.0-beta'
}

By default the gradle produced jar files will automatically use 'version' in their name. So with this code in the build.gradle file, developer builds will have artifacts tagged with '0.0-beta' and TeamCity builds of the same project pick up the TeamCity build number.

But if you want to, for instance, add information to the manifest you'll do something like:

jar {
  manifest {
    attributes 'Implementation-Title': rootProject.name, 'Implementation-Version': version
  }
}

I hope that helps?

Joachim Chapman
  • 340
  • 3
  • 14
  • This solution didn't work for me as it is because of this https://issues.gradle.org/browse/GRADLE-1826 . But I changed hasProperty("teamcity") to rootProject.hasProperty("teamcity") and now it's working. Thank you anyway! – MistaGreen May 05 '15 at 00:09
0

this works from the command line

task hello << {
  println project.ext['teamcity.build.number']
}

and you call it

gradle hello -Pteamcity.build.number=1.45

hopefully that'll work also in your script

  • No. I tried it already: Could not find property 'teamcity' on task ':MyProject:distTar'. – Gavriel Nov 26 '13 at 08:28
  • what problem did you have with project.ext approach? with the task hello it worked correctly although I changed from teamcity['build.number'] to project.ext['teamcity.build.number'] anyhow glad you were able to work it around – Luis Ramirez-Monterosa Nov 27 '13 at 16:04
0

It's a bit of hack, but this is the temporary solution I came up with. Still waiting for a better one though.

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. Optionally instead of adding dev=1 to gradle.properties, you can define it in the command line: -Pdev=1, this way you can do with or without the hack on the same machine (though I don't think it's useful)

Gavriel
  • 18,880
  • 12
  • 68
  • 105
0

This works for me in my build.gradle.kts (Kotlin syntax); I'm building the app in TeamCity and build.number is an existing property in there and takes a form of 8.2.0.XXX where XXX is the order number of the build run in TeamCity.

android {
    ...

    defaultConfig {
        applicationId("something")
        versionCode(1)
        versionName("8.2")
        if (rootProject.hasProperty("build.number")) {
            versionName(rootProject.properties["build.number"] as String?)
        }
        ...

    }
}

The variable can be referenced from the code using BuildConfig.VERSION_NAME.

If started from the IDE or cmd-line (where the property build.number does not exist), the app is built with 8.2 as BuildConfig.VERSION_NAME.

pepan
  • 678
  • 6
  • 11