Maven 的 tomcat:run mojo 没有看到我的 servlet
我有一个由 Maven 管理的简单 WAR 项目。我的 servlet 代码位于 src/main/java/* 下,所有静态内容(HTML、图像等)位于 src/main/webapp 下。我可以将生成的 WAR 部署到我们的临时 JBoss 服务器上并且它可以工作,但是重新部署的往返过程非常慢。我希望能够在嵌入式 Tomcat 上本地测试更改,但我遇到了问题。
servlet 映射是在 src/main/resources/web.xml 中设置的——我认为这是正确的;相关部分看起来
<servlet>
<servlet-name>myServ</servlet-name>
<servlet-class>my.pkg.MyServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServ</servlet-name>
<url-pattern>/ms</url-pattern>
</servlet-mapping>
Maven 告诉我它正在 localhost:8080/myprojectname 运行,事实上,当我去那里时,我可以看到静态内容。但是,我尝试过 localhost:8080/myprojectname/ms 以及 localhost:8080/ms —— 前者给我一个 404,后者给我一个 0 字节响应。
简而言之:我的活动内容在哪里?我在控制台上看不到任何内容。看起来 Maven 创建了一个 target/tomcat/logs
但它是空的。 target/tomcat/webapps
也是空的,当我运行 mvn tomcat:run -X
时,我得到了大量调试输出,包括对所有 JAR 依赖项的引用,指向 target/classes
的 classesDir
属性(这是正确的),但没有提及我设置的 URL 模式,也没有提及要调用的 Servlet 类的名称处理程序。我觉得我应该告诉 Maven 或 Tomcat 如何找到 web.xml,或者也许以另一种方式指定 URL 映射......以防万一你不知道,我是新手:D
ETA :我的设置看起来非常像 这个人,尽管我们的问题似乎不同。
I have a simple WAR project managed by Maven. I have the servlet code under src/main/java/*
and all the static content (HTML, images, etc) under src/main/webapp
. I can deploy the resulting WAR on our staging JBoss server and it works, but the round-trip to redeploy is painfully slow. I'd like to be able to test changes locally on an embedded Tomcat, but I'm having problems.
The servlet mapping is set up in src/main/resources/web.xml
-- I think it's correct; the relevant part looks like
<servlet>
<servlet-name>myServ</servlet-name>
<servlet-class>my.pkg.MyServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServ</servlet-name>
<url-pattern>/ms</url-pattern>
</servlet-mapping>
Maven tells me it's running at localhost:8080/myprojectname
, and indeed when I go there I can see the static content. However, I've tried localhost:8080/myprojectname/ms
as well as ``localhost:8080/ms` -- the former gives me a 404, and the latter a 0-byte response.
In short: where's my active content? I see nothing on the console. It looks like Maven made a target/tomcat/logs
but that's empty. target/tomcat/webapps
is empty too, and when I run mvn tomcat:run -X
, I get a lot of debug output, including references to all my JAR dependencies, a classesDir
property that points to target/classes
(which is correct), but no mention anywhere of that URL pattern I set, nor the name of the Servlet class to invoke as a handler. I feel like I'm supposed to tell Maven or Tomcat how to find the web.xml, or maybe specify the URL mapping in another way.... in case you can't tell, I'm new at this :D
ETA: My setup looks an awful lot like this guy, though our problems appear to be different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
比我想象的更简单:虽然 POM 设置为使用 src/main/resources/web.xml,但这不是放置它的“正确”位置。将其移动到 src/main/webapp/META-INF 使 servlet 出现——当然,这导致了项目设置中另外十几个错误,但那是另一天的故事了。
Simpler than I thought: though the POM is set up to use
src/main/resources/web.xml
, that's not the "right" place to put it. Moving it tosrc/main/webapp/META-INF
made the servlet show up -- which, of course, led to another dozen or so mistakes in the project settings, but that's a story for another day.