Skip to content

Commit 150e24d

Browse files
authored
[maven] environmentVariables -> globalProperties (#7559)
1 parent 206f3f7 commit 150e24d

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

modules/openapi-generator-maven-plugin/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ mvn clean compile
9898
| `skipIfSpecIsUnchanged` | `codegen.skipIfSpecIsUnchanged` | Skip the execution if the source file is older than the output folder (`false` by default. Can also be set globally through the `codegen.skipIfSpecIsUnchanged` property)
9999
| `addCompileSourceRoot` | `openapi.generator.maven.plugin.addCompileSourceRoot` | Add the output directory to the project as a source root, so that the generated java types are compiled and included in the project artifact (`true` by default). Mutually exclusive with `addTestCompileSourceRoot`.
100100
| `addTestCompileSourceRoot` | `openapi.generator.maven.plugin.addTestCompileSourceRoot` | Add the output directory to the project as a test source root, so that the generated java types are compiled only for the test classpath of the project (`false` by default). Mutually exclusive with `addCompileSourceRoot`.
101-
| `environmentVariables` | N/A | A **map** of items conceptually similar to "environment variables" or "system properties". These are merged into a map of global settings available to all aspects of the generation flow. Use this map for any options documented elsewhere as `systemProperties`.
101+
| `environmentVariables` | N/A | deprecated. Use globalProperties instead.
102+
| `globalProperties` | N/A | A **map** of items conceptually similar to "environment variables" or "system properties". These are available to all aspects of the generation flow. See [Global Properties](https://openapi-generator.tech/docs/globals/) for list of available properties.
102103
| `configHelp` | `codegen.configHelp` | dumps the configuration help for the specified library (generates no sources)
103104

104105
### Configuring **map** structures
@@ -114,17 +115,17 @@ For configuration options documented as a **map** above, the key/value options m
114115
```
115116

116117
This configuration node location will match that of the plugin configuration examples at the top of this document and in the section below. Here, `option` matches in option name in the first column in the table from the previous section.
117-
The `key` and `value` text are any values you'd like to provide for that option. As an example, to configure `environmentVariables` to match the `-Dmodels=User,Pet` example from our [Selective Generation](https://openapi-generator.tech/docs/customization#selective-generation) documentation, see below.
118+
The `key` and `value` text are any values you'd like to provide for that option. As an example, to configure `globalProperties` to match the `--global-property models=User,Pet` example from our [Selective Generation](https://openapi-generator.tech/docs/customization#selective-generation) documentation, see below.
118119

119120
```xml
120121
<configuration>
121-
<environmentVariables>
122+
<globalProperties>
122123
<models>User,Pet</models>
123-
</environmentVariables>
124+
</globalProperties>
124125
</configuration>
125126
```
126127

127-
Not that some of these environment variable options may overwrite or conflict with other options available to the maven plugin. For example, the above `environmentVariables` example is equivalent to the following:
128+
Not that some of these environment variable options may overwrite or conflict with other options available to the maven plugin. For example, the above `globalProperties` example is equivalent to the following:
128129

129130
```xml
130131
<configuration>
@@ -133,7 +134,7 @@ Not that some of these environment variable options may overwrite or conflict wi
133134
</configuration>
134135
```
135136

136-
The difference here is that you may define `generateModels` and `modelsToGenerate` as properties, while `environmentVariables` may only be configured as a configuration node.
137+
The difference here is that you may define `generateModels` and `modelsToGenerate` as properties, while `globalProperties` may only be configured as a configuration node.
137138

138139
### Custom Generator
139140

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,12 @@ public class CodeGenMojo extends AbstractMojo {
407407
@Parameter(defaultValue = "false", property = "openapi.generator.maven.plugin.addTestCompileSourceRoot")
408408
private boolean addTestCompileSourceRoot = false;
409409

410-
// TODO: Rename to global properties in version 5.0
410+
// TODO: Rename to global properties in version 5.1
411411
@Parameter
412412
protected Map<String, String> environmentVariables = new HashMap<>();
413413

414414
@Parameter
415-
protected Map<String, String> originalEnvironmentVariables = new HashMap<>();
415+
protected Map<String, String> globalProperties = new HashMap<>();
416416

417417
@Parameter(property = "codegen.configHelp")
418418
private boolean configHelp = false;
@@ -430,7 +430,6 @@ public void setBuildContext(BuildContext buildContext) {
430430
@Override
431431
public void execute() throws MojoExecutionException {
432432
File inputSpecFile = new File(inputSpec);
433-
resetEnvironmentVariables();
434433
addCompileSourceRootIfConfigured();
435434

436435
try {
@@ -699,13 +698,19 @@ public void execute() throws MojoExecutionException {
699698
applyReservedWordsMappingsKvpList(reservedWordsMappings, configurator);
700699
}
701700

702-
if (environmentVariables != null) {
703-
for (String key : environmentVariables.keySet()) {
704-
originalEnvironmentVariables.put(key, GlobalSettings.getProperty(key));
705-
String value = environmentVariables.get(key);
706-
if (value != null) {
707-
configurator.addGlobalProperty(key, value);
708-
}
701+
if (globalProperties == null) {
702+
globalProperties = new HashMap<>();
703+
}
704+
705+
if (environmentVariables != null && environmentVariables.size() > 0) {
706+
globalProperties.putAll(environmentVariables);
707+
getLog().warn("environmentVariables is deprecated and will be removed in version 5.1. Use globalProperties instead.");
708+
}
709+
710+
for (String key : globalProperties.keySet()) {
711+
String value = globalProperties.get(key);
712+
if (value != null) {
713+
configurator.addGlobalProperty(key, value);
709714
}
710715
}
711716

@@ -857,19 +862,6 @@ private void addCompileSourceRootIfConfigured() throws MojoExecutionException {
857862
}
858863
}
859864

860-
private void resetEnvironmentVariables() {
861-
// Reset all environment variables to their original value. This prevents unexpected
862-
// behaviour
863-
// when running the plugin multiple consecutive times with different configurations.
864-
for (Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) {
865-
if (entry.getValue() == null) {
866-
GlobalSettings.clearProperty(entry.getKey());
867-
} else {
868-
GlobalSettings.setProperty(entry.getKey(), entry.getValue());
869-
}
870-
}
871-
}
872-
873865
/**
874866
* This method enables conversion of true/false strings in
875867
* config.additionalProperties (configuration/configOptions) to proper booleans.

0 commit comments

Comments
 (0)