Sometimes you may want to generate custom Intellij IDEA run/debug configurations dynamically. Starting from 2020.1, IDEA can store run configurations as files. The run configuration files will be stored in .run directory of the current workspace.

With this feature, we can easily generate custom run configurations. All we need to do is to create configuration files dynamically and save them to the .run directory.

The run configuration is a simple XML file. Below are common options in the XML file.

OptionsDescription
VM_PARAMETERSJVM options
PROGRAM_PARAMETERSProgram parameters
MAIN_CLASS_NAMEMain class
WORKING_DIRECTORYWorking directory
envsEnvironment variables

Below is an example of the run configuration file.

<component name="ProjectRunConfigurationManager">
  <configuration default="false" name="my-app" type="JetRunConfigurationType" nameIsGenerated="true">
    <module name="app" />
    <option name="VM_PARAMETERS" value="-Dapp.value=xyz" />
    <option name="PROGRAM_PARAMETERS" value="" />
    <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
    <option name="ALTERNATIVE_JRE_PATH" />
    <option name="PASS_PARENT_ENVS" value="true" />
    <option name="MAIN_CLASS_NAME" value="com.mycompany.AppMain" />
    <option name="WORKING_DIRECTORY" value="" />
    <envs>
      <env name="TEST" value="demo" />
    </envs>
    <method v="2">
      <option name="Make" enabled="true" />
    </method>
  </configuration>
</component>

The run configuration file should be named as *.run.xml, e.g. my-app.run.xml.

After a new configuration file is created in .run directory, a new Run/Debug configuration will appear in IDEA automatically and can be used directly.

You can use the IDEA Run/Debug Configurations dialog to generate XML files in the .run directory, then use the generated file as a reference on how to generate them dynamically. When you select the checkbox "Store as project file", the run.xml file will be generated in the .run directory.