[maven] environmentVariables -> globalProperties (#7559)

This commit is contained in:
Jim Schubert
2020-10-01 21:33:17 -04:00
committed by GitHub
parent 206f3f7238
commit 150e24dc55
2 changed files with 22 additions and 29 deletions

View File

@@ -98,7 +98,8 @@ mvn clean compile
| `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)
| `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`.
| `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`.
| `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`.
| `environmentVariables` | N/A | deprecated. Use globalProperties instead.
| `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.
| `configHelp` | `codegen.configHelp` | dumps the configuration help for the specified library (generates no sources)
### Configuring **map** structures
@@ -114,17 +115,17 @@ For configuration options documented as a **map** above, the key/value options m
```
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.
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.
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.
```xml
<configuration>
<environmentVariables>
<globalProperties>
<models>User,Pet</models>
</environmentVariables>
</globalProperties>
</configuration>
```
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:
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:
```xml
<configuration>
@@ -133,7 +134,7 @@ Not that some of these environment variable options may overwrite or conflict wi
</configuration>
```
The difference here is that you may define `generateModels` and `modelsToGenerate` as properties, while `environmentVariables` may only be configured as a configuration node.
The difference here is that you may define `generateModels` and `modelsToGenerate` as properties, while `globalProperties` may only be configured as a configuration node.
### Custom Generator

View File

@@ -407,12 +407,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(defaultValue = "false", property = "openapi.generator.maven.plugin.addTestCompileSourceRoot")
private boolean addTestCompileSourceRoot = false;
// TODO: Rename to global properties in version 5.0
// TODO: Rename to global properties in version 5.1
@Parameter
protected Map<String, String> environmentVariables = new HashMap<>();
@Parameter
protected Map<String, String> originalEnvironmentVariables = new HashMap<>();
protected Map<String, String> globalProperties = new HashMap<>();
@Parameter(property = "codegen.configHelp")
private boolean configHelp = false;
@@ -430,7 +430,6 @@ public class CodeGenMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {
File inputSpecFile = new File(inputSpec);
resetEnvironmentVariables();
addCompileSourceRootIfConfigured();
try {
@@ -699,15 +698,21 @@ public class CodeGenMojo extends AbstractMojo {
applyReservedWordsMappingsKvpList(reservedWordsMappings, configurator);
}
if (environmentVariables != null) {
for (String key : environmentVariables.keySet()) {
originalEnvironmentVariables.put(key, GlobalSettings.getProperty(key));
String value = environmentVariables.get(key);
if (globalProperties == null) {
globalProperties = new HashMap<>();
}
if (environmentVariables != null && environmentVariables.size() > 0) {
globalProperties.putAll(environmentVariables);
getLog().warn("environmentVariables is deprecated and will be removed in version 5.1. Use globalProperties instead.");
}
for (String key : globalProperties.keySet()) {
String value = globalProperties.get(key);
if (value != null) {
configurator.addGlobalProperty(key, value);
}
}
}
final ClientOptInput input = configurator.toClientOptInput();
final CodegenConfig config = input.getConfig();
@@ -857,19 +862,6 @@ public class CodeGenMojo extends AbstractMojo {
}
}
private void resetEnvironmentVariables() {
// Reset all environment variables to their original value. This prevents unexpected
// behaviour
// when running the plugin multiple consecutive times with different configurations.
for (Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) {
if (entry.getValue() == null) {
GlobalSettings.clearProperty(entry.getKey());
} else {
GlobalSettings.setProperty(entry.getKey(), entry.getValue());
}
}
}
/**
* This method enables conversion of true/false strings in
* config.additionalProperties (configuration/configOptions) to proper booleans.