mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-10-14 16:33:56 +00:00
feat: add support for minimal update option in maven plugin (#21872)
This commit is contained in:
parent
fde017150e
commit
2ebda09b9f
@ -321,6 +321,12 @@ public class CodeGenMojo extends AbstractMojo {
|
|||||||
@Parameter(name = "generateAliasAsModel", property = "openapi.generator.maven.plugin.generateAliasAsModel")
|
@Parameter(name = "generateAliasAsModel", property = "openapi.generator.maven.plugin.generateAliasAsModel")
|
||||||
private Boolean generateAliasAsModel;
|
private Boolean generateAliasAsModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only write output files that have changed.
|
||||||
|
*/
|
||||||
|
@Parameter(name = "minimalUpdate", property = "openapi.generator.maven.plugin.minimalUpdate")
|
||||||
|
private Boolean minimalUpdate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A map of language-specific parameters as passed with the -c option to the command line
|
* A map of language-specific parameters as passed with the -c option to the command line
|
||||||
*/
|
*/
|
||||||
@ -698,6 +704,10 @@ public class CodeGenMojo extends AbstractMojo {
|
|||||||
configurator.setGenerateAliasAsModel(generateAliasAsModel);
|
configurator.setGenerateAliasAsModel(generateAliasAsModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (minimalUpdate != null) {
|
||||||
|
configurator.setEnableMinimalUpdate(minimalUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
if (isNotEmpty(generatorName)) {
|
if (isNotEmpty(generatorName)) {
|
||||||
configurator.setGeneratorName(generatorName);
|
configurator.setGeneratorName(generatorName);
|
||||||
} else {
|
} else {
|
||||||
|
@ -38,6 +38,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -88,6 +89,17 @@ public class CodeGenMojoTest extends BaseTestCase {
|
|||||||
assertEquals("joda", configOptions.get("dateLibrary"));
|
assertEquals("joda", configOptions.get("dateLibrary"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testMinimalUpdateConfiguration() throws Exception {
|
||||||
|
// GIVEN
|
||||||
|
CodeGenMojo mojo = loadMojo(newTempFolder(), "src/test/resources/minimal-update", null);
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
mojo.execute();
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
assertEquals(Boolean.TRUE, getVariableValueFromObject(mojo, "minimalUpdate"));
|
||||||
|
}
|
||||||
|
|
||||||
public void testHashGenerationFileContainsExecutionId() throws Exception {
|
public void testHashGenerationFileContainsExecutionId() throws Exception {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
final Path tempDir = newTempFolder();
|
final Path tempDir = newTempFolder();
|
||||||
@ -136,6 +148,50 @@ public class CodeGenMojoTest extends BaseTestCase {
|
|||||||
assertFalse("src directory should not have been regenerated", Files.exists(generatedDir.resolve("src")));
|
assertFalse("src directory should not have been regenerated", Files.exists(generatedDir.resolve("src")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testMinimalUpdate() throws Exception {
|
||||||
|
//GIVEN
|
||||||
|
/* Set up the mojo */
|
||||||
|
final Path tempDir = newTempFolder();
|
||||||
|
final CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/minimal-update", null, "executionId");
|
||||||
|
|
||||||
|
/* Perform an initial generation */
|
||||||
|
mojo.execute();
|
||||||
|
|
||||||
|
/* Collect last modified times of generated files */
|
||||||
|
final Path generatedDir = tempDir.resolve("target/generated-sources/minimal-update");
|
||||||
|
assertTrue("Generated directory should exist", Files.exists(generatedDir));
|
||||||
|
|
||||||
|
Map<Path, Long> lastModifiedTimes = new HashMap<>();
|
||||||
|
try (Stream<Path> files = Files.walk(generatedDir)) {
|
||||||
|
files
|
||||||
|
.filter(Files::isRegularFile)
|
||||||
|
.filter(path -> !path.getFileName().toString().endsWith(".sha256"))
|
||||||
|
.filter(path -> !path.getFileName().toString().equals("FILES"))
|
||||||
|
.forEach(file -> {
|
||||||
|
try {
|
||||||
|
lastModifiedTimes.put(file, Files.getLastModifiedTime(file).toMillis());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
assertTrue("Should have recorded last modified times for more than 3 files", lastModifiedTimes.size() > 3);
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
/* Execute the mojo again */
|
||||||
|
mojo.execute();
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
/* Verify that file modification times haven't changed (files weren't touched) */
|
||||||
|
for (Map.Entry<Path, Long> entry : lastModifiedTimes.entrySet()) {
|
||||||
|
Path file = entry.getKey();
|
||||||
|
Long originalTime = entry.getValue();
|
||||||
|
Long currentTime = Files.getLastModifiedTime(file).toMillis();
|
||||||
|
assertEquals("File " + file + " should not have been modified (minimal update should skip unchanged files)",
|
||||||
|
originalTime, currentTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For a Pom file which refers to an input file which will be on the classpath, as opposed to a file path,
|
* For a Pom file which refers to an input file which will be on the classpath, as opposed to a file path,
|
||||||
* test that the generated source is regenerated when the hash has changed.
|
* test that the generated source is regenerated when the hash has changed.
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
<!--
|
||||||
|
~ 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>minimal.update.test</groupId>
|
||||||
|
<artifactId>minimal-update-test</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>minimal-update-test</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.openapitools</groupId>
|
||||||
|
<artifactId>openapi-generator-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<inputSpec>petstore-on-classpath.yaml</inputSpec>
|
||||||
|
<generatorName>spring</generatorName>
|
||||||
|
<output>${basedir}/target/generated-sources/minimal-update</output>
|
||||||
|
<minimalUpdate>true</minimalUpdate>
|
||||||
|
<configOptions>
|
||||||
|
<hideGenerationTimestamp>true</hideGenerationTimestamp>
|
||||||
|
</configOptions>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>executionId</id>
|
||||||
|
<phase>generate-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>generate</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
Loading…
x
Reference in New Issue
Block a user