简体   繁体   English

两个具有MVC设计的java maven项目:它们如何共享相同的“ M”类?

[英]Two java maven projects with MVC design: how can they share the same “M” classes?

I develop a JavaEE app in two separate maven projects: 我在两个单独的maven项目中开发了JavaEE应用程序:
- one project is the API -一个项目就是API
- the other is a JSF app where calls are made to the API of the 1st project. -另一个是JSF应用程序,可在其中调用第一个项目的API。

They both share the same Model (the classes describing my data structure). 它们都共享相同的Model(描述我的数据结构的类)。

How can I have these 2 projects to share the same Model? 如何让这两个项目共享同一模型? Should I create a third Maven project, which will contain these common classes, and this 3rd project will be a dependency for the 2 projects? 我是否应该创建第三个Maven项目,其中将包含这些通用类,而第3个项目将是这两个项目的依赖项? It seems a bit heavy handed. 似乎有点沉重。 Is there a better design? 有更好的设计吗?

I guess that your model classes will be compiled into a separate deliverable, for example a JAR file. 我想您的模型类将被编译成一个单独的可交付成果,例如JAR文件。 Since every Maven project object model describes a single deliverable, the most reasonable option in order to properly reuse the code and include the binary in both web apps is the one you have already proposed. 由于每个Maven项目对象模型都描述一个可交付结果,因此,为了正确地重用代码并将二进制文件包含在两个Web应用程序中,最合理的选择就是您已经建议的方法。

This way, your project may be like this: 这样,您的项目可能是这样的:

+ pom.xml (parent, "pom" type packaging with 3 sub-modules) |_ /ModelJAR/pom.xml (model classes, "jar" type packaging) |_ /API/pom.xml (REST? API, maybe "war" type packaging) |_ /WebAPP/pom.xml (web application, maybe "war" type packaging)

And as you say, both API and WebAPP projects would depend on the model project. 正如您所说,API和WebAPP项目都将取决于模型项目。 It may look cumbersome, but (and I admit that this is a matter of taste) it states clearly the number, nature and location of every deliverable in your code. 它看起来很麻烦,但是(而且我承认这是个问题),它在代码中清楚地说明了每个可交付成果的数量,性质和位置。 I hope you find this useful. 希望这个对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM