Maven,多模块项目的Spring配置

8

我希望设置一个多模块的Maven项目(如下所述),以便良好扩展。我对这种方法有一些疑问,这主要是从Sonatype example中汲取的。

我已经阅读了一定数量的关于Maven多模块项目的文章,但没有找到超越基本水平的示例。

问题:

  1. 以下是否是一个好的项目结构来开始?或者从一开始就有灾难的味道——即在设置构建时会导致大量重构?简而言之,我希望避免设置与Maven相悖的东西。
  2. 我希望一些模块相当独立,而大多数模块是相互关联的。每个模块都作为Git存储库开始,稍后将紧密链接的模块一起重构,这样可以吗?

目标:

  1. 为基于Spring、JSF2和Maven的模块化项目设计良好的项目结构,以允许包含选定模块及其依赖关系的构建。

  2. 应该可以通过Maven配置(如jetty-maven-plugin)在轻量级容器(如Tomcat/Jetty)上部署单独的Web模块。这应该能够通过Maven拉取必要的依赖项。这使得在开发过程中容易专注于正在进行的模块(而不必运行完整的构建和部署),并只在完成构建时部署整个应用程序。

  3. 设置应该允许基于选择的模块创建多个分发版。我认为这可以通过使用将拉取并打包相应模块的构建模块来实现。

项目结构

Core domain classes.  
somapp.core (maven project)  
 |- someapp.core (maven module)  
 |- someapp.core.tests  

Account Management Domain classes   
someapp.accountmgmt   
|- someapp.accountmgmt   
|- someapp.accountmgmt.tests   

component1 domain classes   
someapp.component1   
|- someapp.component1   
|- someapp.component1.tests

Service 1 - # account management (User login)
someapp.accountmgmt  
 |- someapp.accountmgmt.api  
 |- someapp.accountmgmt.impl  
 |- someapp.accountmgmt.mocks  
 |- someapp.accountmgmt.tests  

someapp.service2  
 |- someapp.service2.api  
 |- someapp.service2.impl  
 |- someapp.service2.mocks   
 |- someapp.service2.tests   
 |- someapp.service2.cli    # CLI access for service2

someapp.service3  
 |- like above  

someapp.accountmgmt.web  
 |- someapp.accountmgmt.web  

someapp.service2.web  
 |- someapp.service2.web  

someapp.service3.web  
 |- someapp.service3.web  

someapp.build1 # bundle accountmgmt and service2 into 1 war file  

someapp.build2 # bundle accountmgmt and service3 into 1 war file  

somapp.build3 # bundle  accountmgmt, service2 and service3 into 1 war file  

(i.e. someapp.accountmgmt.web.war, someapp.accountmgmt.jar, someapp.service2.web.war, someapp.service2.jar, someapp.service3.web.war, someapp.service3.jar, someapp.core.jar)

我知道项目结构不是固定不变的。我想设置一个良好的起点。欢迎提供建议/示例链接。

2个回答

3
对于Spring的部分已经在多模块项目中的Spring配置讨论并接受了答案。至于一般布局,我只看到一个WAR项目和服务只有在它们相关时才捆绑在一起(例如UserLoginService不会与DomainObjectsService一起)。
我建议将结构分成几个不同的项目,将依赖项(业务对象等)作为JAR项目部署到本地存储库,并在需要它们的(现在不同的)项目中列出它们作为普通的Maven依赖项。然后在您的应用服务器上,您可以将应用程序部署到不同的路径下(例如yourdomain.com/app1,yourdomain.com/service2)。
对于你的雄心壮志,我向你致以敬意!
编辑:如果您愿意,可以有多个WAR,有一种方法,可以参考这篇SpringSource博客文章:在多WAR Spring应用程序中使用共享的父应用程序上下文

0

从Spring IO开始的层次结构,一直到您的构件,可以作为一个单一的构建多模块项目来完成。

Spring-IO (dependencies)
   - Your parent pom (custom and further dependency management, plugins etc)
      - someapp-parent (really just a container for each -independent- sub-module)
         someapp-api    (deploy as jar into Nexus
         someapp-remote (Implements API and makes REST calls to your web app - also an independent jar)
         someapp-web    ('war' Exposes REST - JSON - representations of your API domain objects)
         someapp-dashboar (Admin console working with the API/web app via remote so you can manage everything, also a 'war')

顺便说一句,Spring IO真的很好,它是一组祝福的依赖项,可以很好地协同工作并避免类加载器问题。非常值得将您的项目迁移到最新版本中使用。我还建议您为您的Web应用程序使用Spring Boot。

如上所述,我认为将所有与应用程序相关的模块构建为一个整体是值得的,这样版本控制会更容易,并且您可以在单个构建命令中测试所有内容。我最近参与了一个将所有这些模块分开的项目,这意味着合并更改、构建工件、部署工件等需要4倍的工作量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接