Contact Us 1-800-596-4880

Configuring Environment Properties

This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

You can configure your Mule application to automatically load property files based on the development environment (QA, preproduction, production, etc) you are working on, making it easier to deploy your app both on-premises and in the cloud. This configuration requires you to complete the following tasks:

  1. Create a properties file for each development environment in your application.

  2. Configure a properties placeholder in your app to look for the environment upon launch.

  3. Set an environment variable to point to a specific development environment during application deployment.

Consider externalizing other aspects of your configuration, like time-out values, polling frequencies, etc. Even if they don’t vary between environments. This will facilitate tuning and experimenting as the whole Mule application would become configurable through a single properties file.

Basic Anatomy of an Environment Property Configuration

A few things to consider before creating and configuring environment properties:

  • Use a consistent naming strategy for your properties files and make them unique across applications. This will be easier for re-using across teams.

  • A good strategy is to add the application name or ID to the properties file names.

If you’re deploying multiple applications through a Shared Resources structure, don’t set anything in the properties files, as there could be conflicts between the various apps that share a domain. Instead, you can set environment variables over the scope of the deployed app, its domain and other apps under that domain.

As explained in the Shared Resources page, in Studio you can create these variables through the Environment tab of the Run Configurations menu, reachable via the dropdown menu next to the Play button.

Review the following Mule configuration fragment that defines an HTTP connector with variable configuration:

<http:listener-config name="HttpListenerConfiguration"
                      doc:name="HTTP Listener Configuration"
                      host="${host}"
                      port="${port}"
                      basePath="${path}" />

This example uses Spring’s property placeholder resolution mechanism. The variable bits are clearly visible: the base path, host, and port can vary for each environment where this connector gets deployed in. When deploying your application, mule.env can be dynamically replaced by the particular environment you’re deploying to, such as qa or prod.

To provide values for these environment variables, we use a Yaml configuration file:

path: "test/products"
host: "localhost"
port: 8082

You could entirely avoid having to touch your application’s code values by adding a second Yaml file with other values assigned to the same variables:

path: "products"
host: "www.acme.com"
port: 8081

Creating and Configuring Environment Properties

Create variable properties files and configure them in your application to look for the environment upon launch. Additionally, you can test environment properties in Studio. To complete these tasks review the following steps:

Using Studio Visual Editor

  1. Define a list of environments you wish to support in your application. For example, one common use case involves configuring the application to support both Production and a QA environments.
    We will define the variable variable in the environments prod and qa and test it using a single application.

  1. Right-click the src/main/resources folder, then select New > File to create a properties file for each environment you wish to support. For example:

    • qa.yaml

    • prod.yaml

      deploying to multiple environments 01
  2. Double-click each of these new files to open them in new tabs in Studio. By editing them, you can configure the properties of the environment that correspond to the filename. For example, you may wish to add the properties as per the following images. Keep in mind that you can use these properties anywhere in your application.

    deploying to multiple environments 02
    deploying to multiple environments 03
  3. Close the properties files.

  4. Click in the Global Elements tab of your Studio project, below the canvas.

  5. Create a new global element. When selecting the element type, pick Configuration properties under Global Configurations.

    deploying to multiple environments 04
  6. Set the value of the File field to ${env}.yaml

  7. Create a new property to add a default environment. When selecting the element type, pick Global Property under Global Configurations.

    deploying to multiple environments 05
  8. Set a value to the property.

    deploying to multiple environments 06
  9. You can change the environment by editing the configured Global Property value.

Using XML Editor

  1. Define a list of environments you wish to support in your application. For example, one common use case involves configuring the application to support both Production and a QA environments.
    We will define the variable variable in the environments prod and qa and test it using a single application.

  1. Right-click the src/main/resources folder, then select New > File to create a properties file for each environment you wish to support. For example:

    • qa.yaml

    • prod.yaml

      deploying to multiple environments 01
  2. Double-click each of these new files to open them in new tabs in Studio. By editing them, you can configure the properties of the environment that correspond to the filename. For example, you may wish to add the properties as per the following images. Keep in mind that you can use these properties anywhere in your application.

    deploying to multiple environments 02
    deploying to multiple environments 03
  3. Close the properties files.

  4. Click in the Configuration XML tab of your Studio application, below the canvas.

  5. Add a configuration-properties element to the XML file and let the file name configurable.

<configuration-properties doc:name="Configuration properties" file="${env}.yaml" />
  1. Add a Global Property element to set the default environment name.

<global-property doc:name="Global Property" name="env" value="qa" />
  1. You can change the environment by editing the configured Global Property value.

Testing Configured Environment Properties in Studio

You can test that your application uses the specific environment you configured in your variables properties file, by running the application in Mule embedded in Studio.

  1. In the Package Explorer view, right-click the filename of the project you wish to deploy, then select Run As > Mule Application.

  2. Studio automatically deploys your application according to the environment variable you specified in the Global Property from the previous procedure.

  3. Again in the Package Explorer view, right-click the filename of the project you wish to deploy, then select Run As > Run Configurations…​.

    deploying to multiple environments 07
  4. Define the System Property in the Arguments tab.

    deploying to multiple environments 08
  5. Click the Run button to complete the procedure. The properties configured in prod.yaml will be used instead of the ones defined in qa.yaml.

Setting Environment Variables for Application Deployment

Mule loads the environment variables when it starts, so depending on your deployment target (CloudHub or On-premises) you need to set the environment when you deploy the app (CloudHub) or when you start your Mule instance (On-premises).

Setting a Specific Environment when Starting Mule On-Premises

Using the command line

Identify the environment in which to deploy your application at runtime with an environment variable. Execute the command to run Mule in your environment as per the following example:

$ mule -M-Dmule.env=prod

Once Mule starts with your configured environment, you can deploy the application with your desired deployment method.

Using Anypoint Runtime Manager

As a prerequisite you need to create a server in Anypoint Runtime Manager and link it to Mule. Then you can create a custom server property.

Setting a Specific Environment when Deploying to CloudHub

Deploying to CloudHub from Studio

From Studio you can initiate the process to set your properties environment variables and deploy your application to CloudHub :

  1. In the Package Explorer view, right-click the filename of the project you wish to deploy, then select Anypoint PlatformDeploy to Cloud.

    deploying to multiple environments 09
  2. Complete the requested data, and click the Properties tab.

  3. Add a new environment variable by setting Key to env and its value to prod.

    deploying to multiple environments 10
  4. Click Deploy Application.

Deploying to CloudHub from Runtime Manager

To deploy your application to CloudHub via Anypoint Runtime Manager, you can set properties values using Runtime Manager Console.