What's the ideal way to configure an Eclipse Web Project to test Spring/JPA
I recently used eclipse (myeclipse actually) to create a new Web Project via the new->web project wizard. The default directory structure for the project looks as follows:
src
WebRoot
WebRoot/META-INF
WebRoot/META-INF/MANIFEST.MF
WebRoot/WEB-INF
WebRoot/WEB-INF/lib
WebRoot/WEB-INF/classes (not visible via the Package/Project Explorer)
WebRoot/WEB-INF/web.xml
开发者_运维百科
For jpa I added:
WebRoot/META-INF/persistence.xml
For spring I added:
WebRoot/META-INF/spring/spring-master.xml
When trying to run a very basic junit test to load the spring context and hibernate entity manager I was running into classpath problems. Initally to get around the first class path problem I encountered I added WebRoot/META-INF/spring
to the junit->Run Configurations classpath when Spring couldn't find my spring configuration files.
But then I ran into another classpath error when jpa couldn't find my entity bean definition. Reading this article (dated 2007) it states that META-INF/persistence.xml
should be contained within WEB-INF/classes
because JPA searches for entity bean class files in the parent directory of META-INF/persistence.xml
When I copied META-INF
(via Windows Explorer since eclipse is hiding the classes directory) and all its contents into WEB-INF/classes
my classpath errors went away (as well as the need to monkey with the junit run configuration to remedy the previous problem of spring not knowing where my spring config file is located turns out I still need to add the spring folder to the junit classpath option).
But now I have configuration files in a folder which does not show in Eclipse's Package/Project Explorer.
What is the best way to setup Eclipse for this situation? Tell it to show the classes folder somehow? Why does Eclipse create META-INF
as a sibling to WEB-INF
for web projects?
Is there a way to inform JPA to look for entity classes somewhere other than the default?
Thanks for any input!
You should know that eclipse mixes the compiled classes and resource from src
(and any other source code directory) in the <WebRoot>/WEB-INF/classes
directory.
So one way to access the same file in Test and Webapp (for example META-INF/persistence.xml) is to put it in src/META-INF/persistence.xml
. Then it is available in <WebRoot>/WEB-INF/classes/META-INF/persistence.xml
for the web app, and in <classpath>/META-INF/persistence.xml
for EVERY application or test.
So my recommendation is to put all the stuff that is addressed via classpath (persistence.xml, spring.xml) in a source directory, and let eclipse do the rest. - If you specify the location of a resource via classpath, then it should work the same way for webapp, test, app.
BTW: eclipse can create many source code directories, you can use one for the normal application classes, one for the class path relevant resource and one for the test classes.
精彩评论