Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>config.file.test</groupId>
<artifactId>not-available</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>OpenAPI Generator Minimal Update Test</name>
<url>https://openapi-generator.tech/</url>
<build>
<finalName>No generation</finalName>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<configuration>
<configurationFile>${basedir}/config.yaml</configurationFile>
<skipIfConfigurationFileMissing>true</skipIfConfigurationFileMissing>
</configuration>
<executions>
<execution>
<id>executionId</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inputSpec: petstore-on-classpath.yaml
generatorName: spring
output: ./target/generated-sources/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>config.file.test</groupId>
<artifactId>available</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>ConfigFile</name>
<url>https://openapi-generator.tech/</url>
<build>
<finalName>configuration file test</finalName>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<configuration>
<configurationFile>${basedir}/config.yaml</configurationFile>
<skipIfConfigurationFileMissing>true</skipIfConfigurationFileMissing>
</configuration>
<executions>
<execution>
<id>executionId</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -811,4 +811,12 @@ public ClientOptInput toClientOptInput() {

return input.openAPI((OpenAPI) context.getSpecDocument());
}

public String getGeneratorName() {
return generatorName;
}

public String getInputSpec() {
return inputSpec;
}
}
Loading