如何从具有所有优点的多模块Maven项目继承? [英] How to inherit from a multimodule Maven project with all its goodies?

查看:85
本文介绍了如何从具有所有优点的多模块Maven项目继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到合适的,可扩展的解决方案的问题:

The problem to which I cannot find a nice, scalable solution:

我有一个项目,可以提供给定工件的多种风味.已将其设置为一个多模块项目,目前包含3个模块:

I have a project that delivers multiple flavours of the given artifact. This has been set up as a multimodule project, currently with 3 modules:

  • /flavor1_module
  • /flavor2_module
  • /flavor3_module

问题是,我还有另外50个项目需要以相同的方式进行设置,即提供3种口味.

The thing is that I have another 50 projects that need to be setup in the same way, i.e. delivering the 3 flavours.

考虑的解决方案:

  1. 将已创建的多模块项目转换为所有其他50个项目的父项目
    • 缺点:它只是无效.与父模块一起保存的指令不会被继承,因此不会执行.
  1. turning the already created multimodule project into a parent for all other 50 projects
    • Cons: It just doesn't work. The instructions kept withing parent's modules are not inherited, thus they are not executed.
  • 缺点::如果我需要flavour4,则需要手动更新所有50个项目以添加flavour4_module(并重复其内容).无法扩展.
  • Cons: if I would need flavour4, I need to manually update all 50 projects to add flavour4_module (and duplicate its content). Not scalable.
  • 缺点::我需要自行开发由模块提供的机制. (例如,在单独的目录中构建每种口味).我也将失去模块提供的明确分隔.
  • Cons: I would need to implement on my own mechanisms which are provided by modules out of the box. (e.g. building every flavour in a separate directory). I would also lose the clear seperation that modules provide.

有什么想法可以很好地做到这一点吗?还有其他选择吗?

Any ideas how to do it nicely? Is there any other option?

谢谢, 卢卡斯

另一种选择是扩展 maven-reactor-plugin 具有 reactor:inject-modules 目标,该目标将从外部工件下载模块定义,并将其定义附加为普通模块.这将动态创建一个新模块.然后,所有50个项目都可以将此pom.xml作为其父项.

Another option would be extending the maven-reactor-plugin with reactor:inject-modules goal, which would download module definition from an external artifact, and attach its definition as a normal module. This would create a new module on the fly. Then all 50 projects could make this pom.xml their parent.

配置看起来像这样(草稿):

The configuration would look like this (a draft):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-reactor-plugin</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <id>inject</id>
      <phase>initialize</phase>
      <goals>
        <goal>inject-modules</goal>
      </goals>
      <configuration>
        <modules>
          <module>
            <artifactId>flavour1_module</artifactId>
            <groupId>[ groupId ]</groupId>
            <version>[ version ]</version>
          </module>
          <module>
            <artifactId>flavour2_module</artifactId>
            <groupId>[ groupId ]</groupId>
            <version>[ version ]</version>
          </module>
          <module>
            <artifactId>flavour3_module</artifactId>
            <groupId>[ groupId ]</groupId>
            <version>[ version ]</version>
          </module>
        </modules>
      </configuration>
    </execution>
  </executions>
</plugin>

这种方式行得通吗?

更新:

编写一个插件来操纵要执行的模块列表(我在上文中描述过的模块注入概念)似乎无法实现,因为模块是由maven核心处理的,并且该机制并非旨在用插件扩展.事实证明,这两个插件都执行相似的工作,即操纵要执行的项目列表:

Writing a plugin that manipulates the list of modules to execute (the idea of module injection that I described above) doesn't seem to be possible to implement, because modules are processed by maven core, and the mechanism was not designed to be extended with a plugin. This is confirmed by the fact that both plugins that do a similar job i.e. manipulating the list of projects to execute:

  • maven-reactor-plugin
  • maven-invoker-plugin

通过执行系统调用来创建maven子进程,以达到目的.对我来说,这不是走的路,因为这是一个非常不稳定的解决方案.实际上, maven-reactor-plugin 已成为

do the trick by executing a system call to create maven child process. For me it is not the way to go, because it's a very unstable solution. Indeed maven-reactor-plugin became incompatible with Maven3.

maven-invoker-plugin 看起来仍然很有希望.该插件最初旨在运行集成测试,但可以使用它来扩展例如编译阶段. 但这要求将子pom.xml-s视为资源并即时进行修改.对于我在此处描述的问题,解决方案将过于复杂且不稳定.我更喜欢在构建Maven模型时可以在内存中运行的更轻巧的东西.

maven-invoker-plugin still looks promising. The plugin was originally designed to run integration tests, but it would be possible to use it to extend e.g. the compilation phase. But it requires the child pom.xml-s to be treated as resources and modified on the fly. For the problem that I described here the solution would be too complicated and unstable. I would prefer something lighter that could operate in memory while building maven model.

因此,现在我使用配置文件,尝试使它们尽可能紧凑.大概过一段时间,我将需要再次考虑这个问题.

So for now I use profiles, trying to make them as compact as possible. Probably in a while I will need again to think about the problem.

推荐答案

现在,您可以使用 maven-瓷砖插件以获取所需的行为.

Now you can use the maven-tiles plugin to get the desired behaviour.

使用maven-tiles,您可以在不同的pom中定义构建行为,并导入到任何您喜欢的地方.

With maven-tiles you can define build behaviours in different poms and import them wherever you like.

MNG-5102 daddy3.zip

Maven的仅继承海峡外套现已正式移除.

Maven's inheritance-only strait jacket is now officially removed.

这篇关于如何从具有所有优点的多模块Maven项目继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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