Using Embedded Jetty server

Recently i started using Embedded Maven Jetty plugging during development, it makes development so easy and simple. For example take a look at Hello Embedded Server sample, which is a Sample Servlet 3.0 compliant servlet that uses pom.xml(Maven build file) like this

<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 
  ttp://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.webspherenotes.embedded</groupId>
  <artifactId>HelloEmbeddedServer</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Hello Embedded server Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>HelloEmbeddedServer</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.2.v20120308</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <webApp>
            <contextPath>/HelloEmbeddedServer</contextPath>
          </webApp>
        </configuration>
      </plugin>

    </plugins>
  </build>
</project>
Now all that i have to do is execute mvn jetty:run and it takes care of compiling the app and deploying it in Jetty server. The default configuration of this plugin would assume that your target classes are available in <projectrootdirectory>/target/classes, and read classes from that directory and use them, so we dont have to build the .war file, instead you can keep working with the expanded version. Now if you use mvn eclipse:eclipse to setup your project in the Eclipse IDE then you will notice that your project is setup to generate .class files in <projectrootdirectory>/target/classes folder like this
In my case i did setup scanIntervalSeconds attribute with value equal to 10 which tells Maven Jetty plugin to scan <projectrootdirectory>/target/classes directory and reinitialize the app if something has changed. By default whenever i make changes and save them Eclipse will recompiling the .java file and copy classes into <projectrootdirectory>/target/classes directory, and those changes would get picked up automatically in 10 seconds.

No comments:

Post a Comment