1

In Rails and most modern web frameworks, I can easily develop web applications interactively by running a development server with a single simple command (rails server) and visiting the browser.

For Java WAR applications, the only way I can find to run it is to install tomcat, build, and copy the application into it's webapps folder, which requires sudo.

Is there a simpler way to run such WAR applications for development without sudo, and without copying the directory around (this is specially bad since the webapps folder varies with the installation)?

inotify based build on save would be a plus so I can just edit the files and immediately see the result after reloading the browser.

I'm not restricted to using Tomcat: any Servlet / JSP implementation capable of running WAR files would do for me.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985

2 Answers2

3

Using Maven

If you're already using Maven to build your project, then the simplest route is probably the Jetty Maven plugin.

First, add this to your pom.xml:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.2.7-SNAPSHOT</version>
</plugin>

Then you can run a Jetty server from the command line with:

mvn jetty:run

This is quite Rails-like in that it starts a server in your console. It watches for changes to you application source files or config files and reloads as necessary.

There's no issue with sudo, as you are not packaging and deploying the war file to a tomcat instance somewhere else. Instead it runs your application as an exploded war file directly from the output directory that Maven creates.

However, I appreciate that this sounds easier than it really is - the bulk of the work could well be in converting your project over to Maven...

Using Standalone Jetty with Automatic Web App Deployment

Another approach if you don't want to hand over your project to Maven is to use Jetty's automated web app deployment.

serg10
  • 31,923
  • 16
  • 73
  • 94
1

jetty-runner is the best option I could find.

Once installed with Maven, you can do:

java -jar target/dependency/jetty-runner.jar target/*.war

To install, to your pom.xml:

<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>copy</goal></goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.mortbay.jetty</groupId>
                                <artifactId>jetty-runner</artifactId>
                                <version>7.5.4.v20111024</version>
                                <destFileName>jetty-runner.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And run:

mvn package

But of course, you are not forced to use Maven with it.

This is the method recommended by Heroku: https://devcenter.heroku.com/articles/deploy-a-java-web-application-that-launches-with-jetty-runner

A question whose solution also solves this one is: Jetty Run War Using only command line

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985