How to enable NetBeans 15 for Mac honour the system environment variables with Gradle builds

212 Views Asked by At

I have recently started to use GitHub Packages to distribute our shared libraries internally and have retrospectively changed the Gradle build configuration to use credentials based on system environment variables, rather than hard-coded e.g.

repositories {

    mavenLocal() // only use when testing -SNAPSHOT locally

    mavenCentral() // third-parties
    
    maven { // our-library
          name = "MyLibrary"
          url = "https://maven.pkg.github.com/MyCompany/mylibrary"
          credentials {
                    username = project.findProperty("git_username") ?: System.getenv("git_username")
                    password = project.findProperty("git_token") ?: System.getenv("git_token")
          }
    }
}

Unfortunately no matter how many environment variables I changed, whether in ~/.zshrc or .zshenv or .bash_profile or /etc/launchd.conf the build was indicating that the credentials were prohibited, or more accurately were resolved as nul.

Each time I changed a config I also ran the appropriate source to ensure it was active, I even resorted to rebooting, just in case.

The environment variable would always show in the terminal confirming the environment variable was always set up correctly, e.g. echo $git_username or printenv yielded the environment variable and expected sensitive token.

After a lot of experimenting and with the help of a colleague we determined that launching NetBeans 15 from the dock wasn't helping, so instead we also added the following line to ~/.zshrc

alias netbeans="/Applications/NetBeans/Apache\ NetBeans\ 15.app/Contents/MacOS/netbeans &"

But now to launch NetBeans I can't use the dock icon, but manually launch the terminal and then type

netbeans

Here's the new lines added to ~/.zshrc

#
# RW - For GitHub Package access
#
export git_username=NotApplicableUsesToken
export git_token=redacted1
export git_publish_username=NotApplicableUsesToken
export git_publish_token=redacted2

#
# RW - So Netbeans launches and honours the environment variables above
#
alias netbeans="/Applications/NetBeans/Apache\ NetBeans\ 15.app/Contents/MacOS/netbeans &"

My question is, why didn't NetBeans when launched from the dock discover the environment variables? How should I configure NetBeans to pickup the environment variables without this workaround?

1

There are 1 best solutions below

0
On

The Aqua GUI doesn't read any of the shell configuration files, e.g. .bash_profile, .bashrc, .login, .profile, .zprofile, or .zshrc. You were correct to start looking at launchd. Unfortunately /etc/launchd.conf is no longer supported and the file is not read.

Apple's Runtime Configuration Guidelines in the Environment Variables section states:

There are two ways to make environment variables available to an application. The first is to define the variables in a Terminal session and then launch the application from the same session. ...

The second way to associate environment variables with an application is to include the LSEnvironment key in the application’s information property list file. ...

Editing an application's plist doesn't seem like the best idea and your changes may be lost when the application is updated.

There is a third approach.

launchctl has setenv, unsetenv, and getenv sub-commands for managing environment variables. However the environment variables are not persisted across launchd instances.

It seems that a common approach is to create an agent job .plist in ~/Library/LaunchAgents that runs at user login to macOS that will execute launchctl setenv to set the environment variables that should be available to applications launched from the dock. There are discussions at "Set systemwide variable with /etc/launchd.conf does not work in 10.10" and "Environment variables for GUI apps" that point to resources for this approach. You may also want to see Creating Launch Daemons and Agents.

I haven't tested or tried this approach myself.