问题
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)
ProjectAPIParent
a) ProjectEntity
b) ProjectRepository
c) ProjectAPI
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
APIParentandClientUIParentindividuallyYou will have to take care, of course, that
APIParentandClientUIParentdon't build individually at the same time locally.declare things common to all (e.g. JUnit dependency) just once, in
ApplicationParentuse <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