2
0
Fork 0
mirror of https://github.com/ethauvin/rife2-hello.git synced 2025-04-25 15:37:10 -07:00

Rework Gradle build

This commit introduces a _convention plugin_ for RIFE2 support.
It provides a number of advantages:

- the build logic is clearly separated from the build script,
which now only contains user-specific configuration, like the
framework version or how to configure test logging
- it fixes a number of issues like hardcoded dependencies
(`dependsOn` is in general a mistake)
- it removes the need to resolve the agent path eagerly
- it makes the agent configurable
- it clearly separates the user classpath from the RIFE
classpath (both for precompiling templates and for the agent)
- template compilation is cached
- it avoids the abuse of `src/main/resources` to put
templates and uses a dedicated directory instead, which
removes the need for exclusions

In addition, this should make it relatively straightforward
to convert the convention plugin into a proper RIFE2 plugin,
by extracting the code in `build-logic` into its own
repository then publishing to the Gradle plugin portal.
This commit is contained in:
Cedric Champeau 2023-02-21 23:33:54 +01:00
parent 3a9bbec851
commit 2e025cd693
9 changed files with 284 additions and 77 deletions

View file

@ -2,7 +2,8 @@ import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
plugins {
java
application
id("com.uwyn.rife2")
}
base {
@ -10,25 +11,22 @@ base {
version = 1.0
}
application {
mainClass.set("hello.App")
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots") } // only needed for SNAPSHOT
}
sourceSets {
main {
runtimeClasspath = files(file("src/main/resources"), runtimeClasspath);
}
}
sourceSets.main {
resources.exclude("templates/**")
rife2 {
version.set("1.3.0")
useAgent.set(true)
}
dependencies {
implementation("com.uwyn.rife2:rife2:1.3.0")
runtimeOnly("com.uwyn.rife2:rife2:1.3.0:agent")
runtimeOnly("org.eclipse.jetty:jetty-server:11.0.13")
runtimeOnly("org.eclipse.jetty:jetty-servlet:11.0.13")
runtimeOnly("org.slf4j:slf4j-simple:2.0.5")
@ -38,68 +36,11 @@ dependencies {
}
tasks {
val dependencies = configurations
.runtimeClasspath.get().files;
val rifeAgentJar = dependencies
.filter { it.toString().contains("rife2") }
.filter { it.toString().endsWith("-agent.jar") }[0]
test {
jvmArgs = listOf("-javaagent:$rifeAgentJar")
useJUnitPlatform()
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
}
// Pre-compile the RIFE2 templates to bytecode for deployment
register<JavaExec>("precompileHtmlTemplates") {
classpath = sourceSets["main"].runtimeClasspath
mainClass.set("rife.template.TemplateDeployer")
args = listOf(
"-verbose",
"-t", "html",
"-d", "${projectDir}/build/classes/java/main",
"-encoding", "UTF-8", "${projectDir}/src/main/resources/templates"
)
}
register("precompileTemplates") {
dependsOn("precompileHtmlTemplates")
}
// Ensure that the templates are pre-compiled before building the jar
jar {
dependsOn("precompileTemplates")
}
// Replace the run task with one that uses the RIFE2 agent
register<JavaExec>("run") {
classpath = sourceSets["main"].runtimeClasspath
mainClass.set("hello.App")
jvmArgs = listOf("-javaagent:$rifeAgentJar")
}
// These two tasks create a self-container UberJar
register<Copy>("copyWebapp") {
from("src/main/")
include("webapp/**")
into("$buildDir/webapp")
}
register<Jar>("uberJar") {
dependsOn("jar")
dependsOn("copyWebapp")
archiveBaseName.set("hello-uber")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes["Main-Class"] = "hello.AppUber"
}
val uberDependencies = dependencies
.filter { !it.toString().matches("rife2-.*agent\\.jar".toRegex()) }
.map(::zipTree)
from(uberDependencies, "$buildDir/webapp")
with(jar.get())
}
}
}