Skip to content

Commit 5a28ee2

Browse files
committed
configurationFile with generatorName
skipIfConfigurationFileMissing
1 parent f9d2b8b commit 5a28ee2

5 files changed

Lines changed: 172 additions & 8 deletions

File tree

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
import java.io.File;
5353
import java.io.IOException;
54+
import java.lang.reflect.Field;
5455
import java.net.MalformedURLException;
5556
import java.net.URI;
5657
import java.net.URISyntaxException;
@@ -195,6 +196,12 @@ public class CodeGenMojo extends AbstractMojo {
195196
@Parameter(name = "configurationFile", property = "openapi.generator.maven.plugin.configurationFile")
196197
private String configurationFile;
197198

199+
/**
200+
* Skip the execution if the source file is older than the output folder.
201+
*/
202+
@Parameter(name = "skipIfConfigurationFileMissing", property = "codegen.skipIfConfigurationFileMissing", defaultValue = "false")
203+
private Boolean skipIfConfigurationFileMissing;
204+
198205
/**
199206
* Specifies if the existing files should be overwritten during the generation.
200207
*/
@@ -563,6 +570,25 @@ public class CodeGenMojo extends AbstractMojo {
563570

564571
@Override
565572
public void execute() throws MojoExecutionException {
573+
if (Boolean.TRUE.equals(skipIfConfigurationFileMissing) && isNotEmpty(configurationFile)) {
574+
if (!new File(configurationFile).exists()) {
575+
getLog().info("Code generation is skipped because configuration file [" + configurationFile + "] is missing");
576+
return;
577+
}
578+
}
579+
580+
// attempt to read from config file
581+
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile);
582+
583+
// if a config file wasn't specified, or we were unable to read it
584+
if (configurator == null) {
585+
configurator = new CodegenConfigurator();
586+
} else {
587+
// retrieve mandatory fields from the configurationFile if not defined in the pom.xml
588+
this.generatorName = fromConfigurator(configurator, "generatorName", String.class, generatorName);
589+
this.inputSpec = fromConfigurator(configurator, "inputSpec", String.class, inputSpec);
590+
}
591+
566592
if (StringUtils.isBlank(inputSpec) && StringUtils.isBlank(inputSpecRootDirectory)) {
567593
LOGGER.error("inputSpec or inputSpecRootDirectory must be specified");
568594
throw new MojoExecutionException("inputSpec or inputSpecRootDirectory must be specified");
@@ -638,14 +664,6 @@ public void execute() throws MojoExecutionException {
638664
}
639665
}
640666

641-
// attempt to read from config file
642-
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile);
643-
644-
// if a config file wasn't specified, or we were unable to read it
645-
if (configurator == null) {
646-
configurator = new CodegenConfigurator();
647-
}
648-
649667
configurator.setVerbose(verbose);
650668

651669
if (skipOverwrite != null) {
@@ -1032,6 +1050,30 @@ public void execute() throws MojoExecutionException {
10321050
}
10331051
}
10341052

1053+
/**
1054+
* Access private fields of the CodegenConfigurator class.
1055+
*
1056+
* @param configurator the CodegenConfigurator
1057+
* @param fieldName name of the field
1058+
* @param clazz type of the field
1059+
* @param defaultValue default if configuration.fieldName is null
1060+
* @return the value of configuration.fieldName if defaultValue is null
1061+
*/
1062+
private <T> T fromConfigurator(CodegenConfigurator configurator, String fieldName, Class<T> clazz, T defaultValue) {
1063+
if (defaultValue != null) {
1064+
// keep backward compatibilty, the value in the pom.xml has precedence over the value in the config file.
1065+
return defaultValue;
1066+
}
1067+
try {
1068+
Field field = CodegenConfigurator.class.getDeclaredField(fieldName);
1069+
field.setAccessible(true);
1070+
T value = (T)field.get(configurator);
1071+
return value == null? defaultValue : value;
1072+
} catch (Exception e) {
1073+
throw new RuntimeException("Failed to read " + fieldName + " from configuration file.", e);
1074+
}
1075+
}
1076+
10351077
/**
10361078
* Calculate an SHA256 hash for the openapi specification.
10371079
* If the specification is hosted on a remote resource it is downloaded first.

modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,31 @@ public void test_skipIfSpecIsUnchanged_recognizesUpdatesInExternalReferencedFile
315315
assertTrue("Src directory should have been regenerated", Files.exists(generatedDir.resolve("src")));
316316
}
317317

318+
public void testConfigurationFile() throws Exception {
319+
// GIVEN
320+
CodeGenMojo mojo = loadMojo(newTempFolder(), "src/test/resources/configuration-file", null);
321+
322+
// WHEN
323+
mojo.execute();
324+
325+
// THEN
326+
assertEquals("spring", getVariableValueFromObject(mojo, "generatorName"));
327+
}
328+
329+
public void testSkipConfigurationFileIfMissing() throws Exception {
330+
// GIVEN
331+
final Path tempDir = newTempFolder();
332+
final Path generatedDir = tempDir.resolve("target/generated-sources/configuration-file-missing");
333+
CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/configuration-file-missing", null);
334+
335+
// WHEN
336+
mojo.execute();
337+
338+
// THEN
339+
assertFalse("Src directory should not be present", Files.exists(generatedDir.resolve("src")));
340+
}
341+
342+
318343
protected CodeGenMojo loadMojo(Path temporaryFolder, String projectRoot, String profile) throws Exception {
319344
return loadMojo(temporaryFolder, projectRoot, profile, "default");
320345
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!--
2+
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>minimal.update.test</groupId>
20+
<artifactId>minimal-update-test</artifactId>
21+
<packaging>jar</packaging>
22+
<version>1.0.0-SNAPSHOT</version>
23+
<name>OpenAPI Generator Minimal Update Test</name>
24+
<url>https://openapi-generator.tech/</url>
25+
<build>
26+
<finalName>minimal-update-test</finalName>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.openapitools</groupId>
30+
<artifactId>openapi-generator-maven-plugin</artifactId>
31+
<configuration>
32+
<configurationFile>${basedir}/config.yaml</configurationFile>
33+
<skipIfConfigurationFileMissing>true</skipIfConfigurationFileMissing>
34+
</configuration>
35+
<executions>
36+
<execution>
37+
<id>executionId</id>
38+
<phase>generate-sources</phase>
39+
<goals>
40+
<goal>generate</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
inputSpec: petstore-on-classpath.yaml
2+
generatorName: spring
3+
output: ./target/generated-sources/minimal-update<
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!--
2+
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>minimal.update.test</groupId>
20+
<artifactId>minimal-update-test</artifactId>
21+
<packaging>jar</packaging>
22+
<version>1.0.0-SNAPSHOT</version>
23+
<name>OpenAPI Generator Minimal Update Test</name>
24+
<url>https://openapi-generator.tech/</url>
25+
<build>
26+
<finalName>minimal-update-test</finalName>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.openapitools</groupId>
30+
<artifactId>openapi-generator-maven-plugin</artifactId>
31+
<configuration>
32+
<configurationFile>${basedir}/config.yaml</configurationFile>
33+
<skipIfConfigurationFileMissing>true</skipIfConfigurationFileMissing>
34+
</configuration>
35+
<executions>
36+
<execution>
37+
<id>executionId</id>
38+
<phase>generate-sources</phase>
39+
<goals>
40+
<goal>generate</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>

0 commit comments

Comments
 (0)