cardori cardori - 7 months ago 36
Java Question

Can't create packages with Maven webapp

I have created a project using the following maven webapp project in eclipse:

enter image description here

When adding a package to the project (right click project -> new -> package), the package gets added as a folder (I added a package named core). It does not have the usual package icon:

enter image description here

If I try to create a new class and select a package, there are no entries in the list box. I have tried creating packages in a normal eclipse dynamic web project and these work correctly.

How do I get packages in Maven enabled webapp projects?


Your code should be in src/main/java. If it doesn't exist already, create it, and then pick Maven -> Update Project from the context menu on the project.

You'll then see src/main/java alongside src/main/resources. Right click that, select New -> Package, and it should appear as you'd expect.

Maven won't compile sources under src/main/resources, which is why your code needs to be in src/main/java. The src/main/resources directory is for files that you want to appear on your classpath, which don't need to be compiled. You might put properties files there, for example.

Edit: The reason that 'resources' folders aren't shown in the usual layout in Package Explorer is that m2e sets up resource folders in a different way to source folders. For an explanation of why it does that, see: Maven/Eclipse: The default build path for resources excluded everything