Jetty startup delay due to scanning Jetty startup delay due to scanning spring spring

Jetty startup delay due to scanning


This is due to the fact that Jetty 9 scans all Jars in the WEB-INF folder for annotations in order to start the web context. If you've attempted to find a solution to this problem, you probably already discovered that fact. I tried several such answers, but never found the correct solution among them.

In order to eliminate such scanning as much as possible, we can define a pattern that tells Jetty which sources to scan and which not to scan. This is done by either setting some configuration in maven, or by setting an attribute in the jetty-context.xml. (If you are using the maven plugin, you need to set Jetty's jetty-context.xml also in your pom.xml)

Some other solutions that have not worked for me (either no increase in startup time or no correct startup at all)

Jetty 8.1.2 startup delayjetty8 with maven plugin takes to long to start

etc.

The correct solution is also done using such jetty-context.xml, but with another pattern. In a Spring application, we need to scan the Spring jars, and this alone will already give a massive boost if you have many dependencies. Even better is if you only scan the spring-web jars instead. If you have Spring Security, then it might also be needed to include those jars.

As such, the pattern that gave me maximum speedup is shown here:

<?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"><Configure class="org.eclipse.jetty.webapp.WebAppContext">    <Call name="setAttribute">        <Arg>org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern</Arg>        <Arg>.*/spring-security[^/]*\.jar$|.*/spring-web[^/]*\.jar$|.*/classes/.*</Arg>    </Call></Configure>

We exclude anything that is not in our classes folder in WEB-INF, as well as any jars that do not include the regex pattern given.

Hope this helps someone!