Repository: wildfly/wildfly-glow
Pull request #117: When there are files in classes/WEB-INF those are moved to the war
Author: login: ehsavoie
Created: 2025-03-14T10:43:35Z | Merged: 2025-03-17T09:14:26Z

WEB-INF dir


Diff:
diff --git a/cli/src/main/java/org/wildfly/glow/jbang/JBangIntegration.java b/cli/src/main/java/org/wildfly/glow/jbang/JBangIntegration.java
index 4564deeb..6b1a6a4c 100644
--- a/cli/src/main/java/org/wildfly/glow/jbang/JBangIntegration.java
+++ b/cli/src/main/java/org/wildfly/glow/jbang/JBangIntegration.java
@@ -50,6 +50,7 @@ public class JBangIntegration {
 
     private static final Pattern MAVEN_JAR_PATTERN = Pattern.compile(
             "([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+):jar:([a-zA-Z0-9_.-]+)");
+    private static final String WEB_INF = "WEB-INF";
 
     private static void extractJar(Path jar, Path destDir) throws IOException {
         try (FileSystem fs = FileSystems.newFileSystem(jar, null)) {
@@ -80,7 +81,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
     private static Path toWar(Path appClasses, Set<Path> libs) throws IOException {
         System.out.println("Adding libs to WAR: " + libs);
         Path parent = appClasses.getParent();
-        Path webInf = parent.resolve("WEB-INF");
+        Path webInf = parent.resolve(WEB_INF);
         Path webClassesDir = webInf.resolve("classes");
         Path webLibDir = webInf.resolve("lib");
 
@@ -89,7 +90,13 @@ private static Path toWar(Path appClasses, Set<Path> libs) throws IOException {
         Files.walkFileTree(appClasses, new SimpleFileVisitor<Path>() {
             @Override
             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
-                Path targetPath = webClassesDir.resolve(appClasses.relativize(file));
+                Path filePath = appClasses.relativize(file);
+                Path targetPath;
+                if (filePath.startsWith(WEB_INF) && filePath.getNameCount() > 1) {
+                    targetPath = webInf.resolve(filePath.subpath(1, filePath.getNameCount()));
+                } else {
+                    targetPath = webClassesDir.resolve(filePath);
+                }
                 Files.createDirectories(targetPath.getParent());
                 Files.copy(file, targetPath, StandardCopyOption.REPLACE_EXISTING);
                 return FileVisitResult.CONTINUE;
diff --git a/tests/run-jbang-tests.sh b/tests/run-jbang-tests.sh
index 56e67bd0..49c521ce 100755
--- a/tests/run-jbang-tests.sh
+++ b/tests/run-jbang-tests.sh
@@ -140,6 +140,58 @@ EOF
   rm $java_source_file
 }
 
+
+# Test a simple Jakarta RS application
+# without any 3rd-party dependencies
+# with a jboss-web.xml
+# (all deps are provided by WildFly)
+function testAppWithWebInf {
+  test_count=$((test_count + 1))
+  java_source_file=$test_dir/appWithWebInf.java
+  jboss_web_file=$test_dir/jboss-web.xml
+  cat <<-EOF >$java_source_file
+///usr/bin/env jbang "\$0" "\$@" ; exit $?
+//JAVA ${jdk_version}
+//DEPS org.wildfly.bom:wildfly-expansion:${wildfly_latest_version}@pom
+//DEPS org.wildfly.glow:wildfly-glow:${glow_version}
+//DEPS jakarta.ws.rs:jakarta.ws.rs-api
+//DEPS jakarta.enterprise:jakarta.enterprise.cdi-api
+//FILES WEB-INF/jboss-web.xml=jboss-web.xml
+//GLOW --server-version=${wildfly_latest_version}
+
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.ApplicationPath;
+import jakarta.ws.rs.core.Application;
+
+@ApplicationPath("/")
+public class appWithWebInf extends Application {
+
+    @Path("/hello")
+    @ApplicationScoped
+    public static class Hello {
+
+        @GET
+        public String sayHello() {
+            return "Hello, WildFly!";
+        }
+
+    }
+}
+EOF
+cat <<-EOF >$jboss_web_file
+<?xml version="1.0"?>
+<jboss-web>
+   <context-root>/bank</context-root>
+</jboss-web>
+EOF
+  jbangBuild $java_source_file
+  jbangRun $java_source_file "check_endpoint_response 'http://localhost:8080/bank/hello' 'Hello, WildFly!'"
+
+  rm $java_source_file
+}
+
 # Test a simple Jakarta RS application
 # with health enabled using //GLOW --add-ons=health
 function testAppWithHealth {
@@ -327,6 +379,7 @@ echo "Running tests with JDK ${jdk_version}"
 setupTestSuite
 
 testApp
+testAppWithWebInf
 testAppWithLib
 testAppWithHealth
 testAppWithMetrics


Reviews:
user: login: jmesnil
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-14T10:56:00Z
user: login: ehsavoie
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-14T11:01:53Z
user: login: jmesnil
body: could you add a test to https://github.com/wildfly/wildfly-glow/blob/main/tests/run-jbang-tests.sh for this change please?
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-03-14T15:23:48Z
user: login: jmesnil
body: awesome, thanks!
state: APPROVED
author_association: MEMBER
submitted_at: 2025-03-14T16:30:17Z

Review comments:
diff_hunk: @@ -234,6 +241,10 @@ public static Map<String, Object> postBuild(Path appClasses, Path pomFile,
                 .filter(s -> s.startsWith("//GLOW "))
                 .map(s -> s.substring(6).strip())
                 .collect(Collectors.reducing((s1, s2) -> s1 + " " + s2));
+        Optional<String> glowWarArgs = comments.stream()
+                .filter(s -> s.startsWith("//GLOW-WAR "))
path: cli/src/main/java/org/wildfly/glow/jbang/JBangIntegration.java
user: login: jmesnil
body: what's that used for?
created_at: 2025-03-14T10:55:11Z
author_association: MEMBER
diff_hunk: @@ -234,6 +241,10 @@ public static Map<String, Object> postBuild(Path appClasses, Path pomFile,
                 .filter(s -> s.startsWith("//GLOW "))
                 .map(s -> s.substring(6).strip())
                 .collect(Collectors.reducing((s1, s2) -> s1 + " " + s2));
+        Optional<String> glowWarArgs = comments.stream()
+                .filter(s -> s.startsWith("//GLOW-WAR "))
path: cli/src/main/java/org/wildfly/glow/jbang/JBangIntegration.java
user: login: ehsavoie
body: leftover that i didn't see 
created_at: 2025-03-14T11:01:53Z
author_association: MEMBER

PR comments:
user: login: ehsavoie
created_at: 2025-03-14T16:10:09Z
body: @jmesnil test added
author_association: MEMBER
user: login: jfdenise
created_at: 2025-03-17T09:14:42Z
body: @ehsavoie and @jmesnil Thanks. Merged.
author_association: CONTRIBUTOR