Silk0vsky
Silk0vsky

Reputation: 1032

How to pass environment variable to gradle build file managed by Intellij IDEA

Environment: macOS BigSur

I have a gradle project which going to use private repository to fetch artifacts. To use such repos I need to supply gradle with credentials. Those credentials are temporal by nature, so I need to regenerate them from time to time. To do it I've tried to fetch credentials from environment variable.

repositories {
  maven {
    url 'https://***.codeartifact.***.amazonaws.com/maven/***'
      credentials {
        username "***"
        password System.getenv('CODEARTIFACT_AUTH_TOKEN')
    }
  }
}

I've tried to set this credentals via ~/.bash_profile & ~/.zshrc

When I'm running gradle commands via terminal, it works. But when I try to reload gradle projects via IDEA UI, it fails to lookup the environment variable.

enter image description here

Looks like IDEA use something that ignores standard terminals configs.

Just for curiosity I've tried to highlight environment variables that available for gradle build. So I've added print(System.getenv()) to build file. When I reloading projects via IDEA UI it outputs:

[PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin, JAVA_HOME:/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home, COMMAND_MODE:unix2003, VERSIONER_PYTHON_VERSION:2.7, LOGNAME:*******, XPC_SERVICE_NAME:application.com.jetbrains.intellij.ce.117113.118030, __CFBundleIdentifier:com.jetbrains.intellij.ce, SHELL:/bin/bash, USER:*******, TMPDIR:/var/folders/vf/thcas1g120g1df3brgb9h50w0450gp/T/, SSH_AUTH_SOCK:/private/tmp/com.apple.launchd.fa3tDAbfDE/Listeners, XPC_FLAGS:0x0, __CF_USER_TEXT_ENCODING:0x1F6:0x0:0x2, LC_CTYPE:en_GB.UTF-8, HOME:/Users/*******]

I guess something set this environment variables, but it's not clear how to set custom one.

Any ideas?

Upvotes: 3

Views: 2208

Answers (1)

Gleb Belyaev
Gleb Belyaev

Reputation: 1049

Helped for me:
I added variables in /etc/launchd.conf file.
Now Gradle from Intellij IDEA work correctly.

see https://apple.stackexchange.com/a/146047/17551.

Upvotes: 1

Related Questions