Search code examples
jenkinspropertiesmsbuilddefault

Setting value for a property MSBuild


How to set a property based in whether value for a parameter is available (from Jenkin's job) or not. If available, I need to use that value, else a default value in MSBuild.

Thanks...


Solution

  • You do this via "condition". (and optionally you can stack them if necessary)

    First example, set a property if the command line did not set it. below, if the command line (which is what jenkins probably does, is send in properties via the command line).........if the command line does not set the property, the property will resolve to peanut. else it will use what the command line sent in.

      <PropertyGroup>
        <WorkingCheckout Condition="'$(WorkingCheckout)'!=''">peanut</WorkingCheckout>
      </PropertyGroup>
    

    and for kicks....a second example (this does not address your question but enhances this answer)

    setting a property using condition and exists...if there are multiple possibilities. The below will check

    ".." and "..\.." and "..\..\.."

    for a file and set it if the file exists. And I put in a "error message" if no "exists" condition is satisfied. A poor man's exception handling.

      <PropertyGroup>
        <MySettingsFile>Bad_Relative_Path_Logic_Need_To_Check_All_Relative_Paths</MySettingsFile>
        <MySettingsFile Condition="Exists('$(WorkingCheckoutMassaged)..\SomeFolder\MySettings.xml')">$(WorkingCheckoutMassaged)..\SomeFolder\MySettings.xml</MySettingsFile>
        <MySettingsFile Condition="Exists('$(WorkingCheckoutMassaged)..\..\SomeFolder\MySettings.xml')">$(WorkingCheckoutMassaged)..\..\SomeFolder\MySettings.xml</MySettingsFile>
        <MySettingsFile Condition="Exists('$(WorkingCheckoutMassaged)..\..\..\SomeFolder\MySettings.xml')">$(WorkingCheckoutMassaged)..\..\..\SomeFolder\MySettings.xml</MySettingsFile>
      </PropertyGroup>