如何包含父 Spring 项目(使用 Git 子模块)?

发布于 2024-08-22 22:07:09 字数 1232 浏览 6 评论 0原文

我目前正在使用 Spring 开发多个 Web 应用程序。我使用 Maven 进行构建,使用 Git 进行版本控制。目前我正在尝试找到一种方法来分割所有网络应用程序使用的一些东西的开发,例如我有一些对于所有项目都相同的帮助器类。问题是,我不想只使用类,还想使用资源文件和某种父 POM,同时仍独立于存储库并能够从 Git 中受益。

虽然我并不热衷于改变构建系统,但我也不是 Maven 的真正粉丝。尤其是继承和聚合的概念现在限制了我。也许常春藤是一个选择?

我想给你一个我的设置的快速概述:

有某种父项目,包括一些类、Spring 配置文件和其他资源,如模板、图像和样式表。我们称之为base。这不是一个完整的 Spring Web 应用程序,不会被部署。还有其他几个项目继承base,并且应该打包到 WAR 中。我们将它们称为 webapp1webapp2

basewebapp1webapp2 有自己的 Git 存储库:

\
 |
 |- base.git       (base's repository)
 |
 |- webapp1.git    (webapp1's repository)
 | \
 |   base          (base used as a Git submodule)
 |
 |- webapp2.git    (webapp2's repository)
   \
     base          (base used as a Git submodule)

我希望能够将 base 的代码从在 webapps 中使用 Git 子模块,还能够在 webapp 目录中使用 mvn package 为每个 webapp 构建功能齐全的 WAR。

Maven 的 parentmodule 不允许这样的动态方法。我根本没有找到像这样使用 module 的方法,并且使用 parent 来满足我的需求是复杂和静态的:对 base 的每次更改需要将新版本推送到存储库,以便 Web 应用程序可以继承它。

也许我没有完全理解Maven的继承,但我现在很迷茫。

有人取得过类似的成功吗?您使用什么构建系统以及如何使用?

I'm currently developing multiple web applications using Spring. I'm using Maven for building and Git for version control. At the moment I'm trying to find a way to split development of some things used by all webapps, e.g. I have some helper classes that are the same for all projects. The problem is, I don't want to use only classes, but also resource files and some sort of parent POM while still being independent from a repository and able to benefit from Git.

Although I'm not enthusiastic to change the build system, I'm not a real fan of Maven. Especially the concept of inheritance and aggregation is what constrains me right now. Maybe Ivy is an option?

I'd want to give you a quick overview of my setup:

There's some sort of parent project including some classes, Spring configuration files and other resources like templates, images and style sheets. Let's call it base. This one is not a complete Spring webapp and won't be deployed. There are several other projects which inherit from base and should be packed into a WAR. Let's call them webapp1 and webapp2.

base, webapp1 and webapp2 have their own Git repositories:

\
 |
 |- base.git       (base's repository)
 |
 |- webapp1.git    (webapp1's repository)
 | \
 |   base          (base used as a Git submodule)
 |
 |- webapp2.git    (webapp2's repository)
   \
     base          (base used as a Git submodule)

I want to be able to change bases code from inside the the webapps using a Git submodule and also be able to build a fully functional WAR of each webapp using mvn package inside the webapp`s directory.

Maven's parent or module don't allow a dynamic approach like this. I didn't find a way to use module like that at all and using parent for my needs is complex and static: Every change to base would require a new version to be pushed to the repository so that the webapp`s can inherit from it.

Maybe I didn't completely understand Maven's inheritance, but I'm pretty lost right now.

Did anyone achieve something similar with success? What build system did you use and how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-08-29 22:07:09

您的用例似乎是 覆盖 的一个很好的用例(另请参阅示例 )。

覆盖层用于在多个 Web 应用程序之间共享公共资源。一般来说,WAR 项目的所有依赖项都收集在 WEB-INF/lib 中,除了覆盖在 WAR 源上的 WAR 工件之外。

但是 base 不会位于 webapp 模块的“下方”,它很可能是一个同级模块(也许它可以使用一些 Git 黑魔法位于 webapp 的“内部”,但这超出了我的 Git 技能范围我没有想到如何用 Maven 来处理这个问题)。不,真的,我认为 Maven 的方式是使用覆盖。如果这不是你想要的,最好使用 Maven 之外的其他东西。

Your use case seems to be a good use case for overlays (also have a look at the examples).

Overlays are used to share common resources accross multiple web applications. In general, all dependencies of a WAR project are collected in WEB-INF/lib except for WAR artifacts that are overlayed on the WAR source.

But base wouldn't be "under" the webapp module, it would very likely be a sibling module (maybe it can be "in" the webapp using some Git black magic but this goes beyond my Git skills and I fail at thinking of how this could be handled with Maven). No, really, I think the Maven way would be to use overlays. And if this is not what you want, better use something else than Maven IMO.

一瞬间的火花 2024-08-29 22:07:09

我自己找到了一个可行的解决方案。

我的问题的基本解决方案是 元素的一个小子元素,称为 。它允许在指定目录中搜​​索父 POM。否则,您始终需要部署新版本,然后才能在应用程序中对其进行测试。

<parent>
    <groupId>com.example</groupId>
    <artifactId>base</groupId>
    <version>1.0.0</groupId>
    <relativePath>base</relativePath>
</parent>

但这只是起点,允许使用 Git 子模块。为了让它真正工作,我需要执行以下操作:

  • 使用来自 webapp 和 base 的类和资源创建 WAR,即其中的所有文件

    base/src/main/java
    基础/src/主要/资源
    基础/src/main/web应用程序
    src/主/java
    src/主/资源
    src/main/web应用程序
    

    虽然资源很容易覆盖,但需要对 org.apache.maven.plugin:maven-war-plugin 进行一些配置才能将 webapp 资源放入 WAR 中。编译两个不同的源文件夹需要一个插件,因此我需要使用 org.codehaus.mojo:build-helper-maven-plugin 来将 base 的类放入WAR。

  • 此外,我对资源进行了大量过滤,因此几乎不需要自定义基本文件即可启动并运行 Web 应用程序。例如,我在主模板文件中使用 ${project.artifactId} ,因此对于名为 webapp1 的 Web 应用程序,我的 HTML 看起来像这样:

    
    
    

确实经历了很多尝试和错误,但最后我成功了,我认为这是使用 Maven 实现我的目标的最佳方法。使用像 Buildr 这样的动态工具会容易得多,但遗憾的是 Buildr 在 Windows 上速度很慢(感谢Ruby)并且不能很好地集成到大多数 IDE 中。

I found a working solution by myself.

The basic solution of my problem is a small sub-element of the <parent> element called <relativePath>. It allows to search for the parent POM in the specified directory. Otherwise you would always need to deploy a new version before you could test it within your application.

<parent>
    <groupId>com.example</groupId>
    <artifactId>base</groupId>
    <version>1.0.0</groupId>
    <relativePath>base</relativePath>
</parent>

But that was only the starting point, allowing to work with a Git submodule. To get it really working, I needed to do the following:

  • Create a WAR with classes and resources from both, the webapp and base, i.e. all files inside

    base/src/main/java
    base/src/main/resources
    base/src/main/webapp
    src/main/java
    src/main/resources
    src/main/webapp
    

    While resources are pretty easy to cover, a bit of configuration for org.apache.maven.plugin:maven-war-plugin is needed to get the webapp resources into the WAR. Compiling of two different source folders requires a plugin, so I need to use org.codehaus.mojo:build-helper-maven-plugin to get the classes of base into the WAR.

  • Additionally, I used a lot of filtering for the resources, so there's almost no need for customization of the basic files to get a webapp up and running. For example I use ${project.artifactId} inside my main template file, so my HTML <head> will look something like this for a webapp called webapp1:

    <link href="stylesheets/base.css" rel="stylesheet" media="screen" type="text/css" />
    <link href="stylesheets/webapp1.css" rel="stylesheet" media="screen" type="text/css" />
    

It really took a lot of trial and error, but at last I got it working and I think this is the best way to achieve my target using Maven. This would've been a lot easier using a dynamic tool like Buildr, but sadly Buildr is slow on Windows (thanks to Ruby) and doesn't integrate very well into most IDEs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文