title: How to configure Jenkins to @Grab dependencies from Artifactory in sandboxed pipeline script
body: <p>I'm able to use @Grab annotation in Jenkins pipeline script in both, sandbox and non-sandbox mode. My problem is that dependencies are by default resolved against maven central repository but I need to get them resolved against our company Artifactory. To demonstrate the issue here's simple pipeline script:</p>
  
  <pre><code>//@Grab(group = 'my.compay', module='my-module-name', version='1.0.0-SNAPSHOT')
  //import my.company.MyFancyClass
  @Grab('com.google.guava:guava:23.0')
  import com.google.common.base.Joiner
  
  pipeline {
      agent any
      stages {
          stage('Grape Test') {
              steps {
                  echo "Joiner: ${Joiner.class}"
                  // echo "MyFancyClass: ${MyFancyClass.class}"
              }
          }
      }
  }
  
  </code></pre>
  
  <p>When I run script as it is, I'll see in output fully qualified name of the class. However, when I uncomment commented lines which works with dependency exposed in company artifactory script fails with following:</p>
  
  <pre><code>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
  WorkflowScript: 2: unable to resolve class my.company.MyFancyClass
   @ line 1, column 1.
     @Grab(group = 'my.company', module='my-module-name', version='1.0.0-SNAPSHOT')
  
  1 error
  </code></pre>
  
  <p>After investigation I find out that @Grab uses Groovy specific thing called Grape which is described <a href="http://docs.groovy-lang.org/latest/html/documentation/grape.html" rel="noreferrer">here</a>. Mentioned documentation describes how to configure Grape to use your own repositories using ivy settings in file ~/.groovy/grapeConfig.xml. I found solution and make it working for regular Groovy code with following content:</p>
  
  <pre><code>&lt;ivysettings&gt;
  &lt;settings defaultResolver="downloadGrapes"/&gt;
  &lt;credentials host="localhost" realm="Artifactory Realm" username="USERNAME" passwd="PASSWORD"/&gt;
  &lt;resolvers&gt;
    &lt;chain name="downloadGrapes" returnFirst="true"&gt;
      &lt;filesystem name="cachedGrapes"&gt;
        &lt;ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/&gt;
        &lt;artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"/&gt;
      &lt;/filesystem&gt;
      &lt;ibiblio name="localm2" root="file:${user.home}/.m2/repository/" checkmodified="true" changingPattern=".*" changingMatcher="regexp" m2compatible="true"/&gt;
  
      &lt;ibiblio name="maven-release" m2compatible="true" root="http://artifactory:8081/artifactory/maven-release"/&gt;
      &lt;ibiblio name="maven-snapshot" m2compatible="true" root="http://artifactory:8081/artifactory/maven-snapshot"/&gt;
      &lt;!-- todo add 'endorsed groovy extensions' resolver here --&gt;
      &lt;ibiblio name="jcenter" root="https://jcenter.bintray.com/" m2compatible="true"/&gt;
      &lt;ibiblio name="ibiblio" m2compatible="true"/&gt;
    &lt;/chain&gt;
  &lt;/resolvers&gt;
  &lt;/ivysettings&gt;
  </code></pre>
  
  <p>Unfortunately this ivy configuration file fixing my problem only partialy, and make Grapes working as expected only from regular Groovy code (same for calling grape command from CLI).</p>
  
  <p>I have no idea how Jenkins uses Groovy/Grapes but it obviously ignores default location for this file. I read some Ivy docs and found out that can customize some ivy settings like for example config file location using properties. That's why I tried to convice my Jenkins to effectively use my config by running it from CLI using following command:</p>
  
  <pre><code>java -Divy.settings.file=%USERPROFILE%\.groovy\grapeConfig.xml -jar jenkins.war --httpPort=8888
  </code></pre>
  
  <p>That really works but to my surprise again only partially! It works only when I switch off sandbox mode for my pipeline script. If I switch it on, I'm again able to @Grab dependencies only from maven central.</p>
  
  <p>Any ideas are very welcome...</p>