【问题标题】:Location of jetty.xml in a maven project?jetty.xml 在 Maven 项目中的位置?
【发布时间】:2014-04-24 11:29:29
【问题描述】:

我正在从这里阅读有关码头 xml 的信息: http://www.eclipse.org/jetty/documentation/current/jetty-xml-config.html。 它指出“jetty.xml 是 Jetty 的默认配置文件,通常位于 $JETTY_HOME/etc/jetty.xml”。但我的问题是,如果我将 jetty 作为 maven 插件在哪里可以找到 xml?

这是我的 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>

这是我如何启动服务器:

包 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();
    }
}

【问题讨论】:

    标签: java maven jetty


    【解决方案1】:

    由于您使用的是嵌入式 Jetty(不在 Jetty 中运行它),因此您应该将配置文件放在类路径中,即 src/main/resources,这是一个标准的 Maven 位置。

    根据tutorial,您应该加载资源文件并使用它初始化 Jetty:

    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();
        }
    }
    

    这个例子是针对文件服务器的,但我想它对于任何其他服务器都是相似的。

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多