2

I'm trying to get Maven to automatically download all dependencies for jetty-start so I run this:

java start.jar etc/jetty.xml

But when I do this:

java start.jar --list-options

I get several missing modules that I have to manually add as dependencies to my Maven file. I've tried adding them, and I get stuck on finding an appropriate version of servlet-api that will provide javax.servlet.http.HttpServletResponse, even though the jar downloaded for jetty-servlet has javax/servlet/http/HttpServletResponse.class inside it. Here is the error:

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.eclipse.jetty.start.Main.invokeMain(Main.java:457)
        at org.eclipse.jetty.start.Main.start(Main.java:602)
        at org.eclipse.jetty.start.Main.main(Main.java:82)
Caused by: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletResponse
        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
        at java.lang.Class.getConstructor0(Class.java:2699)
        at java.lang.Class.newInstance0(Class.java:326)
        at java.lang.Class.newInstance(Class.java:308)
        at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:333)
        at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:291)
        at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1203)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1138)
        ... 7 more
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletResponse
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        ... 17 more

I've tried adding the org.eclipse.jetty.aggregate:jetty-all package to my dependency list, but the packages inside it are not detected by java start.jar --list-options, and therefore don't work.

There are a couple of pages of documentation that are helpful, but don't answer this question specifically:

Neil
  • 24,551
  • 15
  • 60
  • 81

4 Answers4

6

You don't.

The jetty-start artifact is for use with the jetty distribution. There is no automatic download of dependencies when using the jetty-start artifact because the assumption is that you have the distribution on disk locally and are just trying to knit together your classpath for the server start.

With maven you use the jetty-maven-plugin if your looking to start a webapp during your build for testing or build purposes. With that plugin usage you will get the lionshare of the dependencies needed unless your trying to do something specific that demands additional dependencies in which case you add them to the section of the plugin declaration.

cheers!

jesse mcconnell
  • 7,102
  • 1
  • 22
  • 33
  • The Solr project gets all required Jetty libraries using Ivy, so there should be a way to do this with Maven. Although right now I'm using the maven-antrun-plugin to use Ivy to get the libraries. It seems like quite the hackjob. – Neil May 03 '12 at 02:24
  • the jetty-start artifact is not the main entry point into jetty unless you using a distribution style layout, in which case your typically not using jetty via maven. the most common usage of jetty and maven is either through the jetty-maven-plugin or using it embedded where you create the Server object in your java code and then use maven dependencies to manage the classpath. in that case use jetty-server or jetty-webapp artifacts as a starting point and get most the dependencies transitively...or just use a jetty-aggregate as a dependency. start is not a typical start point with maven – jesse mcconnell May 03 '12 at 03:41
  • if your heart is set on using jetty-start though, look at the start.config file inside of the start.jar for an idea on how that mechanism works. – jesse mcconnell May 03 '12 at 03:42
3

You need both jetty-start and jetty-all to do what you are trying to do - though you can also use a more specific set of artifacts than jetty-all as well.

Note that with a series of artifacts on the classpath you can't use java -jar, so instead will need to run org.eclipse.jetty.start.Main directly

For example:

java -cp start.jar:jetty-all.jar org.eclipse.jetty.start.Main --list-options

You probably want to create an assembly containing the required JARs (which are retrieved as Maven dependencies), and then add a start script to point to them.

You can find a more complete example here, which uses the assembly plugin and the Java Service Wrapper to run Jetty: http://svn.apache.org/viewvc/archiva/trunk/archiva-jetty/pom.xml?revision=1307001&view=markup

Brett Porter
  • 5,827
  • 27
  • 25
  • The `java -cp start.jar:jetty-all.jar org.eclipse.jetty.start.Main --list-options` line doesn't list any options, even when I can clearly find all the relevant classes with `jar -tf jetty-all.jar`. – Neil May 03 '12 at 03:18
  • doesn't list any options - is the output an exception, or empty output? It's worth nothing that command line assumes configuration files exist in `./etc`, if they are elsewhere you need to pass them before the --list-options command – Brett Porter May 07 '12 at 14:48
0

It may not answer your question, but here's how I launch Jetty in a main() method. I create an assembled app with the .war file included, and scan the filesystem to find the war, and launch Jetty with the copy of the war that was built and included in the distribution zip file.

    String warLocation = locateWarFile();

    WebAppContext webapp = new WebAppContext();
    webapp.setWar(warLocation);

    webapp.setContextPath("/");

    webapp.setConfigurations(new Configuration[] {
            new WebInfConfiguration(),
            new EnvConfiguration(),
            new WebXmlConfiguration(),
    });

    // launch Jetty's web server and serve requests..
    Server server = new Server();
    Connector connector=new SelectChannelConnector();
    connector.setPort(Integer.getInteger("jetty.port",8080));
    server.setConnectors(new Connector[]{connector});
    server.setHandler(webapp);
    server.start();

for the record, my maven dependencies:

    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-util</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-naming</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-plus</artifactId>
    </dependency>
ianpojman
  • 1,743
  • 1
  • 15
  • 20
  • This can't work for me. My application is Solr, and uses Solr Maven dependencies to download other parts of the application that I didn't make (admin page, javascript, images) and packages it into a war file. I can't write an application and start Jetty manually, I have to use jetty-start. – Neil May 03 '12 at 03:24
-1

The following POM file is configured to download and run the Apache Solr web application, using Jetty:

<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.myspotontheweb</groupId>
    <artifactId>solr</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr</artifactId>
            <version>3.6.0</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-velocity</artifactId>
            <version>3.6.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.3.v20120416</version>
                <configuration>
                    <systemProperties>
                        <systemProperty>
                            <name>solr.solr.home</name>
                            <value>${basedir}/src/main/solr/home</value>
                        </systemProperty>
                        <systemProperty>
                            <name>solr.data.dir</name>
                            <value>${project.build.directory}/solr/data</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>run-exploded</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Application is launched from Maven as follows:

mvn compile

For completeness

Solr requires additional configuration files. See the doco for details.

$ tree
.
|-- pom.xml
`-- src
    `-- main
        `-- solr
            `-- home
                `-- conf
                    |-- admin-extra.html
                    |-- elevate.xml
                    |-- mapping-FoldToASCII.txt
                    |-- mapping-ISOLatin1Accent.txt
                    |-- protwords.txt
                    |-- schema.xml
                    |-- scripts.conf
                    |-- solrconfig.xml
                    |-- spellings.txt
                    |-- stopwords_en.txt
                    |-- stopwords.txt
                    `-- synonyms.txt
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • I know using `mvn jetty:run-exploded` will run Jetty, but I'm trying to figure out how to package the application as a jar. I don't think it's possible. – Neil May 02 '12 at 22:03
  • @Neil It's possible. Checkout the following posting: http://open.bekk.no/embedded-jetty-7-webapp-executable-with-maven/ – Mark O'Connor May 02 '12 at 22:27
  • I don't see how this blog post will help me get jetty-start working with Maven. Is there any way I can have Maven package up a war file with Jetty embedded, that starts without writing extra code, much in the way that `mvn jetty:run-exploded` will run it on the spot? – Neil May 03 '12 at 02:15
  • @Neil you need to google for "Executable War" or "Executable web app". Here's a related stackoverflow, although the solution is ANT based: http://stackoverflow.com/questions/2458440/executable-war-file-that-starts-jetty-without-maven – Mark O'Connor May 03 '12 at 07:01