java 多模块项目的Maven、Spring配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15407647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven, Spring configuration for multi-module project
提问by JetStream
I am looking to set up a multi module maven project (described below) that would scale well. I have some questions about the approach which is largely drawn from the Sonatype example.
我希望建立一个可以很好扩展的多模块 maven 项目(如下所述)。我对主要来自Sonatype 示例的方法有一些疑问。
I have done a certain amount of reading on maven multi module projects but couldn't find an example going beyond the basic level.
我已经对 maven 多模块项目进行了一定数量的阅读,但找不到超出基本级别的示例。
Questions:
问题:
- Is this (below) a good project structure to start with? Or does it smell of disaster right from the start - i.e. would lead to heavy restructuring when setting up builds? In short, I am looking to avoid setting up something that goes against the grain with Maven.
- I am expecting some modules to be quite independent, while most will be interrelated. Is it alright to start with each module as a Git repo and later refactor in together modules that are tightly linked?
- 这是(下面)一个好的项目结构吗?或者它从一开始就带有灾难的味道——即在设置构建时会导致大量重组?简而言之,我希望避免设置与 Maven 背道而驰的东西。
- 我期待一些模块是非常独立的,而大多数将是相互关联的。将每个模块作为 Git 存储库开始,然后将紧密链接的模块重构在一起是否可以?
Objectives:
目标:
Good project structure for a modular Spring, JSF2, Maven based project, that would allow for builds involving a selection of modules and their dependencies.
It should be possible to deploy an individual web module on a lightweight container like Tomcat/Jetty through Maven configuration (like jetty-maven-plugin). This should be able to pull in the necessary dependencies through Maven. That makes it easy during development to focus on the module being worked on (not having to run a full build and deployment) and deploy the full application only in a complete build.
The setup should allow for multiple distributions based on a selection of modules to be included in the build. I take it this can be achieved through the use of build modules that will pull and package the corresponding modules.
基于 Spring、JSF2、Maven 的模块化项目的良好项目结构,允许构建涉及选择模块及其依赖项。
应该可以通过 Maven 配置(如 jetty-maven-plugin)在 Tomcat/Jetty 等轻量级容器上部署单个 Web 模块。这应该能够通过 Maven 引入必要的依赖项。这使得在开发期间可以轻松地专注于正在处理的模块(不必运行完整的构建和部署)并仅在完整的构建中部署完整的应用程序。
该设置应允许基于要包含在构建中的模块选择进行多个分发。我认为这可以通过使用将拉取和打包相应模块的构建模块来实现。
Project structure
项目结构
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)
I understand project structures are not set in stone. I would like to set up one that is a good starting point. Suggestions / Links to examples are welcome.
我知道项目结构不是一成不变的。我想建立一个很好的起点。欢迎提供建议/示例链接。
回答by Michael Gower
Well for the Spring part it's already discussed and an answer accepted at Spring Configuration in a multi-module project. As far as the general layout I've only seen one WAR per project and services only bundled together if they are related (e.g. UserLoginService would not go together with DomainObjectsService).
好吧,对于 Spring 部分,它已经讨论过,并且在Spring Configuration in a multi-module project 中接受了一个答案。至于总体布局,我只看到每个项目和服务只有一个 WAR 捆绑在一起,如果它们相关(例如 UserLoginService 不会与 DomainObjectsService 一起使用)。
I would suggest breaking up the structure into several different projects, with the dependencies (business objects, etc) deployed as JAR projects to a local repo and listed as normal Maven dependencies in the (now different) projects that need them. Then in your app-server you can deploy the apps to different paths (e.g. yourdomain.com/app1, yourdomain.com/service2).
我建议将结构分解为几个不同的项目,将依赖项(业务对象等)作为 JAR 项目部署到本地存储库,并在需要它们的(现在不同的)项目中列为正常的 Maven 依赖项。然后在您的应用程序服务器中,您可以将应用程序部署到不同的路径(例如 yourdomain.com/app1、yourdomain.com/service2)。
My compliments to your ambition though!
不过,我要赞美你的野心!
EDIT: There is a way to have multiple WARs if you wish, see this SpringSource blog post about Using a shared parent application context in a multi-war Spring application.
编辑:如果您愿意,有一种方法可以拥有多个 WAR,请参阅有关在多War Spring 应用程序中使用共享父应用程序上下文的SpringSource 博客文章。
回答by user1016765
A hierarchy starting with Spring IO through to your artifacts can be done as a single build multi-module project.
从 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 is BTW really good as a blessed set of dependencies that work well together and avoid classloader issues. Well worth migrating your project to use it as the latest version looks pretty up-to-date. I'd also recommend using Spring Boot for your web app(s).
顺便说一句,Spring IO 作为一组受祝福的依赖项非常好,它们可以很好地协同工作并避免类加载器问题。非常值得迁移您的项目以使用它,因为最新版本看起来非常最新。我还建议为您的 Web 应用程序使用 Spring Boot。
As outlined I think its worth having all your app-related modules build as one so versioning is easier and you can test everything in a single build command. I've recently worked on a project where we keep all these modules separate, and it just means 4x the effort to merge changes, build artifacts, deploy artifacts etc.
如上所述,我认为将所有与应用程序相关的模块构建为一个是值得的,这样版本控制就更容易了,并且您可以在单个构建命令中测试所有内容。我最近参与了一个项目,我们将所有这些模块分开,这意味着合并更改、构建工件、部署工件等的工作量增加了 4 倍。