Skip to content

Commit

Permalink
WarRunner: Replace Tomcat with Jetty
Browse files Browse the repository at this point in the history
Fixes #979
  • Loading branch information
earldouglas committed Nov 16, 2024
1 parent b55e542 commit 30164f3
Show file tree
Hide file tree
Showing 41 changed files with 1,332 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
with:
java-version: 11
java-version: 17
- run: sbt javafmtCheckAll scalafmtCheckAll "scalafixAll --check" test publishLocal scripted
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,13 @@ $ git push --tags origin

Update the [Giter8 template](https://github.com/earldouglas/sbt-war.g8)
to use the new version.

## Reference

* Servlet version history:
<https://en.wikipedia.org/wiki/Jakarta_Servlet#History>
* Jetty version history:
<https://en.wikipedia.org/wiki/Jetty_(web_server)#History>
* Tomcat version history:
<https://en.wikipedia.org/wiki/Apache_Tomcat#Releases>
<https://tomcat.apache.org/whichversion.html>
50 changes: 37 additions & 13 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ lazy val warRunner_3_0 =
Compile / compile / javacOptions ++=
Seq(
"-source",
"11",
"17",
"-target",
"11",
"17",
"-g:lines"
),
crossPaths := false, // exclude Scala suffix from artifact names
autoScalaLibrary := false, // exclude scala-library from dependencies
libraryDependencies += "com.github.jsimone" % "webapp-runner" % "7.0.91.0"
libraryDependencies += "org.eclipse.jetty" % "jetty-webapp" % "8.1.22.v20160922"
)

lazy val warRunner_3_1 =
Expand All @@ -39,14 +39,15 @@ lazy val warRunner_3_1 =
Compile / compile / javacOptions ++=
Seq(
"-source",
"11",
"17",
"-target",
"11",
"17",
"-g:lines"
),
crossPaths := false, // exclude Scala suffix from artifact names
autoScalaLibrary := false, // exclude scala-library from dependencies
libraryDependencies += "com.heroku" % "webapp-runner" % "8.5.68.1"
libraryDependencies += "com.heroku" % "webapp-runner" % "8.5.68.1",
libraryDependencies += "org.eclipse.jetty" % "jetty-webapp" % "9.4.56.v20240826"
)

lazy val warRunner_4_0 =
Expand All @@ -58,14 +59,35 @@ lazy val warRunner_4_0 =
Compile / compile / javacOptions ++=
Seq(
"-source",
"11",
"17",
"-target",
"11",
"17",
"-g:lines"
),
crossPaths := false, // exclude Scala suffix from artifact names
autoScalaLibrary := false, // exclude scala-library from dependencies
libraryDependencies += "com.heroku" % "webapp-runner" % "9.0.96.0"
libraryDependencies += "com.heroku" % "webapp-runner" % "9.0.96.0",
libraryDependencies += "org.eclipse.jetty" % "jetty-webapp" % "10.0.24"
)

lazy val warRunner_5_0 =
project
.in(file("runners/5.0"))
.settings(
name := "war-runner",
version := version.value + "_5.0",
Compile / compile / javacOptions ++=
Seq(
"-source",
"17",
"-target",
"17",
"-g:lines"
),
crossPaths := false, // exclude Scala suffix from artifact names
autoScalaLibrary := false, // exclude scala-library from dependencies
libraryDependencies += "com.heroku" % "webapp-runner" % "10.1.31.0",
libraryDependencies += "org.eclipse.jetty" % "jetty-webapp" % "11.0.24"
)

lazy val warRunner_6_0 =
Expand All @@ -77,14 +99,15 @@ lazy val warRunner_6_0 =
Compile / compile / javacOptions ++=
Seq(
"-source",
"11",
"17",
"-target",
"11",
"17",
"-g:lines"
),
crossPaths := false, // exclude Scala suffix from artifact names
autoScalaLibrary := false, // exclude scala-library from dependencies
libraryDependencies += "com.heroku" % "webapp-runner" % "10.1.31.0"
libraryDependencies += "com.heroku" % "webapp-runner" % "10.1.31.0",
libraryDependencies += "org.eclipse.jetty.ee10" % "jetty-ee10-webapp" % "12.0.15"
)

lazy val sbtWar =
Expand Down Expand Up @@ -113,7 +136,8 @@ lazy val sbtWar =
warRunner_3_0,
warRunner_3_1,
warRunner_4_0,
warRunner_6_0
warRunner_5_0,
warRunner_6_0,
)

// Publish to Sonatype, https://www.scala-sbt.org/release/docs/Using-Sonatype.html
Expand Down
27 changes: 22 additions & 5 deletions runners/3.0/src/main/java/com/earldouglas/WarRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.earldouglas;

import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class WarRunner {

/**
Expand All @@ -12,11 +19,21 @@ public static void main(final String[] args) throws Exception {

final WarConfiguration warConfiguration = WarConfiguration.load(args[0]);

final String[] warRunnerArgs =
new String[] {
"--port", Integer.toString(warConfiguration.port), warConfiguration.warFile.getPath(),
};
final Path warPath =
Paths
.get(warConfiguration.warFile.getPath())
.toAbsolutePath()
.normalize();

final Server server = new Server(warConfiguration.port);

final WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(warPath.toUri().toASCIIString());

server.setHandler(webapp);

webapp.runner.launch.Main.main(warRunnerArgs);
server.start();
server.join();
}
}
23 changes: 23 additions & 0 deletions runners/3.0/src/test/fakeproject/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<display-name>Hello, Servlet 3.0</display-name>

<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.earldouglas.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>
21 changes: 21 additions & 0 deletions runners/3.0/src/test/java/com/earldouglas/HelloServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.earldouglas;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

public class HelloServlet extends HttpServlet {

@Override
protected void doGet(
final HttpServletRequest request,
final HttpServletResponse response
) throws IOException {
response.setContentType("text/plain");
response.getWriter().println("Hello, world!");
}
}
41 changes: 38 additions & 3 deletions runners/3.0/src/test/scala/com/earldouglas/WarRunnerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class WarRunnerTest

override def beforeAll(): Unit = {

new com.earldouglas.HelloServlet()

val thread: Thread =
new Thread {
override def run(): Unit = {
Expand Down Expand Up @@ -44,7 +46,7 @@ class WarRunnerTest
awaitOpen(8988)
}

test("/foo.html") {
test("GET /foo.html") {

val expected: HttpClient.Response =
HttpClient.Response(
Expand All @@ -71,7 +73,7 @@ class WarRunnerTest
) shouldBe expected
}

test("/bar.html") {
test("GET /bar.html") {

val expected: HttpClient.Response =
HttpClient.Response(
Expand All @@ -98,7 +100,7 @@ class WarRunnerTest
) shouldBe expected
}

test("/baz/raz.css") {
test("GET /baz/raz.css") {

val expected: HttpClient.Response =
HttpClient.Response(
Expand All @@ -124,4 +126,37 @@ class WarRunnerTest
}
) shouldBe expected
}

test("GET /hello") {

val expected: HttpClient.Response =
HttpClient.Response(
status = 200,
headers = Map(
"Content-Type" -> "text/plain"
),
body = """|Hello, world!
|""".stripMargin
)

val obtained: HttpClient.Response =
HttpClient.request(
method = "GET",
url = "http://localhost:8988/hello",
headers = Map.empty,
body = None
)

obtained.copy(
headers =
obtained
.headers
.filter { case (k, _) =>
k == "Content-Type"
}
.map { case (k, v) =>
(k, v.replaceAll(";charset=.*", ""))
}
) shouldBe expected
}
}
27 changes: 22 additions & 5 deletions runners/3.1/src/main/java/com/earldouglas/WarRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.earldouglas;

import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class WarRunner {

/**
Expand All @@ -12,11 +19,21 @@ public static void main(final String[] args) throws Exception {

final WarConfiguration warConfiguration = WarConfiguration.load(args[0]);

final String[] warRunnerArgs =
new String[] {
"--port", Integer.toString(warConfiguration.port), warConfiguration.warFile.getPath(),
};
final Path warPath =
Paths
.get(warConfiguration.warFile.getPath())
.toAbsolutePath()
.normalize();

final Server server = new Server(warConfiguration.port);

final WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(warPath.toUri().toASCIIString());

server.setHandler(webapp);

webapp.runner.launch.Main.main(warRunnerArgs);
server.start();
server.join();
}
}
23 changes: 23 additions & 0 deletions runners/3.1/src/test/fakeproject/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<display-name>Hello, Servlet 3.1</display-name>

<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.earldouglas.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>
21 changes: 21 additions & 0 deletions runners/3.1/src/test/java/com/earldouglas/HelloServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.earldouglas;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

public class HelloServlet extends HttpServlet {

@Override
protected void doGet(
final HttpServletRequest request,
final HttpServletResponse response
) throws IOException {
response.setContentType("text/plain");
response.getWriter().println("Hello, world!");
}
}
Loading

0 comments on commit 30164f3

Please sign in to comment.