How can I define the teamcity['build.number'] property in gradle from command line

前端 未结 3 1724
天命终不由人
天命终不由人 2021-01-03 02:42

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 wit

相关标签:
3条回答
  • 2021-01-03 03:11

    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?

    0 讨论(0)
  • 2021-01-03 03:18

    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

    0 讨论(0)
  • 2021-01-03 03:29

    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)

    0 讨论(0)
提交回复
热议问题