Speeding up Maven3 Jetty Plugin and Spring Application
We use Jetty Maven Plugin for local development to speed up the compile/run cycle. Our typical application is started using “mvn jetty:run” command and starts in 5-10 seconds. Recently we upgraded to Maven3 and Jetty 9 and all of the sudden Jetty restart times increased by about 30-40 seconds. Here is what we saw in the console:
2015-04-12 19:52:09.185:INFO:oejs.Server:main: jetty-9.2.7.v20150116 2015-04-12 19:52:34.072:INFO:/rcra-handler:main: No Spring WebApplicationInitializer types detected on classpath 2015-04-12 19:52:34.173:INFO:/rcra-handler:main: Initializing Spring root WebApplicationContext
So there is 25 second delay while the application is scanned for WebApplicationInitializer. I quickly figured out that I need to filter some jars, but it was not entirely clear how to configure the jetty plugin correctly. So here is what I came up with:
1. Create jetty-context.xml file and place it under your WEB-INF directory. This tells Jetty to only scan Spring, Spring Security jars and classes directory.
<?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>
2. Configure Maven Jetty Plugin to load the context file.
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.2.7.v20150116</version> <configuration> <webApp> <contextPath>/rcra-handler</contextPath> </webApp> <contextXml>${basedir}/src/main/webapp/WEB-INF/jetty-context.xml</contextXml> <useTestScope>true</useTestScope> </configuration> </plugin>