io.quarkiverse.freemarker.runtime.FreemarkerBuildConfig Maven / Gradle / Ivy
package io.quarkiverse.freemarker.runtime;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import freemarker.template.TemplateModel;
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
@ConfigRoot(name = "freemarker", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class FreemarkerBuildConfig {
/**
* Comma-separated list of absolute resource paths to scan recursively for templates.
* All tree folder from 'resource-paths' will be added as a resource.
* Unprefixed locations or locations starting with classpath will be processed in the same way.
*
* Defaults relevant for this option are documented on {@code quarkus.freemarker.base-path}
*
* @deprecated Use template set group of options instead ({@code quarkus.freemarker..base-path}
* {@code quarkus.freemarker..includes} and
* {@code quarkus.freemarker..excludes})
*/
@ConfigItem
public Optional> resourcePaths;
/**
* List of directives to register with format name=classname
*
* @see freemarker.template.Configuration#setSharedVariable(String, TemplateModel)
*/
@ConfigItem
public Map directives;
/**
* The default template set
*/
@ConfigItem(name = ConfigItem.PARENT)
public TemplateSet defaultTemplateSet;
/**
* Additional named template sets.
*/
@ConfigDocSection
@ConfigDocMapKey("template-set-name")
@ConfigItem(name = ConfigItem.PARENT)
public Map namedTemplateSets;
@ConfigGroup
public static class TemplateSet {
/**
* The base path of this template set. Template set is a triple of {@code base-path}, {@code includes} and
* {@code excludes} serving to select a number of templates for inclusion in the native image.
* {@code includes} and {@code excludes} are relative to {@code base-path}.
*
* Use slash ({@code /}) as a path separator on all platforms. The value must not start with a slash.
*
* Do not set any {@code base-path} value if you want {@code includes} and {@code excludes} to be relative to
* root resource path.
*
Defaults
*
*
* Option
* Default value in case none of
* {@code quarkus.freemarker.[base-path|includes|excludes|resource-paths]}
* is set
* Default value otherwise
*
*
* {@code quarkus.freemarker.base-path}
* {@code freemarker/templates}
* not set (interpreted as root resource path folder)
*
*
* {@code quarkus.freemarker.includes}
* {@code **}
* not set (no files included)
*
*
* {@code quarkus.freemarker.excludes}
* not set (no files excluded)
* not set (no files excluded)
*
*
*
* The defaults described in the second column of the table are to achieve the backwards compatibility
* with the behavior of {@code quarkus.freemarker.resource-paths} before Quarkus Freemarker 0.2.0.
*
*
Allowed combinations
*
* Setting {@code base-path} and/or {@code excludes} but not setting {@code includes} will result in a build
* time error. We have chosen this behavior (rather than using {@code **} as a default for includes) to avoid
* including all resources inadvertently and thus bloating your native image.
*
* @since 0.2.0
*/
@ConfigItem
public Optional basePath;
/**
* A comma separated list of globs to select FreeMarker templates for inclusion in the native image.
*
* {@code includes} are relative to {@code base-path}. Use slash ({@code /}) as a path separator on all
* platforms. The glob syntax is documented on {@code quarkus.native.resources.includes}.
*
* Example:
*
*
* quarkus.freemarker.includes = **.ftl
*
*
* @since 0.2.0
*/
@ConfigItem
public Optional> includes;
/**
* A comma separated list of globs not to include in the native image.
*
* {@code excludes} are relative to {@code base-path}. Use slash ({@code /}) as a path separator on all
* platforms. The glob syntax is documented on {@code quarkus.native.resources.includes}.
*
* Example:
*
*
* quarkus.freemarker.excludes = **/unwanted*
*
*
* @since 0.2.0
*/
@ConfigItem
public Optional> excludes;
public TemplateSet assertValid(String key) {
if (!includes.isPresent()) {
final String infix = key == null ? "" : "." + key;
final String badProps = Stream
.of(new AbstractMap.SimpleImmutableEntry>("base-path", basePath),
new AbstractMap.SimpleImmutableEntry>("excludes", excludes))
.filter(entry -> entry.getValue().isPresent())
.map(AbstractMap.SimpleImmutableEntry::getKey)
.map(k -> "quarkus.freemarker" + infix + "." + k)
.collect(Collectors.joining(" and "));
throw new IllegalStateException(
"If you set " + badProps + ", you must also set quarkus.freemarker" + infix
+ ".includes;"
+ " check your application.properties or wherever you set the named properties");
}
return this;
}
public boolean isSetByUser() {
return basePath.isPresent() || includes.isPresent() || excludes.isPresent();
}
}
}