如何使用 eclipse+m2eclipse 防止重复的 servlet jar

发布于 2024-11-05 02:43:13 字数 1206 浏览 5 评论 0原文

我正在使用 Eclipse + Maven + m2eclipse 在 Apache Tomcat 中构建和测试 Web 应用程序。

我已经在 Eclipse 中配置了 Tomcat 服务器,并为我的 Web 应用程序配置了部署程序集,包括“Maven 依赖项”(Java 构建路径条目的专业化)。

当我部署并启动服务器时,Tomcat/Catalina 总是警告我:

信息:validateJarFile(/projects/src/main/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webapp/WEB-INF/lib/servlet-api-2.5。 jar) - jar 未加载。请参阅 Servlet 规范 2.3,第 9.7.2 节。违规类:javax/servlet/Servlet.class

这是因为 m2eclipse 将 servlet-api-2.5 视为我的项目的依赖项,并将其视为“Maven 依赖项”的一部分,并将其复制为部署程序集的一部分,但是Tomcat servlet 容器有它自己的副本,并且不喜欢在类路径上看到 2 个副本。

我在我的 pom.xml 中标记了从我的项目到 servlet-api-2.5 的依赖关系,

<scope>
provided
</scope>

这确实阻止了独立 Maven 将 servlet-api-2.5 打包到我的构建中,但 m2eclipse 并不这么认为。

(这不是什么大问题,因为警告是无害的,我只在 IDE 内测试时看到它,真正的客户不会看到它,但我仍然想知道如何修复它,因为我喜欢干净和我喜欢知道事情是如何工作的。)

有没有正确的方法来告诉 m2eclipse 不要部署这个文件,或者告诉 Eclipse 不要让 m2eclipse 对哪些依赖项是运行时依赖项有最终决定权?

我确实找到了 https://issues.sonatype.org/browse/MNGECLIPSE-1193提及

“Maven 依赖项”容器反映了测试编译时范围,因此它应该具有范围“provided”的依赖项

I'm using Eclipse + Maven + m2eclipse to build and test a web application in Apache Tomcat.

I've configured a Tomcat server inside Eclipse, and configured the deployment assembly for my web app, including "Maven Dependencies" (specialization of Java Build Path Entries).

When I deploy and start the server, Tomcat/Catalina always warns me:

INFO: validateJarFile(/projects/src/main/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webapp/WEB-INF/lib/servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

This is because m2eclipse sees servlet-api-2.5 as a dependency of my project, and considers it as part of "Maven Dependencies", and copies it as part of the deployment assembly, but the Tomcat servlet container has its own copy of this and doesn't like seeing 2 copies on the class path.

I've marked the dependency from my project to servlet-api-2.5 in my pom.xml with

<scope>
provided
</scope>

which does prevent standalone Maven from packaging servlet-api-2.5 into my builds, but m2eclipse doesn't see it that way.

(This isn't a huge deal because the warning is harmless, I only see it during testing inside the IDE, and real customers won't see it, but I'd still like to know how to fix it because I like cleanliness and I like knowing how things work.)

Is there a correct way to tell m2eclipse not to deploy this file, or to tell Eclipse not to let m2eclipse have the final say on which dependencies are runtime dependencies?

I did find https://issues.sonatype.org/browse/MNGECLIPSE-1193 which mentions

the "Maven Dependencies" container reflects test compile time scope, so it is supposed to have dependencies with scope "provided"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心安伴我暖 2024-11-12 02:43:13

Install extras for m2eclipse plugin ("Maven Integration for WTP") from update site http://m2eclipse.sonatype.org/sites/m2e-extras. After install, update the project configuration.

时间你老了 2024-11-12 02:43:13

通常,您的项目中存在依赖于 servlet-apî.jar 的依赖项

。Maven 的默认行为是我将尝试导入您的依赖项 + 导入依赖项的依赖项。

如果你想排除特定的“子依赖”,你可以给maven这样的配置:

<dependency>
    <groupId>com.hpsworldwide.mbtrs.switch</groupId>
    <artifactId>YOUR_DEPENDENCY</artifactId>
    <version>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然后maven将导入YOUR_DEPENDENCY +所有YOUR_DEPENDENCY依赖项,但将从YOUR_DEPENDENCY的依赖项中排除servlet-api。

Normally there is a dependency in your project that is depending on servlet-apî.jar

The default behaviour of Maven is that i wiill try to import your dependency + the dependencies of the imported dependency.

If you want to exclude a specific "sub-dependency", you can give maven a configuration like this :

<dependency>
    <groupId>com.hpsworldwide.mbtrs.switch</groupId>
    <artifactId>YOUR_DEPENDENCY</artifactId>
    <version>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

then maven will import YOUR_DEPENDENCY + all YOUR_DEPENDENCY dependencies, but will exclude servlet-api from the dependencies of YOUR_DEPENDENCY.

我也只是我 2024-11-12 02:43:13

我刚刚遇到了类似的问题,相信我已经找到了答案。

如果您转到 Eclipse 中的服务器配置设置并选择“服务模块而不发布”,那么这种情况应该不会再发生。

Maven/M2Eclipse 正在正确构建 WAR - servlet-api-2.5.jar 不会位于您的目标目录或 WAR 文件中。

但问题是,当您通过 eclipse 将应用程序部署到 tomcat 时,Eclipse 不会默认使用您的 maven 构建的 WAR/target 目录,它只是使用项目的正常 Eclipse“导出”设置。因此,它会在您的“Java EE 模块”(或“部署程序集列表”,如果您使用的是 Helios)中看到 Maven_Dependency 中所有 jar 的列表,但它不考虑范围,而只是部署所有 jar。

如果您选择不发布服务选项,那么 Eclipst/Tomcat 应该直接从目标目录运行应用程序,以便遵守 Maven 范围。

它永远不会影响您的实时部署(除非您通过 eclipse 进行部署!),因为 Maven 正在做正确的事情,但它有时会在本地引起问题,因为您可能会遇到冲突的 servlet/jsp jar,这可能会导致类广播异常和一般的痛苦。 ..

I have just had a similar problem, and believe I have got to the bottom of it.

If you go to your server configuration settings in Eclipse and select "Serve Modules without Publishing" then this should no longer occur.

Maven/M2Eclipse is building the WAR correctly - servlet-api-2.5.jar wont be in your target directories or WAR file.

But the problem is, when you deploy the application via eclipse on to your tomcat, Eclipse does not use your maven built WAR/target directories as default, it just uses the normal Eclipse "export" settings for your project. So it sees in your "Java EE Modules" (or "Deployment Assembly List" if you are using Helios) the list of all the jars in your Maven_Dependencies, but it does not respect the scope, and just deploys all the jars.

If you select serve without publishing option then Eclipst/Tomcat should just run the app straight off your target directory so will respect the maven scopes.

It won't ever affect your live deployments (unless you are deploying via eclipse!) as maven is doing the right thing, but it can sometimes cause problems locally as you can ave conflicting servlet/jsp jars which can cause classcastexceptions and general misery...

南城旧梦 2024-11-12 02:43:13

如果您使用的是 Indigo,您可以通过单击“窗口”-> 找到 WTP 插件。 “偏好设置”-> “Maven”-> “发现”-> “打开目录”。

If you are using Indigo you can find the WTP plugin by clicking to "Window" -> "Preferences" -> "Maven" -> "Discovery" -> "Open Catalog".

心碎的声音 2024-11-12 02:43:13

提示:安装“Maven Integration for WTP”并更新项目配置后,检查目录 /WEB-INF/lib 并删除其中的所有 JAR。现在,清理 Tomcat 工作目录并再次运行。

One tip: after install "Maven Integration for WTP" and update the project configuration, check the directory /WEB-INF/lib and delete all JARs inside. Now, clean the Tomcat work dir and run again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文