mirror of
https://github.com/ethauvin/rife2-hello.git
synced 2025-04-26 07:47:13 -07:00
Aligned with Gradle plugin source state
This commit is contained in:
parent
794f614e57
commit
7f82944093
4 changed files with 132 additions and 20 deletions
|
@ -36,33 +36,65 @@ import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gradle task to pre-compile RIFE2 templates
|
||||||
|
*/
|
||||||
@CacheableTask
|
@CacheableTask
|
||||||
public abstract class PrecompileTemplates extends DefaultTask {
|
public abstract class PrecompileTemplates extends DefaultTask {
|
||||||
|
/**
|
||||||
@Classpath
|
* The directories where template files can be found.
|
||||||
public abstract ConfigurableFileCollection getClasspath();
|
*
|
||||||
|
* @return the directories with template files
|
||||||
|
*/
|
||||||
@InputFiles
|
@InputFiles
|
||||||
@PathSensitive(PathSensitivity.RELATIVE)
|
@PathSensitive(PathSensitivity.RELATIVE)
|
||||||
public abstract ConfigurableFileCollection getTemplatesDirectories();
|
public abstract ConfigurableFileCollection getTemplatesDirectories();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The template types to pre-compile.
|
||||||
|
*
|
||||||
|
* @return a list of template types
|
||||||
|
*/
|
||||||
@Input
|
@Input
|
||||||
public abstract ListProperty<TemplateType> getTypes();
|
public abstract ListProperty<TemplateType> getTypes();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The encoding to use when reading the template files.
|
||||||
|
* Defaults to {@code UTF-8}.
|
||||||
|
*
|
||||||
|
* @return the encoding of the template files
|
||||||
|
*/
|
||||||
@Input
|
@Input
|
||||||
@Optional
|
@Optional
|
||||||
public abstract Property<String> getEncoding();
|
public abstract Property<String> getEncoding();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether the pre-compilation should be verbose or not.
|
||||||
|
*
|
||||||
|
* @return {@code true} when the pre-compilation should be verbose; or
|
||||||
|
* {@code false} otherwise
|
||||||
|
*/
|
||||||
@Input
|
@Input
|
||||||
@Optional
|
@Optional
|
||||||
public abstract Property<Boolean> getVerbose();
|
public abstract Property<Boolean> getVerbose();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the directory into which pre-compiled template class files should be stored.
|
||||||
|
*
|
||||||
|
* @return the output directory for the template pre-compilation
|
||||||
|
*/
|
||||||
@OutputDirectory
|
@OutputDirectory
|
||||||
public abstract DirectoryProperty getOutputDirectory();
|
public abstract DirectoryProperty getOutputDirectory();
|
||||||
|
|
||||||
|
@Classpath
|
||||||
|
public abstract ConfigurableFileCollection getClasspath();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
protected abstract ExecOperations getExecOperations();
|
protected abstract ExecOperations getExecOperations();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform the template pre-compilation
|
||||||
|
*/
|
||||||
@TaskAction
|
@TaskAction
|
||||||
public void precompileTemplates() {
|
public void precompileTemplates() {
|
||||||
for (var type : getTypes().get()) {
|
for (var type : getTypes().get()) {
|
||||||
|
|
|
@ -19,14 +19,45 @@ import org.gradle.api.file.ConfigurableFileCollection;
|
||||||
import org.gradle.api.provider.ListProperty;
|
import org.gradle.api.provider.ListProperty;
|
||||||
import org.gradle.api.provider.Property;
|
import org.gradle.api.provider.Property;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Gradle RIFE2 extension
|
||||||
|
*/
|
||||||
public abstract class Rife2Extension {
|
public abstract class Rife2Extension {
|
||||||
|
/**
|
||||||
|
* The RIFE2 version that should be used by the project.
|
||||||
|
*
|
||||||
|
* @return the RIFE2 version as a string
|
||||||
|
*/
|
||||||
public abstract Property<String> getVersion();
|
public abstract Property<String> getVersion();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether the project should be launched with the RIFE2 agent or not.
|
||||||
|
*
|
||||||
|
* @return {@code true} when the project should be launched with the RIFE2 agent;
|
||||||
|
* {@code false} otherwise
|
||||||
|
*/
|
||||||
public abstract Property<Boolean> getUseAgent();
|
public abstract Property<Boolean> getUseAgent();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies the main Java class to use when building the uber jar.
|
||||||
|
*
|
||||||
|
* @return the fully qualified name of the main class to use when launching the uber jar.
|
||||||
|
*/
|
||||||
public abstract Property<String> getUberMainClass();
|
public abstract Property<String> getUberMainClass();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies the template types that should be precompiled.
|
||||||
|
* By default, none are precompiled.
|
||||||
|
*
|
||||||
|
* @return a list of template types to precompile
|
||||||
|
*/
|
||||||
public abstract ListProperty<TemplateType> getPrecompiledTemplateTypes();
|
public abstract ListProperty<TemplateType> getPrecompiledTemplateTypes();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies the directories where the template files can be found.
|
||||||
|
* By default, this includes {@code "src/main/resources/templates"}.
|
||||||
|
*
|
||||||
|
* @return the collection of directories to look for template files
|
||||||
|
*/
|
||||||
public abstract ConfigurableFileCollection getTemplateDirectories();
|
public abstract ConfigurableFileCollection getTemplateDirectories();
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,11 +45,11 @@ import java.util.Locale;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Rife2Plugin implements Plugin<Project> {
|
public class Rife2Plugin implements Plugin<Project> {
|
||||||
public static final List<String> DEFAULT_TEMPLATES_DIRS = List.of("src/main/resources/templates");
|
static final List<String> DEFAULT_TEMPLATES_DIRS = List.of("src/main/resources/templates");
|
||||||
public static final String DEFAULT_GENERATED_RIFE2_CLASSES_DIR = "generated/classes/rife2";
|
static final String DEFAULT_GENERATED_RIFE2_CLASSES_DIR = "generated/classes/rife2";
|
||||||
public static final String RIFE2_GROUP = "rife2";
|
static final String RIFE2_GROUP = "rife2";
|
||||||
public static final String WEBAPP_SRCDIR = "src/main/webapp";
|
static final String WEBAPP_SRCDIR = "src/main/webapp";
|
||||||
public static final String PRECOMPILE_TEMPLATES_TASK_NAME = "precompileTemplates";
|
static final String PRECOMPILE_TEMPLATES_TASK_NAME = "precompileTemplates";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(Project project) {
|
public void apply(Project project) {
|
||||||
|
@ -121,10 +121,18 @@ public class Rife2Plugin implements Plugin<Project> {
|
||||||
Rife2Extension rife2Extension) {
|
Rife2Extension rife2Extension) {
|
||||||
tasks.named("jar", Jar.class, jar -> {
|
tasks.named("jar", Jar.class, jar -> {
|
||||||
jar.from(precompileTemplatesTask);
|
jar.from(precompileTemplatesTask);
|
||||||
// This isn't great because it needs to be partially hardcoded, in order to avoid the templates
|
excludeTemplateSourcesInClassPath(jar, precompileTemplatesTask, rife2Extension);
|
||||||
// declared in `src/main/resources/templates` to be included in the jar file.
|
});
|
||||||
rife2Extension.getPrecompiledTemplateTypes().get().forEach(templateType ->
|
}
|
||||||
jar.exclude("/templates/**." + templateType.identifier().toLowerCase()));
|
|
||||||
|
private static void excludeTemplateSourcesInClassPath(Jar jar, TaskProvider<PrecompileTemplates> precompileTemplatesTask, Rife2Extension rife2Extension) {
|
||||||
|
// This isn't great because it needs to be partially hardcoded, in order to avoid the templates
|
||||||
|
// declared in `src/main/resources/templates` to be included in the jar file.
|
||||||
|
precompileTemplatesTask.get().getTemplatesDirectories().forEach(dir -> {
|
||||||
|
if (dir.getAbsolutePath().contains("/src/main/resources/")) {
|
||||||
|
rife2Extension.getPrecompiledTemplateTypes().get().forEach(templateType ->
|
||||||
|
jar.exclude("/" + dir.getName() + "/**." + templateType.identifier().toLowerCase()));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +146,7 @@ public class Rife2Plugin implements Plugin<Project> {
|
||||||
conf.setCanBeResolved(false);
|
conf.setCanBeResolved(false);
|
||||||
});
|
});
|
||||||
rife2DevelopmentOnly.getDependencies().addAllLater(templateDirectories.getElements().map(locations ->
|
rife2DevelopmentOnly.getDependencies().addAllLater(templateDirectories.getElements().map(locations ->
|
||||||
locations.stream().map(fs -> dependencies.create(project.files(fs))).collect(Collectors.toList()))
|
locations.stream().map(fs -> dependencies.create(project.files(fs))).collect(Collectors.toList()))
|
||||||
);
|
);
|
||||||
configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(rife2DevelopmentOnly);
|
configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(rife2DevelopmentOnly);
|
||||||
}
|
}
|
||||||
|
@ -163,11 +171,7 @@ public class Rife2Plugin implements Plugin<Project> {
|
||||||
.filter(f -> f.getAsFile().getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
|
.filter(f -> f.getAsFile().getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
|
||||||
.map(project::zipTree)
|
.map(project::zipTree)
|
||||||
.toList()));
|
.toList()));
|
||||||
// This isn't great because it needs to be hardcoded, in order to avoid the templates
|
excludeTemplateSourcesInClassPath(jar, precompileTemplatesTask, rife2Extension);
|
||||||
// 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 ->
|
plugins.withId("application", unused -> jar.manifest(manifest ->
|
||||||
manifest.getAttributes().put("Main-Class", rife2Extension.getUberMainClass().get()))
|
manifest.getAttributes().put("Main-Class", rife2Extension.getUberMainClass().get()))
|
||||||
);
|
);
|
||||||
|
@ -246,4 +250,4 @@ public class Rife2Plugin implements Plugin<Project> {
|
||||||
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir(DEFAULT_GENERATED_RIFE2_CLASSES_DIR));
|
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir(DEFAULT_GENERATED_RIFE2_CLASSES_DIR));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,24 +1,69 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2003-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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.
|
||||||
|
*/
|
||||||
package com.uwyn.rife2.gradle;
|
package com.uwyn.rife2.gradle;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows template types to be specified for pre-compilation.
|
||||||
|
*/
|
||||||
public class TemplateType implements Serializable {
|
public class TemplateType implements Serializable {
|
||||||
@Serial private static final long serialVersionUID = -2736320275307140837L;
|
@Serial private static final long serialVersionUID = -2736320275307140837L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@code html} template type.
|
||||||
|
*/
|
||||||
public static TemplateType HTML = new TemplateType("html");
|
public static TemplateType HTML = new TemplateType("html");
|
||||||
|
/**
|
||||||
|
* The {@code json} template type.
|
||||||
|
*/
|
||||||
public static TemplateType JSON = new TemplateType("json");
|
public static TemplateType JSON = new TemplateType("json");
|
||||||
|
/**
|
||||||
|
* The {@code svg} template type.
|
||||||
|
*/
|
||||||
public static TemplateType SVG = new TemplateType("svg");
|
public static TemplateType SVG = new TemplateType("svg");
|
||||||
|
/**
|
||||||
|
* The {@code xml} template type.
|
||||||
|
*/
|
||||||
public static TemplateType XML = new TemplateType("xml");
|
public static TemplateType XML = new TemplateType("xml");
|
||||||
|
/**
|
||||||
|
* The {@code txt} template type.
|
||||||
|
*/
|
||||||
public static TemplateType TXT = new TemplateType("txt");
|
public static TemplateType TXT = new TemplateType("txt");
|
||||||
|
/**
|
||||||
|
* The {@code sql} template type.
|
||||||
|
*/
|
||||||
public static TemplateType SQL = new TemplateType("sql");
|
public static TemplateType SQL = new TemplateType("sql");
|
||||||
|
|
||||||
private final String identifier_;
|
private final String identifier_;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new template type instance.
|
||||||
|
*
|
||||||
|
* @param identifier the identifier of this template type
|
||||||
|
*/
|
||||||
public TemplateType(String identifier) {
|
public TemplateType(String identifier) {
|
||||||
identifier_ = identifier;
|
identifier_ = identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the identifier for this template type
|
||||||
|
* @return the template type identifier as a string
|
||||||
|
*/
|
||||||
public String identifier() {
|
public String identifier() {
|
||||||
return identifier_;
|
return identifier_;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue