Java - Maven - Reusing Entity layer at both client and server side projects

坚强是说给别人听的谎言 提交于 2021-01-24 01:44:11

问题


I am working on a Spring Boot, Hibernate, Thymeleaf project, I'm a bit confused to achieve the best architecture to reuse my entity layer.

I have 2 Maven parent projects, I want to achieve the below project hierarchy with just creating one ProjectEntity (reusing it in both parents)

  1. ProjectAPIParent

    a) ProjectEntity

    b) ProjectRepository

    c) ProjectAPI

  2. ProjectClientUIParent

    a) ProjectEntity

    b) ProjectClientUI

The problem is one child project cannot have multiple parents.

Now, I have an option of creating one base parent project only but the problem is that it doesn't sound right because I want full loose coupling between client side and the server side projects.

What would you suggest me to do in this case?

Your opinion will matter a lot. Thanks :)


回答1:


There is not really an idiomatic rule to take this kind of decision.

Personally, I select as parent of an artifact, the project that manifests the strongest natural ownership relationship with.

For example if the ProjectEntity source code is designed to be modified only by developers of the ProjectAPIParent project and released with, ProjectAPIParent should be very probably the Maven parent (as well as its aggregrator pom) of the ProjectEntity module. And ProjectClientUIParent should so use ProjectEntity as a dependency.

But if you don't manage to identify clearly a parent project that owns such a relationship with this artifact, you should probably not make ProjectEntity a children of none of them.
In this case, making ProjectEntity a dependency of this two projects makes more sense.


As a side note, whatever your choice for the parent pom, you may define ProjectEntity as Maven modules (to not mix up with Maven children) in the aggregrator pom.xml of ProjectAPIParent and ProjectClientUIParent.




回答2:


You could establish the following:

+- ApplicationParent
   +- APIParent
   |  +- ProjectAPI
   |  +- Repository
   +- ClientUIParent
   |  +- ClientUI
   +- Entity

ApplicationParent POM:

<modules>
  <module>APIParent</module>
  <module>ClientUIParent</module>
</modules>

APIParent POM:

<modules>
  <module>../Entity</module>
  <module>Repository</module>
  <module>ProjectAPI</module>
</modules>

ClientUIParent POM:

<modules>
  <module>../Entity</module>
  <module>ClientUI</module>
</modules>

Such you can:

  • build your application as a whole (via ApplicationParent)

  • build APIParent and ClientUIParent individually

    You will have to take care, of course, that APIParent and ClientUIParent don't build individually at the same time locally.

  • declare things common to all (e.g. JUnit dependency) just once, in ApplicationParent

  • use <dependencyManagment> to declare dependency details for all dependencies used in any of the sub-projects just once

  • separate declarations necessary for certain sub-projects only across the individual POMs



来源:https://stackoverflow.com/questions/48629984/java-maven-reusing-entity-layer-at-both-client-and-server-side-projects

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