如何包含父Spring项目(使用Git子模块)? [英] How to include a parent Spring project (using Git submodule)?

查看:107
本文介绍了如何包含父Spring项目(使用Git子模块)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

尽管我并不热衷于更改构建系统,但我并不是Maven的忠实拥护者.尤其是继承和聚合的概念现在限制了我.也许常春藤是一个选择吗?

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:

有一些父项目,包括一些类,Spring配置文件和其他资源,例如模板,图像和样式表.我们称之为base.这不是完整的Spring Webapp,因此不会部署.从base继承 的其他几个项目也应打包到WAR中.我们称它们为webapp1webapp2.

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.

basewebapp1webapp2有自己的Git存储库:

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)

我希望能够使用Git子模块在Web应用程序内部更改base的代码,还希望能够在webapp目录的内部使用mvn package为每个Web应用程序构建功能齐全的WAR.

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的parentmodule不允许使用这种动态方法.我根本没有找到使用module这样的方法,而根据需要使用parent既复杂又静态:对base的每次更改都需要将新版本推送到存储库中,以便Webapp可以从中继承.

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.

也许我不完全了解Maven的继承,但是我现在很迷茫.

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?

推荐答案

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

I found a working solution by myself.

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

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>

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

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:

  • 使用来自Webapp和base的类和资源(即内部所有文件)创建WAR

  • 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

虽然资源非常容易覆盖,但需要对org.apache.maven.plugin:maven-war-plugin进行一些配置才能将Webapp资源纳入WAR.编译两个不同的源文件夹需要一个插件,因此我需要使用org.codehaus.mojo:build-helper-maven-pluginbase的类添加到WAR中.

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.

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

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" />

这确实需要大量的反复试验,但是最后我使它起作用了,我认为这是使用Maven实现目标的最佳方法.使用动态工具(例如 Buildr )会容易得多,但是遗憾的是,Buildr在Windows上运行缓慢(感谢到Ruby),并且不能很好地集成到大多数IDE中.

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.

这篇关于如何包含父Spring项目(使用Git子模块)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆