Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define the teamcity['build.number'] property in gradle 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
like image 517
Gavriel Avatar asked Nov 25 '13 10:11

Gavriel


1 Answers

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?

like image 79
Joachim Chapman Avatar answered Sep 24 '22 18:09

Joachim Chapman