How to set an environment variable from a Gradle build?
<p>I'm trying to set an environment variable from my Gradle build. I'm on MacOS X (El Capitan).
The command is "gradle test".</p>

<p>I'm trying this in my build.gradle:</p>

<pre><code>task setenv(type: Exec) {
    commandLine "export", "SOME_TEST_VAR=aaa"
}
test.dependsOn setenv
</code></pre>

<p>and the build fails:</p>

<blockquote>
  <p>Execution failed for task ':myproject:setenv'.</p>
  
  <blockquote>
    <p>A problem occurred starting process 'command 'export''</p>
  </blockquote>
</blockquote>

<p>I also tried this:</p>

<pre><code>test.doFirst {
    ProcessBuilder pb1 = new ProcessBuilder("export SOME_TEST_VAR=some test value")
    pb1.start();
}
</code></pre>

<p>The build succeeds. However, if I check the environment variable in my JUnit test it fails:</p>

<pre><code>assertTrue(System.getenv().containsKey("SOME_TEST_VAR"));
</code></pre>

<p>Is there any way to set an environment variable from a Gradle build (in the build.gradle file)?</p>

<p><strong>Update:</strong></p>

<p>I've tested it in isolation: the values do get passed and my test task receives everything, be it a systemProperty, environment variables or jvmArgs.  </p>

<p>So, it's nothing wrong with Gradle itself here.  </p>

<p>The problem arises when I'm trying it on the real project. It uses Spring for dependency injection. I may be wrong but it looks like the Spring framework purges those values somewhere.  </p>

<p>That sub-project is currently being frozen and I can't check my guess in detail right now.</p>