mirror of
https://github.com/ethauvin/rife2-hello.git
synced 2025-04-25 07:27:12 -07:00
Add support for extra template directories
By default, the template was using `src/main/templates` as the source directory for templates. This has the benefit of properly isolating the templates from the application classpath: the framework is then responsible for injecting them to the app runtime in whatever suitable form: either untouched as sources (in development mode), or precompiled (for production). With this change, it is also possible to use `src/main/resources/templates` as a source directory. It _may_ feel more natural to users (although I would disagree), but it has the drawback that the jars have to be configured to _exclude_ those resources, because by default whatever is in `src/main/resources` MUST be included in a jar (independently of the run mode). It is also possible for the user to configure additional source directories should they want to.
This commit is contained in:
parent
5ca1fa305b
commit
21c85ea93b
7 changed files with 70 additions and 23 deletions
|
@ -6,6 +6,7 @@ public class App extends Site {
|
|||
public void setup() {
|
||||
var hello = get("/hello", c -> c.print(c.template("hello")));
|
||||
get("/", c -> c.redirect(hello));
|
||||
get("/world", c -> c.print(c.template("world")));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -13,4 +14,4 @@ public class App extends Site {
|
|||
.staticResourceBase("src/main/webapp")
|
||||
.start(new App());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
11
app/src/main/resources/templates/world.html
Normal file
11
app/src/main/resources/templates/world.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title><!--v title-->Hello<!--/v--></title>
|
||||
<link rel="stylesheet" href="{{v webapp:rootUrl/}}css/style.css?{{v context:paramRandom/}}">
|
||||
</head>
|
||||
<body>
|
||||
<p>Hello World from src/main/resources/templates</p>
|
||||
</body>
|
||||
</html>
|
|
@ -23,7 +23,7 @@ import org.gradle.api.provider.Property;
|
|||
import org.gradle.api.tasks.CacheableTask;
|
||||
import org.gradle.api.tasks.Classpath;
|
||||
import org.gradle.api.tasks.Input;
|
||||
import org.gradle.api.tasks.InputDirectory;
|
||||
import org.gradle.api.tasks.InputFiles;
|
||||
import org.gradle.api.tasks.Optional;
|
||||
import org.gradle.api.tasks.OutputDirectory;
|
||||
import org.gradle.api.tasks.PathSensitive;
|
||||
|
@ -32,6 +32,7 @@ import org.gradle.api.tasks.TaskAction;
|
|||
import org.gradle.process.ExecOperations;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -41,9 +42,9 @@ public abstract class PrecompileTemplates extends DefaultTask {
|
|||
@Classpath
|
||||
public abstract ConfigurableFileCollection getClasspath();
|
||||
|
||||
@InputDirectory
|
||||
@InputFiles
|
||||
@PathSensitive(PathSensitivity.RELATIVE)
|
||||
public abstract DirectoryProperty getTemplatesDirectory();
|
||||
public abstract ConfigurableFileCollection getTemplatesDirectories();
|
||||
|
||||
@Input
|
||||
public abstract ListProperty<TemplateType> getTypes();
|
||||
|
@ -65,22 +66,27 @@ public abstract class PrecompileTemplates extends DefaultTask {
|
|||
@TaskAction
|
||||
public void precompileTemplates() {
|
||||
for (var type : getTypes().get()) {
|
||||
getExecOperations().javaexec(javaexec -> {
|
||||
javaexec.setClasspath(getClasspath());
|
||||
javaexec.getMainClass().set("rife.template.TemplateDeployer");
|
||||
List<String> args = new ArrayList<>();
|
||||
if (getVerbose().isPresent() && Boolean.TRUE.equals(getVerbose().get())) {
|
||||
args.add("-verbose");
|
||||
getTemplatesDirectories().getFiles().forEach(dir -> {
|
||||
if (Files.exists(dir.toPath())) {
|
||||
getExecOperations().javaexec(javaexec -> {
|
||||
javaexec.setClasspath(getClasspath());
|
||||
javaexec.getMainClass().set("rife.template.TemplateDeployer");
|
||||
List<String> args = new ArrayList<>();
|
||||
if (getVerbose().isPresent() && Boolean.TRUE.equals(getVerbose().get())) {
|
||||
args.add("-verbose");
|
||||
}
|
||||
args.add("-t");
|
||||
args.add(type.identifier());
|
||||
args.add("-d");
|
||||
args.add(getOutputDirectory().get().getAsFile().getPath());
|
||||
args.add("-encoding");
|
||||
args.add(getEncoding().orElse("UTF-8").get());
|
||||
args.add(dir.getPath());
|
||||
javaexec.args(args);
|
||||
});
|
||||
}
|
||||
args.add("-t");
|
||||
args.add(type.identifier());
|
||||
args.add("-d");
|
||||
args.add(getOutputDirectory().get().getAsFile().getPath());
|
||||
args.add("-encoding");
|
||||
args.add(getEncoding().orElse("UTF-8").get());
|
||||
args.add(getTemplatesDirectory().get().getAsFile().getPath());
|
||||
javaexec.args(args);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,8 @@
|
|||
*/
|
||||
package com.uwyn.rife2.gradle;
|
||||
|
||||
import org.gradle.api.*;
|
||||
import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.Configuration;
|
||||
import org.gradle.api.artifacts.ConfigurationContainer;
|
||||
import org.gradle.api.artifacts.dsl.DependencyHandler;
|
||||
|
@ -38,10 +39,11 @@ import org.gradle.api.tasks.testing.Test;
|
|||
import org.gradle.process.CommandLineArgumentProvider;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Rife2Plugin implements Plugin<Project> {
|
||||
public static final String DEFAULT_TEMPLATES_DIR = "src/main/templates";
|
||||
public static final List<String> DEFAULT_TEMPLATES_DIRS = List.of("src/main/resources/templates", "src/main/templates");
|
||||
public static final String DEFAULT_GENERATED_RIFE2_CLASSES_DIR = "generated/classes/rife2";
|
||||
public static final String RIFE2_GROUP = "rife2";
|
||||
public static final String WEBAPP_SRCDIR = "src/main/webapp";
|
||||
|
@ -114,7 +116,14 @@ public class Rife2Plugin implements Plugin<Project> {
|
|||
|
||||
private static void bundlePrecompiledTemplatesIntoJarFile(TaskContainer tasks,
|
||||
TaskProvider<PrecompileTemplates> precompileTemplatesTask) {
|
||||
tasks.named("jar", Jar.class, jar -> jar.from(precompileTemplatesTask));
|
||||
tasks.named("jar", Jar.class, jar -> {
|
||||
jar.from(precompileTemplatesTask);
|
||||
// This isn't great because it needs to be hardcoded, in order to avoid the templates
|
||||
// declared in `src/main/resources/templates` to be included in the jar file.
|
||||
// which means that if for whatever reason the user also uses the same directory for
|
||||
// something else, it will be excluded from the jar file.
|
||||
jar.exclude("templates");
|
||||
});
|
||||
}
|
||||
|
||||
private void createRife2DevelopmentOnlyConfiguration(Project project,
|
||||
|
@ -125,7 +134,7 @@ public class Rife2Plugin implements Plugin<Project> {
|
|||
conf.setCanBeConsumed(false);
|
||||
conf.setCanBeResolved(false);
|
||||
});
|
||||
rife2DevelopmentOnly.getDependencies().add(dependencies.create(project.files(DEFAULT_TEMPLATES_DIR)));
|
||||
DEFAULT_TEMPLATES_DIRS.stream().forEachOrdered(dir -> rife2DevelopmentOnly.getDependencies().add(dependencies.create(project.files(dir))));
|
||||
configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(rife2DevelopmentOnly);
|
||||
}
|
||||
|
||||
|
@ -149,6 +158,11 @@ public class Rife2Plugin implements Plugin<Project> {
|
|||
.filter(f -> f.getAsFile().getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
|
||||
.map(project::zipTree)
|
||||
.toList()));
|
||||
// This isn't great because it needs to be hardcoded, in order to avoid the templates
|
||||
// declared in `src/main/resources/templates` to be included in the jar file.
|
||||
// which means that if for whatever reason the user also uses the same directory for
|
||||
// something else, it will be excluded from the jar file.
|
||||
jar.exclude("templates");
|
||||
plugins.withId("application", unused -> jar.manifest(manifest ->
|
||||
manifest.getAttributes().put("Main-Class", rife2Extension.getUberMainClass().get()))
|
||||
);
|
||||
|
@ -223,7 +237,7 @@ public class Rife2Plugin implements Plugin<Project> {
|
|||
task.getVerbose().convention(true);
|
||||
task.getClasspath().from(rife2CompilerClasspath);
|
||||
task.getTypes().convention(rife2Extension.getPrecompiledTemplateTypes());
|
||||
task.getTemplatesDirectory().set(project.getLayout().getProjectDirectory().dir(DEFAULT_TEMPLATES_DIR));
|
||||
DEFAULT_TEMPLATES_DIRS.stream().forEachOrdered(dir -> task.getTemplatesDirectories().from(project.getLayout().getProjectDirectory().dir(dir)));
|
||||
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir(DEFAULT_GENERATED_RIFE2_CLASSES_DIR));
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title><!--v title-->Hello<!--/v--></title>
|
||||
<link rel="stylesheet" href="{{v webapp:rootUrl/}}css/style.css?{{v context:paramRandom/}}">
|
||||
</head>
|
||||
<body>
|
||||
<p>Hello World</p>
|
||||
</body>
|
||||
</html>
|
|
@ -25,6 +25,9 @@ class PackagingTest extends AbstractFunctionalTest {
|
|||
}
|
||||
}
|
||||
assert Files.exists(fs.getPath("/rife/template/html/hello.class"))
|
||||
assert Files.exists(fs.getPath("/rife/template/html/world.class"))
|
||||
assert !Files.exists(fs.getPath("/templates/hello.html"))
|
||||
assert !Files.exists(fs.getPath("/templates/world.html"))
|
||||
}
|
||||
|
||||
where:
|
||||
|
|
|
@ -42,6 +42,7 @@ class TemplateCompilationTest extends AbstractFunctionalTest {
|
|||
|
||||
then: "template sources must be present in the classpath"
|
||||
outputContains("Classpath entry: src/main/templates")
|
||||
outputContains("Classpath entry: src/main/resources/templates")
|
||||
}
|
||||
|
||||
def "compiles templates when running #task"() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue