[maven] Introduce addTestCompileSourceRoot (#6120)

This commit is contained in:
Falko Modler
2020-05-09 20:32:54 +02:00
committed by GitHub
parent 6ae61e2dfd
commit 3f174d8a30
2 changed files with 25 additions and 5 deletions
@@ -96,7 +96,8 @@ mvn clean compile
| `withXml` | `openapi.generator.maven.plugin.withXml` | enable XML annotations inside the generated models and API (only works with Java `language` and libraries that provide support for JSON and XML)
| `skip` | `codegen.skip` | skip code generation (`false` by default. Can also be set globally through the `codegen.skip` property)
| `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 (`true` by default)
| `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`.
| `configHelp` | `codegen.configHelp` | dumps the configuration help for the specified library (generates no sources)
@@ -403,11 +403,18 @@ public class CodeGenMojo extends AbstractMojo {
/**
* 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.
* are compiled and included in the project artifact. Mutually exclusive with {@link #addTestCompileSourceRoot}.
*/
@Parameter(defaultValue = "true", property = "openapi.generator.maven.plugin.addCompileSourceRoot")
private boolean addCompileSourceRoot = true;
/**
* 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. Mutually exclusive with {@link #addCompileSourceRoot}.
*/
@Parameter(defaultValue = "false", property = "openapi.generator.maven.plugin.addTestCompileSourceRoot")
private boolean addTestCompileSourceRoot = false;
// TODO: Rename to global properties in version 5.0
@Parameter
protected Map<String, String> environmentVariables = new HashMap<>();
@@ -431,6 +438,7 @@ public class CodeGenMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {
File inputSpecFile = new File(inputSpec);
resetEnvironmentVariables();
addCompileSourceRootIfConfigured();
try {
@@ -848,17 +856,28 @@ public class CodeGenMojo extends AbstractMojo {
final Object sourceFolderObject =
configOptions == null ? null : configOptions
.get(CodegenConstants.SOURCE_FOLDER);
final String sourceFolder =
sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString();
final String sourceFolder;
if (sourceFolderObject != null) {
sourceFolder = sourceFolderObject.toString();
} else {
sourceFolder = addTestCompileSourceRoot ? "src/test/java" : "src/main/java";
}
return output.toString() + "/" + sourceFolder;
}
private void addCompileSourceRootIfConfigured() {
private void addCompileSourceRootIfConfigured() throws MojoExecutionException {
if (addCompileSourceRoot) {
if (addTestCompileSourceRoot) {
throw new MojoExecutionException("Either 'addCompileSourceRoot' or 'addTestCompileSourceRoot' may be active, not both.");
}
project.addCompileSourceRoot(getCompileSourceRoot());
} else if (addTestCompileSourceRoot) {
project.addTestCompileSourceRoot(getCompileSourceRoot());
}
}
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.