Search code examples
powershellparameterssonarqubesonar-runner

How to dynamically set ProjectVersion with Sonar-Runner.bat


Is there a way to use sonar-runner to dynamically update the project version for every build without having to manually update the sonar-project.properties file each time? (note: this is for a non-mavenized .NET MVC app)

Was trying to parameterize the properties file having it reference an environment var set automatically

Added in sonar-runner.bat

for /f "delims=" %%i in ('hg identify -n') do set HG_LOCAL_REV=%%i

Modified sonar-project.properites

sonar.projectVersion=%HG_LOCAL_REV% #don't want to have to manually update this

Would be great if we could just pass the projectVersion as a parameter for sonar-runner.bat


Solution

  • I decided to create a simple template file as a quick work around. Just search/replace for a unique string in the template file and save to a new sonar-project.properties file for the project.

    Here's some sample code in case anyone's interested:

    $hg_version = hg identify -n
    (get-content {C:\dev\myProj\sonar-project.template | foreach-object {$_ -replace "my_proj_version", $hg_version}) | set-content c:\dev\myProj\sonar-project.properties
    sonar-runner
    

    Hope this is helpful for anyone else finding this.