[bug] Templates via classpath (#5164)

* Fixes issue with templates loading via classpath

The templating engines were originally written to load templates via the
classpath, but this functionality was blocked by file-only checks
further up the stack. This loosens those file-only checks, allowing
files and relatively imported files to be included via classpath.

* [docs] Add details about classpath-level templates

* [feat][maven] templateResourcePath for template on classpath
  - NOTE templateResourcePath is not needed for gradle, as it accepts
    a string for the target template directory, which supports classpath
This commit is contained in:
Jim Schubert
2020-01-31 17:36:06 -05:00
committed by GitHub
parent 22c6c0ca68
commit 507f80617d
5 changed files with 60 additions and 5 deletions

View File

@@ -40,6 +40,7 @@ import java.util.Set;
import com.google.common.io.ByteSource;
import com.google.common.io.CharSource;
import io.swagger.v3.parser.util.ClasspathHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -133,6 +134,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "templateDirectory", property = "openapi.generator.maven.plugin.templateDirectory")
private File templateDirectory;
/**
* Resource path containing template files.
*/
@Parameter(name = "templateResourcePath", property = "openapi.generator.maven.plugin.templateResourcePath")
private String templateResourcePath;
/**
* The name of templating engine to use, "mustache" (default) or "handlebars" (beta)
*/
@@ -583,6 +590,13 @@ public class CodeGenMojo extends AbstractMojo {
configurator.setTemplateDir(templateDirectory.getAbsolutePath());
}
if (StringUtils.isNotEmpty(templateResourcePath)) {
if (null != templateDirectory) {
LOGGER.warn("Both templateDirectory and templateResourcePath were configured. templateResourcePath overwrites templateDirectory.");
}
configurator.setTemplateDir(templateResourcePath);
}
if (null != engine) {
configurator.setTemplatingEngineName(engine);
}