Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Environment variables are often a crucial and essential part of configuring an application. They primarily permit us to store sensitive data, configuration settings, and other variables we can access from the code. Setting up environment variables in IntelliJ IDEA is an easy and straightforward process.

In this tutorial, we’ll look at how to set up environment variables in IntelliJ IDEA. We’ll also look at the importance and use of the environment variable in an application.

2. Why Use Environment Variables

Using environment variables, we can run an application in different environments (local, stage, prod, dev). It primarily helps us to configure the project to run in different environments without modifying the actual code. Hence, using the environment variables, we can handle other distributions, making it platform-independent.

Environment variables also provide security in storing sensitive information like API keys, passwords, and auth tokens. Storing this information directly in the code increases potential security threats and risks.

3. Setting up Environmental Variables

In IntelliJ, we can set the Environment variables differently to enhance the overall development work and to secure and provide a dynamic project configuration. Primarily, they’re used to store sensitive information such as API keys, database tokens, and access tokens.

3.1. Using Run Configurations

We can add an environment variable to a project by editing the run configuration of an application. To illustrate, let’s look at the step-by-step process of adding environment variables:

  • Open IntelliJ IDEA
  • Select a project
  • At the top, select Edit Configurations under the Current File
  • Edit the environment variables and click on the + button
  • Add the name and value of the environment variable

We can see the complete process of adding an environment variable to a project in IntelliJ below:

envVariable

In the above process, we added the environment variable testUser baeldung into a project in IntelliJ by making changes in the run configuration.

3.2. Using the workspace.xml

We can also add environment variables to a configuration file and link it to a project by editing the run configuration of an application.

To demonstrate, let’s look at the process of editing the configuration file for environment variables:

  • Open the IntelliJ IDEA IDE
  • Select a project
  • At the top right, select Edit Configurations under the Current File
  • Tick the configuration to “Store as a project file.”

The below GUI contains the process to create an environment configuration file:

envStore

The above steps help us to create a configuration file in which we can define environment variables. Furthermore, the workspace.xml contains all the configurations related to a project or an application. Additionally, it has all the required commands to run and execute the application. We can also add Environment variables to this file to make them available to the project.

Now, let’s look at the steps to add an environment variable to workspace.xml:

<option name="env">
    <map>
        <entry key="articleCategory" value="baeldung-java" />
    </map>
</option>

In the existing workspace.xml, we added the above configuration containing the environment variable articleCategory with the value baeldung-java.

4. Conclusion

This article explored various ways to set the environment variable for an application using IntelliJ. First, we explored the importance of the environment variable for a project. After that, we set up the environment variable in the application using GUI and XML configuration files.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.