使用Maven在项目之间共享代码 [英] share code between projects with maven

查看:338
本文介绍了使用Maven在项目之间共享代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建2个项目.为此,我将使用Spring,JPA,JSF和Maven.我的项目将是3层架构上的结构,因此我将拥有视图,服务和DAO层.持久层将由Hibernate与JPA2.0一起提供.
问题在于这两个项目将共享大部分代码,基本上两个项目都在同一数据库上运行,并且将共享大多数实体,DAO甚至服务.
在搜索了一下之后,我发现可以使用多模块Maven项目来完成此任务.因此,我创建了一个包含以下内容的多模块Maven项目:
-核心程序包(将包含所有通用类),
-内部webApp(内部用户将访问)
-外部网站(外部客户可以访问).

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

我在这里遇到的问题是persitence.xml位置,因为如果我将该文件放入核心程序包,则无法包含其他项目中的实体.如果我将文件放在项目中,则可以在persitence.xml中引用核心实体,但是要共享一些功能,即:

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;

将不再工作,因为eclipse会尖叫"没有名称为CCPU的持久性单元,因为该项目中不再存在persistence.xml文件.
首先,这是解决此类问题的正确方法吗?
最后但并非最不重要的一点,我应该将持久性文件放在哪里,以便能够组合来自所有3个子项目的实体?

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 ?

推荐答案

我遇到的情况是相同的:两个具有通用域对象的Web应用程序,但是我所有的实体都位于域"模块中. Webapp就像域"后端模块的前端客户端".

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.

如果要在适当的项目中找到特定于Web应用程序的实体,请尝试从GenericDaoImpl中删除"em"字段,并将其作为参数传递给所有方法.

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.

这篇关于使用Maven在项目之间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆