gpt4 book ai didi

java - jetty.xml 在 Maven 项目中的位置?

转载 作者:行者123 更新时间:2023-11-30 11:19:23 25 4
gpt4 key购买 nike

我从这里阅读有关 jetty 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();
}
}

最佳答案

由于您使用的是嵌入式 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();
}
}

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

关于java - jetty.xml 在 Maven 项目中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23267639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com