Skip to content

Commit

Permalink
[maven] environmentVariables -> globalProperties (#7559)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimschubert authored Oct 2, 2020
1 parent 206f3f7 commit 150e24d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
13 changes: 7 additions & 6 deletions modules/openapi-generator-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -430,7 +430,6 @@ public void setBuildContext(BuildContext buildContext) {
@Override
public void execute() throws MojoExecutionException {
File inputSpecFile = new File(inputSpec);
resetEnvironmentVariables();
addCompileSourceRootIfConfigured();

try {
Expand Down Expand Up @@ -699,13 +698,19 @@ public void execute() throws MojoExecutionException {
applyReservedWordsMappingsKvpList(reservedWordsMappings, configurator);
}

if (environmentVariables != null) {
for (String key : environmentVariables.keySet()) {
originalEnvironmentVariables.put(key, GlobalSettings.getProperty(key));
String value = environmentVariables.get(key);
if (value != null) {
configurator.addGlobalProperty(key, value);
}
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);
}
}

Expand Down Expand Up @@ -857,19 +862,6 @@ private void addCompileSourceRootIfConfigured() throws MojoExecutionException {
}
}

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.
Expand Down

0 comments on commit 150e24d

Please sign in to comment.