2

I have two maven web application, let's say ProjectA and ProjectB which are packed as WAR files. Both applications are using third maven application ProjectC which contains some shared classes. I added ProjectC as dependency in ProjectA and ProjectB.

ProjectA and ProjectB are running in Eclipse using maven jetty plugin, but in production it is deployed as WAR files to the Tomcat web server.

This approach used to work fine until the moment I started to share some hibernate entities. Due to the problem I described in this question, jar-file from persistence.xml is not found in eclipse, it's not possible to link persistence.xml with entities in ProjectC.jar so I have to find some alternative solution.

I thought that it would work if I could compile ProjectC classes when ProjectA and ProjectB are compiled, so the structure looks like:

projectA 
 -WEB-INF 
   -classes    
     -project-a.classes
     -project-c.classes
projectB 
 -WEB-INF 
   -classes    
     -project-b.classes
     -project-c.classes

rather then:

projectA 
 -WEB-INF 
   -classes    
     -project-a.classes
   -lib
     -project-c.jar
projectB 
 -WEB-INF 
   -classes    
     -project-b.classes
   -lib
     -project-c.jar

I don't know if this is possible to do with maven multimodule approach, but some other ideas which will solve the problem are also welcome.

Thanks,

Community
  • 1
  • 1
zoran jeremic
  • 2,046
  • 4
  • 21
  • 47

2 Answers2

1

Follow this link to know a way you could use to unpack the resources of some third Project (like your Project C) into another project as resources. It is a excellent and simple guide. You can take the idea to include the entities as classes of you projects A and B, but it will be easier put the persistence.xml and other common files in a new project and keep the java classes (your entities) into separate jar projects.

Guillermo
  • 1,523
  • 9
  • 19
  • Thanks for your idea. I agree that it would be best if everything related to the hibernate is in shared project. The problem is that project A is robust and it uses Spring. I don't want to use Spring in the other project. It would require a lot of changes if I want to use the same configuration in both projects, but I'll definitely try. At the moment I'm trying to find out if it's possible to have META-INF/persistence.xml at some location in the project b where it will have access to /lib/project-c.jar – zoran jeremic Jul 03 '15 at 05:56
  • 1
    For welcome. You could always specify entities in the persistence.xml using elements (instead of use ). Then you will not have problems related to the location of project-c as long as it is "accessible" for project-b (like if is in the web-inf/lib folder or priveded by the runtime environment/server to the war app) – Guillermo Jul 03 '15 at 06:21
  • Oh no. There is more than 100 entities and it is still changing, so it would be frustrating for maintaining.I would try tomorrow as you suggested to have persistence.xml in shared project. – zoran jeremic Jul 03 '15 at 06:55
  • At the end I did as you suggested at the end of the post. I've put persistence.xml and other classes responsible for hibernate to the shared project, and it works as I wanted. Thanks. – zoran jeremic Jul 04 '15 at 03:34
0

Try to set addClasspath=true in war manifest and package your war classes to jar with archiveClasses.

You'll have war structure like this

projectA 
 -WEB-INF
   -lib
     -project-a.jar
     -project-c.jar
Konstantin Pavlov
  • 956
  • 1
  • 10
  • 24
  • I'm not sure if I understood your idea, but this will still add my shared project as jar file, and hibernate persistance.xml will not be able to access entities. – zoran jeremic Jul 03 '15 at 04:17
  • That doesn't change anything since hibernate entities from project C will be in jar file. Persistence.xml is located in main/resources/META-INF/persistence.xml, and it contains lib/project-c.jar. However, this jar file can't be found from this location. This discussion gives good explanation why I can't use jar file in my project http://stackoverflow.com/questions/4433341/what-is-the-right-path-to-refer-a-jar-file-in-jpa-persistence-xml-in-a-web-app/16503869#16503869 – zoran jeremic Jul 03 '15 at 05:15
  • All these examples are ear files. It doesn't work with war files which are not embedded in ear. That would make our further work complicated and would require a lot of changes. I would like to avoid it if possible. – zoran jeremic Jul 03 '15 at 07:00