title: Define a variable and set it to a default value if something goes wrong during definition
body: <p>I have the following code in my <code>build.gradle</code></p>
  <p>Contents in <code>version.properties</code> are:</p>
  <pre><code>buildVersion=1.2.3
  </code></pre>
  <ul>
  <li>Value of <code>$v</code> variable during the Gradle build is coming as: 1.2.3 <br/></li>
  <li>Value of <code>$artifactoryVersion</code> variable in JENKINS build is coming as: 1.2.3.<strong>1</strong>, 1.2.3.<strong>2</strong>, 1.2.3.<strong>x</strong> ... and so on where the 4th digit is Jenkins BUILD_NUMBER available to gradle build script during Jenkins build.</li>
  </ul>
  <p>BUT, when I'm running this <code>build.gradle</code> on my desktop where I dont have BUILD_NUMBER variable available or set in my ENVIRONMENT variables, I get an error saying <code>trim()</code> can't work on null. (as there's no <code>BUILD_NUMBER</code> for Desktop/local build).</p>
  <p>I'm trying to find a way i.e.</p>
  <ol>
  <li><p>What should I code in my script so that if <code>BUILD_NUMBER</code> is not available, then instead of gradle build processing failing for an error, it'd set <code>jenkinsBuild = &quot;0&quot;</code> (hard coded) otherwise, pick what it gets during Jenkins build.</p>
  <p>For ex: in Bash, we set a variable <code>var1=${BUILD_NUMBER:-&quot;0&quot;}</code> which will set <code>var1</code> to a valid Jenkins BUILD number if it's available and set to a value, otherwise if it's NULL, then <code>var1 = &quot;0&quot;</code>.</p>
  </li>
  <li><p>I DON'T want to have each developer/user set this <code>BUILD_NUMBER</code> in some property file. All I want is, if this variable doesn't exist, then the code should put <code>&quot;0&quot;</code> in jenkinsBuilds variable and doesn't error out during desktop builds. I know during Jenkins build, it's working fine.</p>
  </li>
  </ol>
  
  <pre><code>// Build Script
  def fname = new File( 'version.properties' )
  Properties props = new Properties()
  props.load( new FileInputStream( fname ) )
  def v = props.get( 'buildVersion' )
  
  
  def env = System.getenv()
  def jenkinsBuild = env['BUILD_NUMBER'].trim()
  if( jenkinsBuild.length() &gt; 0 ) {
    artifactoryVersion = &quot;$v.$jenkinsBuild&quot;
  }
  </code></pre>