Search code examples
javaeclipsemavenjettymaven-jetty-plugin

limit jetty scanning in maven plugin


I'm having problems getting a webapp to start quickly in using the maven jetty plugin in eclipse. I'm using the jetty:run goal.

After turning on the logging the problem seems to be that jetty scans all the jars in my webapp for web app configuration. Just including a dependency to jersey-media-moxy causes jetty to add 48 seconds to its start up time.

How do I limit this scanning in the jetty-maven-plugin? I've found Jetty startup delay due to scanning but before I make some external jetty configuration and include this is the maven configuration, I want to make sure that there isn't a more straightforward option.

http://eclipse.dev/jetty/documentation/current/quickstart-webapp.html looks promising, but I'm unsure how to proceed (as this doesn't mention the maven plugin either).

my pom.xml file is included below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my-app</groupId>
    <artifactId>my-app</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-app</name>

    <build>
        <finalName>my-app</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.9.v20150224</version>
                <configuration>
                    <war>${project.basedir}/target/my-app.war</war>
                    <stopPort>8088</stopPort>
                    <stopKey>foo</stopKey>
                    <stopWait>10</stopWait>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <!-- artifactId>jersey-container-servlet-core</artifactId -->
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <artifactId>jersey-container-servlet</artifactId>
        </dependency>
        <!-- uncomment this to get JSON support -->
        <!--  -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        <!-- -->
    </dependencies>
    <properties>
        <jersey.version>2.16</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Solution

  • This is what I use for my projects to speed up Jetty startup just from the maven pom.xml file (no external configuration is required):

            <plugin>  
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <configuration>
                    <webApp>
    
                        <!-- no need to scan anything as we're using servlet 2.5 and moreover, we're not using ServletContainerInitializer(s) -->
                        <!-- for more relevant information regarding scanning of classes refer to https://java.net/jira/browse/SERVLET_SPEC-36 -->
                        <webInfIncludeJarPattern>^$</webInfIncludeJarPattern>
                        <containerIncludeJarPattern>^$</containerIncludeJarPattern>
                        <!--<webInfIncludeJarPattern>.*/spring-[^/]*\.jar$|.*/.*jsp-api-[^/]\.jar$|./.*jsp-[^/]\.jar$|./.*taglibs[^/]*\.jar$</webInfIncludeJarPattern>-->
                    </webApp>
                </configuration>
            </plugin>
    

    Use any regex of your choice (if you're using servlet initializers). Note that ^$ excludes everything.