From 2fc3f0400bddd07d0c152ac1dd6e6c599e3dd7c7 Mon Sep 17 00:00:00 2001
From: Kai Reinhard <k.reinhard@micromata.de>
Date: Mon, 17 Feb 2025 06:59:37 +0000
Subject: [PATCH] gradle...

---
 build.gradle.kts                   |    2 
 .gitignore                         |    1 
 borgbutler-webapp/build.gradle.kts |  127 +++++++++++++++++++++++++++++++++---------
 borgbutler-server/build.gradle.kts |    2 
 4 files changed, 102 insertions(+), 30 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2397a67..d32c5e9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -40,6 +40,7 @@
 borgbutler-server/build
 borgbutler-server/out
 borgbutler-webapp/build
+borgbutler-webapp/node
 borgbutler-webapp/node_modules
 borgbutler-docker/app/target
 borgbutler-server-*
diff --git a/borgbutler-server/build.gradle.kts b/borgbutler-server/build.gradle.kts
index 94a99f6..c350948 100644
--- a/borgbutler-server/build.gradle.kts
+++ b/borgbutler-server/build.gradle.kts
@@ -61,7 +61,7 @@
 }
 
 tasks.named("distZip", Zip::class) {
-    dependsOn(":borgbutler-webapp:packageWebApp")
+    dependsOn(":borgbutler-webapp:webAppJar")
 }
 
 tasks.register("dist") {
diff --git a/borgbutler-webapp/build.gradle.kts b/borgbutler-webapp/build.gradle.kts
index 37cc7e0..a5c78c0 100644
--- a/borgbutler-webapp/build.gradle.kts
+++ b/borgbutler-webapp/build.gradle.kts
@@ -1,29 +1,100 @@
+plugins {
+    id("com.github.node-gradle.node") version "7.1.0"
+    id("base") // Fügt die 'clean'-Task hinzu
+}
+
+node {
+    // Configure the Node.js and npm versions
+    version.set("23.1.0")
+    // Version of npm to use
+    // If specified, installs it in the npmWorkDir
+    npmVersion.set("10.9.0")
+    // npmVersion.set("") // If empty, the plugin will use the npm command bundled with Node.js
+    download.set(true) // Downloads Node.js and npm instead of using a globally installed version
+
+    // Set the directories for Node.js and npm installations using the modern Gradle API
+    // Use a persistent directory outside the `build` folder
+    workDir.set(layout.projectDirectory.dir("node/nodejs")) // Directory for Node.js installation
+    npmWorkDir.set(layout.projectDirectory.dir("node/npm")) // Directory for npm installation
+    // Explicitly set the Node.js binary path
+    nodeProjectDir.set(file(layout.projectDirectory.dir(".").asFile.absolutePath))
+}
+
+tasks.named<Delete>("clean") {
+    delete(
+        file("node"),  // Delete download directory of node and npm.
+        file("node_modules"),
+        layout.buildDirectory
+    )
+}
+
+tasks {
+    // Configure the existing npmInstall task instead of registering a new one
+    named<com.github.gradle.node.npm.task.NpmTask>("npmInstall") {
+        group = "build"
+        description = "Installs npm dependencies"
+        args.set(listOf("install"))
+        // Skip task if node_modules exists
+        val nodeModulesDir = layout.projectDirectory.dir("node_modules")
+        onlyIf {
+            !nodeModulesDir.asFile.exists()
+        }
+        outputs.dir(project.layout.projectDirectory.dir("node_modules"))
+    }
+
+    register<com.github.gradle.node.npm.task.NpmTask>("npmBuild") {
+        group = "build"
+        description = "Builds the React project"
+        args.set(listOf("run", "build"))
+        dependsOn("npmInstall")
+
+        inputs.files(fileTree("src")) // All source files as input
+        outputs.dir("build") // React output directory as output
+    }
+
+    register<Copy>("copyReactBuild") {
+        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
+        group = "build"
+        description = "Copies built React files to the target directory"
+        dependsOn("npmBuild") // Depends on the React build process
+        from(file("build")) {
+            // Exclude the target directory to prevent recursion
+            exclude("resources/main/static/**")
+        }
+        from(file("src")) {
+            include("index.html")
+        }
+        into(layout.buildDirectory.dir("resources/main/static")) // Target directory in the Gradle project
+        // Skip task if target directory is up-to-date
+        inputs.dir("build") // React build directory as input
+        outputs.dir(layout.buildDirectory.dir("resources/main/static")) // Static resources directory as output
+    }
+
+    register<Jar>("webAppJar") {
+        group = "build"
+        description = "Package React build output as a JAR"
+        archiveBaseName.set("borgbutler-webapp")
+        archiveVersion.set(project.version.toString())
+        destinationDirectory.set(layout.buildDirectory.dir("libs"))
+
+        // Include files from react-build directory
+        from(layout.buildDirectory.get()) {
+            into("static") // Place files under /static in the JAR
+            exclude("resources")
+            exclude("libs")
+            exclude("tmp")
+        }
+        // Include additional files (e.g., src/index.html)
+        from(layout.projectDirectory.file("src/index.html")) {
+            into("static") // Copy index.html to /static
+        }
+        dependsOn("copyReactBuild") // Ensure React build and copy are done first
+    }
+
+    // Include the React build in the Gradle build process
+    named("build") {
+        dependsOn("copyReactBuild") // Makes React build a part of the Gradle build
+    }
+}
+
 description = "borgbutler-webapp"
-
-tasks.register<Exec>("npmBuild") {
-    workingDir = projectDir
-    commandLine("sh", "-c", "npm run build")
-}
-
-tasks.register<Zip>("packageWebApp") {
-    outputs.upToDateWhen { false } // Immer ausführen, damit nichts fehlt
-    dependsOn("npmBuild")
-
-    archiveBaseName.set("borgbutler-webapp")
-    archiveExtension.set("jar")
-    destinationDirectory.set(projectDir)
-
-    from("build") {
-        into("webapp") // Statische Ressourcen in webapp-Verzeichnis
-    }
-
-    doLast {
-        val jarFile = archiveFile.get().asFile
-        val targetDir = buildDir.resolve("libs")
-
-        mkdir(targetDir)
-        ant.invokeMethod("move", mapOf("file" to jarFile.absolutePath, "todir" to targetDir.absolutePath))
-
-        println("*** packageWebApp finished.")
-    }
-}
diff --git a/build.gradle.kts b/build.gradle.kts
index 4b4c461..a330cc7 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -8,7 +8,7 @@
 
 allprojects {
     group = "de.micromata.borgbutler"
-    version = "0.8"
+    version = "0.9"
 }
 
 subprojects {

--
Gitblit v1.10.0