简体   繁体   English

Maven 依赖项在 WEB-INF/lib 中不可见

[英]Maven dependencies not visible in WEB-INF/lib

I'm having this weird problem making my project Maven-based.我在使我的项目基于 Maven 时遇到了这个奇怪的问题。 I created a new Maven project in Eclipse and enabled the Dynamic Web facets.我在 Eclipse 中创建了一个新的Maven项目并启用了动态 Web 方面。 But during runtime now it throws ClassNotFoundException because the Maven dependencies are not copied to WEB-INF/lib directory.但是在运行时它现在抛出ClassNotFoundException因为 Maven 依赖项没有复制到WEB-INF/lib目录。 I even tried adding the copy-dependencies section in POM, but it didn't help.我什至尝试在 POM 中添加复制依赖项部分,但没有帮助。

Someone here seemed to have solved this the right way, but I guess he forgot to mention the solution ::(这里有人似乎以正确的方式解决了这个问题,但我猜他忘了提到解决方案 ::(

This should have nothing to do with eclipse and m2eclipse, and optionally for better support - m2e-wtp .这应该与 eclipse 和 m2eclipse 无关,并且可以选择获得更好的支持 - m2e-wtp Also, you don't need copy-dependencies.此外,您不需要复制依赖项。 Here are a few possible reasons:以下是几个可能的原因:

  • you should invoke mvn package (or right-click > maven > package) and obtain a war file - the <packaging> (in the pom) must be war您应该调用mvn package (或右键单击 > maven > package)并获取一个 war 文件 - <packaging> (在 pom 中)必须是war
  • your dependencies should be with the default scope (if they are provided or test they will not be included in the archive)您的依赖项应具有默认范围(如果providedtest它们将不会包含在存档中)
  • if you are running the project as dynamic web project on a server within eclipse, then you should open the project properties (right click > properties) and select "Deployment Assembly".如果您在 eclipse 中的服务器上将项目作为动态 Web 项目运行,那么您应该打开项目属性(右键单击 > 属性)并选择“部署程序集”。 There click "add", select "build path entries", and choose "maven dependencies".单击“添加”,选择“构建路径条目”,然后选择“maven 依赖项”。 This will instruct WTP to send the maven dependencies to the server dir.这将指示 WTP 将 maven 依赖项发送到服务器目录。

First install Maven Integration For Eclipse WTP (Help->Eclipse Marketplace)首先安装Maven Integration For Eclipse WTP (Help->Eclipse Marketplace)

Then in your web project's pom.xml just add:然后在你的 web 项目的 pom.xml 中添加:

    <packaging>war</packaging>

After that just right click the project, Maven -> Update Project, which should automagically add Maven Dependencies for you in the Deployment Assembly.之后,只需右键单击项目,Maven -> 更新项目,它应该会在部署程序集中为您自动添加 Maven 依赖项。

右键单击项目 > Maven > 更新 Maven 项目 > 确定为我修复它

Check the Deployment Assembly property of the project.检查项目的部署程序集属性。 I have seen some mvn eclipse:eclipse generated Eclipse projects sometimes have the assets being deployed to the wrong location.我见过一些mvn eclipse:eclipse生成的 Eclipse 项目有时会将资产部署到错误的位置。 From what I can remember the new Deployment Assembly had some issues with earlier versions of Eclipse 3.6 (Helios).据我所知,新的部署程序集在 Eclipse 3.6 (Helios) 的早期版本中存在一些问题。

我刚刚做了 maven -> 更新项目对我有用。

If you look into your deployment and see that WEB-Inf/lib is empty of your Maven dependencies, then ensuring those Maven dependencies are in your deployment assembly is crucial as mention in the chosen answer.如果您查看您的部署并发现 WEB-Inf/lib 没有您的 Maven 依赖项,那么确保这些 Maven 依赖项在您的部署程序集中是至关重要的,如所选答案中所述。

However be wary of Eclipse then not refreshing everything even with a Maven update.但是要小心 Eclipse,然后即使使用 Maven 更新也不会刷新所有内容。 If you clean, rebuild and redeploy and still do not see the lib folder being populated try removing and re-adding the application to the server deployments via Servers>Add and Remove.如果您清理、重建和重新部署,但仍然看不到正在填充的 lib 文件夹,请尝试通过服务器>添加和删除将应用程序删除并重新添加到服务器部署中。

This unclogged the system for me.这为我疏通了系统。

Right-click project -> Deployment Assembly: See if you have the Maven Dependencies and all others in there.右键单击项目 -> 部署程序集:查看是否有 Maven 依赖项和所有其他依赖项。 If not, Right-Click project -> Maven -> Enable Workspace Resolution如果没有,右键单击项目 -> Maven -> 启用工作区解析

(maybe then Maven -> Update Project, but then look again into "Deployment Assembly" and see what You've got.) this solved it (finally!) for me. (也许然后 Maven -> 更新项目,然后再次查看“部署程序集”,看看你有什么。)这为我解决了它(终于!)。

On Eclipse, select your Web Project -> right click -> Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Maven Dependencies在 Eclipse 上,选择您的 Web 项目 -> 右键单击​​ -> 属性 -> 部署程序集 -> 添加 -> Java 构建路径条目 -> Maven 依赖项

With this steps, maven libraries will be included on WEB-INF/lib when full publish.通过这些步骤,maven 库将在完全发布时包含在 WEB-INF/lib 中。

you want to use the copy-dependencies plugin你想使用复制依赖插件

see this post and the docs看到这篇文章和文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM