Question

Im reading about jetty xml from here: http://www.eclipse.org/jetty/documentation/current/jetty-xml-config.html. It states that "jetty.xml is the default configuration file for Jetty, typically located at $JETTY_HOME/etc/jetty.xml". But my question is that if i obtain jetty as a maven plugin there where can i find the xml?

Here is my pom.xml:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>jet</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>jet</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>8.1.3.v20120416</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>8.1.3.v20120416</version>
        </dependency>
    </dependencies>
</project>

and here is how i start server:

package com.example.jet;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class App {
    public static void main(String[] args) throws Exception {
        Server s = new Server(8080);
        s.setSendDateHeader(true);
        ServletContextHandler c = new ServletContextHandler(s, "/");
        c.addServlet(new ServletHolder(new MyServlet()), "/hello");
        s.setHandler(c);
        s.start();
    }
}
Was it helpful?

Solution

Since you are using embedded Jetty (not running it in Jetty) so you should put your configuration file on the classpath i.e src/main/resources, which is a standard Maven location.

According to this tutorial you should then load the resource file and initialize Jetty with it:

public class FileServerXml
{
    public static void main(String[] args) throws Exception
    {
        Resource fileserver_xml = Resource.newSystemResource("fileserver.xml");
        XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
        Server server = (Server)configuration.configure();
        server.start();
        server.join();
    }
}

The example is for a file server but I imagine it is similar for any other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top