views:

409

answers:

2

I find it very frustrating doing web development with Maven & Jetty using Eclipse, compare with what I did using Visual Studio. Everytime I make a change, even a minor change in my view file, (*.jsp, for example), then I have to re-package the whole web -> waiting for jetty to reload everything before I can see the change.

Is there any better way to do that, some thing like an automatically plugin that will picked that changed files and deploy the changed files to web server?

A: 

Haven't used Jetty with Eclipse, but if you use Tomcat (and I assume Jetty would work as well) with WTP and the m2eclipse plugin, Eclipse will build and publish your web application every time a resource is saved.

  1. Create (or use existing) Maven project with 'war' packaging.

  2. In the Eclipse "servers" view, right-click and set up a server.

  3. Right click on the configured server, and choose "Add/Remove projects", and select your project.

  4. Click on the green triangle in the servers view to start up the server.

Now Eclipse should auto-build-and-publish your Web application every time you make a change. Note, you must have the m2eclipse plugin AND the m2eclipse WTP integration plugin for this to work.

Will
@Will: In maven project, settings stored in side pom.xml will be used to start the server (the port, for example, by invoke mvn goal). I wonder whether it is possible with your way?
Phương Nguyễn
+4  A: 

The way you are using Maven, Jetty (and Eclipse) together is unclear but since the question is tagged Maven, I'll cover the Maven way. With a project of type war, one easy way to get the webapp up and running is to use the Maven Jetty Plugin. To do so, simply add the following snippet to your POM:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.10</version>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

With this setup, running mvn jetty:run will start a jetty container with your application deployed. Any change on a view will cause the JSP to be recompiled when requested. And to configure the jetty plugin to also watch for Java code changes, you'll have to add the scanIntervalSeconds option:

scanIntervalSeconds Optional. The pause in seconds between sweeps of the webapp to check for changes and automatically hot redeploy if any are detected. By default this is 0, which disables hot deployment scanning. A number greater than 0 enables it.

So the configuration might look like:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.10</version>
        <configuration>
          <scanIntervalSeconds>1</scanIntervalSeconds>
        </configuration>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

And if you want to be able to connect a remote debugger, have a look at these instructions.

This is how I've always used Jetty with Maven and Eclipse, and I've always been happy with this setup. I've never used the Jetty adapter for the WTP, the previous setup is just unbeatable.

Pascal Thivent
Hmm, sounds interesting. Actually, I was thinking about a separated plugin, something that can re-invoke a maven phase (like `package`) in response to file changes. The scanIntervalSeconds is a cool options. But what about another plugin that also start a different version of jetty like Google App Engine (the maven-gae-plugin, for example) that doesnot offer this option?
Phương Nguyễn
@Phuong Unfortunately, I don't have any experience with the gae plugin so I can't say much about it.
Pascal Thivent
@Pascal: I find this FileSync eclipse plugin: http://andrei.gmxhome.de/filesync/index.html which can be used to copy changed files to the deployed folder. I'm trying it right now, looks promising since it won't depend on what the maven-plugin can offer.
Phương Nguyễn
Phương Nguyễn
@Phuong Glad you find a working solution. And thanks for accepting this answer.
Pascal Thivent