71

I'm having this weird problem making my project Maven-based. I created a new Maven project in Eclipse and enabled the Dynamic Web facets. But during runtime now it throws ClassNotFoundException because the Maven dependencies are not copied to WEB-INF/lib directory. I even tried adding the copy-dependencies section in POM, but it didn't help.

Someone here seemed to have solved this the right way, but I guess he forgot to mention the solution ::(

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • 1
    Try the latest m2eclipse plugin. I think there are two parts to it and it helped me with getting the maven dependencies on the eclipse class path. – xecaps12 May 21 '11 at 18:08
  • To make maven play nice with wtp, I had to install the core and the extras from here: http://m2eclipse.sonatype.org/installing-m2eclipse.html – xecaps12 May 22 '11 at 02:01
  • @xecaps thanks, but i had already done this – Suraj Chandran May 22 '11 at 08:47

9 Answers9

176

This should have nothing to do with eclipse and m2eclipse, and optionally for better support - 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
  • your dependencies should be with the default scope (if they are provided or test they will not be included in the archive)
  • 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". There click "add", select "build path entries", and choose "maven dependencies". This will instruct WTP to send the maven dependencies to the server dir.
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I don't have the "Deployment Assembly" in project properties. What do I do now?.. – mvmn Jan 11 '12 at 20:07
  • Ok, I got it Eclipse pre-3.6 (I've used 3.5) had "Java EE Module Dependencies" section in project properties. But setting checkbox near "Maven Dependencies" there was not enough for me. I had to do "Run on Server" to my WTP project to get those JARs copied to WEB-INF/lib. I think I had m2eclipse plugin installed, but apparently didn't have WTP support installed from m2e-extras. I'll try installing that later. – mvmn Jan 11 '12 at 21:12
  • @mvmm m2e-wtp, I'll add it to the answer. But it's optional. You can get it working without it – Bozho Jan 11 '12 at 21:37
  • 2
    I agree with everyone else's sentiment on this one. The third option is exactly what worked for me and hadn't seen that suggested anywhere else but this post. This works in both Indigo and the recently released Juno versions of Eclipse. – Kettch19 Jul 25 '12 at 22:09
  • 1
    This solution ignores 'provided' scope from pom.xml, so in my case I can't add whole Maven Dependencies to Deployment Assembly. Do you have any workaround for disabling some jars from Maven Dependencies Library? – denu Aug 06 '12 at 12:16
  • yes, the m2e-wtp plugin handles the provided scope jars when it comes to WTP deployment – Bozho Aug 06 '12 at 16:35
  • 1
    After using the 3rd option it works, but if I use the Maven->Update Project, it reverts back and removes the entry from Deployment Assemblies. Any way to work around this? – Vedran Aug 20 '12 at 11:38
  • Install m2e-wtp, then check project out in workspace, then run mvn clean package, then Run As -> Run on Server. Make sure you delete any previous .\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0) temporary servers that you tried with previously – Sheamus O'Halloran Jan 03 '13 at 23:10
  • 3
    The third option should not be needed, as m2e-wtp should do this automatically. I noticed that whenever the libraries are missing from WEB-INF/lib, manually building (Maven build... goal package) would generate a jar with the libs, but the war that's deployed to the AS via m2e-wtp would NOT contain them. Cleaning, closing/opening projects would sometimes bring the libs back. – Arjan Tijms Jun 15 '13 at 20:50
  • 3
    Eventually I noticed that when the libs *are* being correctly deployed .classpath contains `` for the `org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER` entry. As soon as the libs are gone again, this specific attribute is gone too. It's even visible in the local file history that this attribute comes and goes. – Arjan Tijms Jun 15 '13 at 20:51
  • 1
    Also ... to add "Dynamic Web Module" (i.e. "Deployment Assembly") - project properties (Right click on project) select "Project Facets", select "Dynamic Web Module", Ok and then do this: http://stackoverflow.com/questions/6042475/validating-jdoconfig-with-incorrect-url (Works on Eclipse Juno, Google appengine + GWT + Maven) – eddyparkinson Jul 09 '13 at 09:27
  • In case yours is a maven dependency of another of your own MySubProject, you will not see it under properties>deployment Assembly>Maven Dependencies, but under properties>deployment Assembly> MySubProject, Closing and opening the projects again did the trick for me, nothing else worked, I am using Neon – Dipanshu Verma Jun 19 '17 at 12:10
  • lol, of course I don't want my maven libs deployed, why should I want that? – aliopi Nov 28 '17 at 10:22
14

First install Maven Integration For Eclipse WTP (Help->Eclipse Marketplace)

Then in your web project's pom.xml just add:

    <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.

Vedran
  • 10,369
  • 5
  • 50
  • 57
  • 2
    +1 That worked for me as well. Without it Maven will package the dependency as a jar and you don't want that, you want that all your dependencies to be packed in a war type file. This has many other effects, read more https://maven.apache.org/pom.html – bogdan.rusu Apr 13 '15 at 21:56
  • 1
    maven -> update project worked for me as well. seems necessary in Eclipse Luna version – kandan Apr 20 '16 at 22:21
5

Right-click project > Maven > Update Maven Project > OK fixed it for me

Erik-Jan
  • 51
  • 1
  • 1
4

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. From what I can remember the new Deployment Assembly had some issues with earlier versions of Eclipse 3.6 (Helios).

andyb
  • 43,435
  • 12
  • 121
  • 150
3

I just did maven -> update project worked for me.

Anil
  • 337
  • 2
  • 10
1

On Eclipse, select your Web Project -> right click -> Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Maven Dependencies

With this steps, maven libraries will be included on WEB-INF/lib when full publish.

0

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.

However be wary of Eclipse then not refreshing everything even with a Maven update. 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.

This unclogged the system for me.

Shaun O'Hagan
  • 191
  • 1
  • 5
0

Right-click project -> Deployment Assembly: See if you have the Maven Dependencies and all others in there. If not, Right-Click project -> Maven -> Enable Workspace Resolution

(maybe then Maven -> Update Project, but then look again into "Deployment Assembly" and see what You've got.) this solved it (finally!) for me.

  • If there's no corresponding project configuration in pom.xml and you check the "Update project configuration from pom.xml" when updating project, the Deployment Assembly setting changes will be flushed and set default. – Baytars Apr 21 '22 at 19:31
0

you want to use the copy-dependencies plugin

see this post and the docs

Community
  • 1
  • 1
mut1na
  • 795
  • 6
  • 21