diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java index 443f5b52b482..cc0088e52663 100644 --- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java +++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java @@ -51,6 +51,7 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -195,6 +196,12 @@ public class CodeGenMojo extends AbstractMojo { @Parameter(name = "configurationFile", property = "openapi.generator.maven.plugin.configurationFile") private String configurationFile; + /** + * Skip the execution if the configuration file does not exist. + */ + @Parameter(name = "skipIfConfigurationFileMissing", property = "codegen.skipIfConfigurationFileMissing", defaultValue = "false") + private Boolean skipIfConfigurationFileMissing; + /** * Specifies if the existing files should be overwritten during the generation. */ @@ -563,6 +570,25 @@ public class CodeGenMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException { + if (Boolean.TRUE.equals(skipIfConfigurationFileMissing) && isNotEmpty(configurationFile)) { + if (!new File(configurationFile).exists()) { + getLog().info("Code generation is skipped because configuration file [" + configurationFile + "] is missing"); + return; + } + } + + // attempt to read from config file + CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile); + + // if a config file wasn't specified, or we were unable to read it + if (configurator == null) { + configurator = new CodegenConfigurator(); + } else { + // retrieve mandatory fields from the configurationFile if not defined in the pom.xml + this.generatorName = fromConfigurator(configurator.getGeneratorName(), generatorName); + this.inputSpec = fromConfigurator(configurator.getInputSpec(), inputSpec); + } + if (StringUtils.isBlank(inputSpec) && StringUtils.isBlank(inputSpecRootDirectory)) { LOGGER.error("inputSpec or inputSpecRootDirectory must be specified"); throw new MojoExecutionException("inputSpec or inputSpecRootDirectory must be specified"); @@ -638,14 +664,6 @@ public void execute() throws MojoExecutionException { } } - // attempt to read from config file - CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile); - - // if a config file wasn't specified, or we were unable to read it - if (configurator == null) { - configurator = new CodegenConfigurator(); - } - configurator.setVerbose(verbose); if (skipOverwrite != null) { @@ -1032,6 +1050,19 @@ public void execute() throws MojoExecutionException { } } + /** + * Use the configurator value is not defined in the pom.xml + * + * @param defaultValue default value taking precedence + */ + private T fromConfigurator(T value, T defaultValue) { + if (defaultValue != null) { + // keep backward compatibilty, the value in the pom.xml has precedence over the value in the config file. + return defaultValue; + } + return value; + } + /** * Calculate an SHA256 hash for the openapi specification. * If the specification is hosted on a remote resource it is downloaded first. diff --git a/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java b/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java index d08c88b8fac6..3899257d0e64 100644 --- a/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java +++ b/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java @@ -315,6 +315,31 @@ public void test_skipIfSpecIsUnchanged_recognizesUpdatesInExternalReferencedFile assertTrue("Src directory should have been regenerated", Files.exists(generatedDir.resolve("src"))); } + public void testConfigurationFile() throws Exception { + // GIVEN + CodeGenMojo mojo = loadMojo(newTempFolder(), "src/test/resources/configuration-file", null); + + // WHEN + mojo.execute(); + + // THEN + assertEquals("spring", getVariableValueFromObject(mojo, "generatorName")); + } + + public void testSkipConfigurationFileIfMissing() throws Exception { + // GIVEN + final Path tempDir = newTempFolder(); + final Path generatedDir = tempDir.resolve("target/generated-sources/configuration-file-missing"); + CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/configuration-file-missing", null); + + // WHEN + mojo.execute(); + + // THEN + assertFalse("Src directory should not be present", Files.exists(generatedDir.resolve("src"))); + } + + protected CodeGenMojo loadMojo(Path temporaryFolder, String projectRoot, String profile) throws Exception { return loadMojo(temporaryFolder, projectRoot, profile, "default"); } diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml new file mode 100644 index 000000000000..716ba128b998 --- /dev/null +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml @@ -0,0 +1,47 @@ + + + + 4.0.0 + config.file.test + not-available + jar + 1.0.0-SNAPSHOT + OpenAPI Generator Minimal Update Test + https://openapi-generator.tech/ + + No generation + + + org.openapitools + openapi-generator-maven-plugin + + ${basedir}/config.yaml + true + + + + executionId + generate-sources + + generate + + + + + + + diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml new file mode 100644 index 000000000000..f0dd0bfdbb80 --- /dev/null +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml @@ -0,0 +1,3 @@ +inputSpec: petstore-on-classpath.yaml +generatorName: spring +output: ./target/generated-sources/config diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml new file mode 100644 index 000000000000..4feec261020c --- /dev/null +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml @@ -0,0 +1,47 @@ + + + + 4.0.0 + config.file.test + available + jar + 1.0.0-SNAPSHOT + ConfigFile + https://openapi-generator.tech/ + + configuration file test + + + org.openapitools + openapi-generator-maven-plugin + + ${basedir}/config.yaml + true + + + + executionId + generate-sources + + generate + + + + + + + diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java index a3597fa37f2e..c1c8b6027eda 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java @@ -811,4 +811,12 @@ public ClientOptInput toClientOptInput() { return input.openAPI((OpenAPI) context.getSpecDocument()); } + + public String getGeneratorName() { + return generatorName; + } + + public String getInputSpec() { + return inputSpec; + } }