Deploying war file using xml on jetty9

I am trying to deploy a war file on jetty9, it works fine if I drop it into webapps folder but I want to deploy it using a xml file.

Here’s the exception I am getting:

2015-10-22 17:36:39.593:WARN:oejw.WebAppContext:main: Failed startup of context [email protected]{/myapp,file:///tmp/jetty-openbd.war-_myapp-any-1881803922382581907.dir/webapp/,null}{/opt/openbd.org/openbd.war}
java.lang.NullPointerException
    at org.eclipse.jetty.webapp.MetaInfConfiguration.preConfigure(MetaInfConfiguration.java:77)
    at org.eclipse.jetty.webapp.WebAppContext.preConfigure(WebAppContext.java:474)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:510)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1510)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1435)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.eclipse.jetty.start.Main.invokeMain(Main.java:214)
    at org.eclipse.jetty.start.Main.start(Main.java:457)
    at org.eclipse.jetty.start.Main.main(Main.java:75)

and my xml :

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
   "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/myapp</Set>
  <Set name="war">/opt/openbd.org/openbd.war</Set>
</Configure>

I tried with <Set name="extractWAR">true</Set> with no luck.

I also used --debug which didn’t provide any clue..

What am I doing wrong? Jetty version: jetty-9.3.3.v20150827

Answer

So for your scenario you’ll need to understand how ${jetty.base} and ${jetty.home} operate.

Step 1: The ${jetty.home} directory

Download and unpack the jetty distribution, anywhere, to any path you want to.

This is now your ${jetty.home} directory.

These are the most important facts about ${jetty.home}

  • Don’t edit it.
  • Don’t change a thing.
  • Don’t delete anything.
  • Leave it alone.

You can have multiple ${jetty.home} directories on your machine if you want. They don’t step on each other or cause any problems.

Step 2: The ${jetty.base} directory

Create a new directory OUTSIDE of the ${jetty.home} directory.

This new, now blank, directory is the start of your ${jetty.base}.

A ${jetty.base} represent the configuration for a specific instance of Jetty. You can have multiple ${jetty.base} directories as well. Each directory represent a unique instance of a running Jetty.

The fundamental configuration for a ${jetty.base} is the start.ini which is a set of command line arguments used to start jetty. (Similar to how all of the Eclipse RT projects out there work)

To initialize your ${jetty.base} using command line tooling you can utilize the ${jetty.home}/start.jar and its --add-to-start=<module> facilities.

Example:

$ cd /path/to/my.base
$ java -jar /path/to/jetty-dist/start.jar --add-to-start=http,deploy,websocket

Congrats you just initialized your ${jetty.base} directory.

Step 3: Adding your WebApps

Since you chose the deploy module in the above step, you now have a ${jetty.base}/webapps which is used by Jetty to auto-deploy the various web applications found within.

See the documentation on the Deployment Architecture to understand what it looks for and how it deploys.

In your case, just put your myapp.xml into the ${jetty.base}/webapps/ directory.

Note: Using the deploy module as-is, will only monitor and deploy from the ${jetty.base}/webapps/ directory. No other directory is looked in, or monitored for web applications. Not even the ${jetty.home}/webapps/ directory.

The monitored directory can be configured via a property in your ${jetty.base}/start.ini. Simply set jetty.deploy.monitoredDir=<relative-path> and this new path will be monitored (no editing of jetty-deploy.xml required). The monitored directory is configured to always be under the specified ${jetty.base} directory. Seeing as the ${jetty.base} directory can be anywhere on your filesystem, there should be no issues with using the ${jetty.base} facilities.

Step 4: Run Jetty

This isn’t that hard either.

$ cd /path/to/my.base
$ java -jar /path/to/jetty-dist/start.jar

Congrats you have a running jetty.

Step 5: (Bonus) Listing the Configuration

There’s 2 ways to see what the configuration looks like.

  1. See the server configuration, before you start jetty.
$ cd /path/to/my.base
$ java -jar /path/to/jetty-dist/start.jar --list-config
  1. See the server state after it is fully started.
$ cd /path/to/my.base
$ java -jar /path/to/jetty-dist/start.jar jetty.server.dumpAfterStart=true

Leave a Reply

Your email address will not be published. Required fields are marked *