From 25e8dc8e8e79c1832da93e76e20234ec06391bcf Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 18 Apr 2025 22:24:55 +0200 Subject: [PATCH] Update build scripts to integrate frontend with backend. --- backend/build.gradle.kts | 49 ++++++------------------------ frontend/build.gradle.kts | 64 +++++++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 49 deletions(-) diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index aecd490..c1aa9ee 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -2,7 +2,6 @@ plugins { java id("org.springframework.boot") version "3.4.4" id("io.spring.dependency-management") version "1.1.7" -// id("com.github.johnrengelman.shadow") version "8.1.1" } group = "com.alttd.altitudeweb" @@ -27,6 +26,7 @@ repositories { dependencies { implementation(project(":open_api")) implementation(project(":database")) + implementation(project(":frontend")) implementation("org.springframework.boot:spring-boot-starter-web") annotationProcessor("org.projectlombok:lombok") testImplementation("org.springframework.boot:spring-boot-starter-test") @@ -54,42 +54,13 @@ tasks.bootJar { archiveClassifier.set("") } +tasks.processResources { + dependsOn("includeFrontend") +} -//tasks.withType { -// mergeServiceFiles() -// dependencies { -// include(dependency("com.mysql:mysql-connector-j")) -// } -//} - -//tasks.withType { -// manifest { -// attributes["Main-Class"] = "com.alttd.altitudeweb.AltitudeWebApplication" -// } -// archiveBaseName.set("altitudeweb") -// archiveClassifier.set("") -// mergeServiceFiles() -// -// // Include everything -// from(sourceSets.main.get().output) -// -// // Include all project dependencies -// configurations = listOf(project.configurations.runtimeClasspath.get()) -// -// // Ensure MySQL is included (even though it should be part of runtimeClasspath already) -// dependencies { -// include(dependency("com.mysql:mysql-connector-j")) -// } -// -// // Enable zip64 mode for large JARs -// isZip64 = true -//} -// -//// Make the shadowJar task the default jar task -//tasks.named("jar") { -// enabled = false -//} -// -//tasks.named("assemble") { -// dependsOn("shadowJar") -//} +tasks.register("includeFrontend") { + description = "Copy the built frontend to the Spring Boot static resources directory" + from("${project.rootDir}/frontend/dist") + into(layout.buildDirectory.dir("resources/main/static")) + doNotTrackState("Cannot reliably track state in the static directory") +} diff --git a/frontend/build.gradle.kts b/frontend/build.gradle.kts index 7d6b964..d6a11ee 100644 --- a/frontend/build.gradle.kts +++ b/frontend/build.gradle.kts @@ -1,14 +1,58 @@ -tasks.register("npmInstall") { - commandLine("npm.cmd", "install") -// commandLine("npm", "install") +import com.github.gradle.node.NodeExtension +import com.github.gradle.node.npm.task.NpmTask + +plugins { + id("com.github.node-gradle.node") version "7.1.0" + id("base") // This adds the standard Gradle lifecycle tasks like "build" } -tasks.register("ngBuild") { - dependsOn("npmInstall", "generateFrontendApi") -// commandLine("npm", "run", "build") - commandLine("npm.cmd", "run", "build") +node { + download.set(true) + version.set("22.14.0") + npmVersion.set("10.9.2") + workDir.set(file("${project.projectDir}/node")) + npmWorkDir.set(file("${project.projectDir}/node")) } -//dependencies { -// implementation(project(":backend")) -//} +// Clean the distribution directory +tasks.register("cleanDist") { + description = "Clean the distribution directory" + delete("dist") +} + +// Create a task that will run npm build +tasks.register("npmBuild") { + description = "Run 'npm run build'" + group = "build" + + doLast { + // Use nodeCommand directly from the plugin + project.exec { + workingDir(project.projectDir) + + // Use node's npm to ensure it works on all environments + val nodeDir = "${project.projectDir}/node" + val isWindows = System.getProperty("os.name").lowercase().contains("windows") + + if (isWindows) { + val npmCmd = file(nodeDir).listFiles()?.find { it.name.startsWith("npm") && it.isDirectory }?.let { + "${it.absolutePath}/npm.cmd" + } ?: "$nodeDir/node_modules/npm/bin/npm.cmd" + + commandLine(npmCmd, "run", "build") + } else { + val npmExecutable = file(nodeDir).listFiles()?.find { it.name.startsWith("npm") && it.isDirectory }?.let { + "${it.absolutePath}/npm" + } ?: "$nodeDir/node_modules/npm/bin/npm" + + commandLine(npmExecutable, "run", "build") + } + } + } + + dependsOn("npmInstall") +} + +tasks.named("assemble") { + dependsOn("npmBuild") +}