Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WarRunner: Replace Tomcat with Jetty #997

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
53 changes: 41 additions & 12 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ 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",
libraryDependencies += "org.eclipse.jetty" % "jetty-annotations" % "8.1.22.v20160922"
)

lazy val warRunner_3_1 =
Expand All @@ -39,14 +40,16 @@ 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",
libraryDependencies += "org.eclipse.jetty" % "jetty-annotations" % "9.4.56.v20240826"
)

lazy val warRunner_4_0 =
Expand All @@ -58,14 +61,37 @@ 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",
libraryDependencies += "org.eclipse.jetty" % "jetty-annotations" % "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",
libraryDependencies += "org.eclipse.jetty" % "jetty-annotations" % "11.0.24"
)

lazy val warRunner_6_0 =
Expand All @@ -77,14 +103,16 @@ 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",
libraryDependencies += "org.eclipse.jetty.ee10" % "jetty-ee10-annotations" % "12.0.15"
)

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

Expand Down
21 changes: 16 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,10 @@
package com.earldouglas;

import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class WarRunner {

/**
Expand All @@ -12,11 +17,17 @@ 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>
16 changes: 16 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,16 @@
package com.earldouglas;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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!");
}
}
39 changes: 36 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,35 @@ 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
}
}
21 changes: 16 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,10 @@
package com.earldouglas;

import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class WarRunner {

/**
Expand All @@ -12,11 +17,17 @@ 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>
16 changes: 16 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,16 @@
package com.earldouglas;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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
Loading