简体   繁体   中英

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:
- one project is the API
- the other is a JSF app where calls are made to the API of the 1st project.

They both share the same Model (the classes describing my data structure).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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