share code between projects with maven

穿精又带淫゛_ 提交于 2019-12-12 00:43:00

问题


I have to create 2 projects. For this I will user Spring,JPA,JSF and Maven. My projects will be structures on a 3 layer architecture, so I will have view,service and DAO layers. The persistence layer will be provided by hibernate with JPA2.0.
The problem is that this two projects will share a big part of code, basically both projects operates on the same database, and will share the majority of entities, DAO`s and maybe even services.
After I googled a little bit i found that this task cand be achieved with a multi module maven project. So I have created a multimodule maven projects which was composed of:
- core package (that will contain all the common classes),
- internal webApp (which will be accessed by internal users)
- external website (which will be accessed by external customers).

The problem that i encountered here is persitence.xml location, because if i put that file to core package i cannot include the entities from other projects. If i put the file inside projects i can refer the core entities with inside persitence.xml, but some functionality that i want to share, ie:

public abstract class GenericDaoImpl<T> implements GenericDao<T> {

    @PersistenceContext(type = PersistenceContextType.TRANSACTION,unitName="CCPU")
    protected EntityManager em;

wont work anymore because eclipse is "screaming" that there is no persistence unit with name CCPU, because the persistence.xml file is no longer in this project.
First of all, is this the right approach for this kind of problem?
The last but not the least, where should i put the persistence file in order to be able to combine entities from all 3 sub projects ?


回答1:


I have the same situation: two webapps with common domain objects, but all my entities are located in "domain" module. Webapps are like "front-end clients" for "domain" back-end module.

If you want to locate webapp-specific entities in appropriate projects try to delete "em" field from GenericDaoImpl and pass it as parameter to all methods.



来源:https://stackoverflow.com/questions/18211666/share-code-between-projects-with-maven

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!