如何强制使用两个 Maven 配置文件中的一个? [英] How to enforce the use of exactly one out of two Maven profiles?

查看:74
本文介绍了如何强制使用两个 Maven 配置文件中的一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Maven 项目,该项目定义了两个单独的配置文件,developerrelease(您肯定会明白这里的意思).我希望随时激活这两个配置文件中的一个,但永远不要同时激活这两个配置文件.如果两者都以某种方式被激活,则此构建没有意义并且应该失败.如果两者都没有被激活,这个构建也没有意义,应该会失败.

I have a Maven project that defines two separate profiles, developer and release (surely you get the drift, here). I want one of these two profiles to be activated at any time, but never both. If both are somehow activated, this build makes no sense and should fail. If neither is activated, this build also makes no sense and should fail.

我确信我可以编写一些自定义插件代码来实现这一点,我很可能最终会这样做,但我有兴趣使用 POM 配置来实现这一点(可以使用来自 Maven Central 的现有插件).

I'm sure I can write some custom plugin code to achieve this, and I might very well end up going that way, but I'd be interested in achieving this using POM configuration (could be using existing plugins from Maven Central).

应该可以使用 -P (--activate-profiles) 激活插件,所以 通过属性不会成为有效的解决方案.使用 activeByDefault 的解决方案也将无效,因为 activeByDefault 通常被称为陷阱,不可靠(我们实际上可能会激活其他配置文件,从而呈现 activateByDefault 无法使用).

It should be possible to activate plugins using -P (--activate-profiles) so <activation> through properties would not be a valid solution. Solutions using activeByDefault would not be valid either, since activeByDefault is generally known as a pitfall, unreliable (and we may in fact activate other profiles, thus rendering activateByDefault unusable).

非常感谢您的建议.

推荐答案

此类问题的最简单解决方案是使用 maven-enforcer-plugin 正是有这样的规则来强制激活至少两个或多个配置文件中的一个.

The simplest solution for this kind of problem would be to use the maven-enforcer-plugin which exactly has such a rule to force to activate at least one of two or more profiles.

不幸的是,requireActiveProfile 目前有一个错误.但目前正在为新版本做准备以解决这个问题.

Unfortunately the requireActiveProfile has currently a bug. But currently a preparation for a new release is on going which solves this.

更新 上述错误已在 1.4 版(2015 年发布)中修复.

Update The bug mentioned above has been fixed in release 1.4 (which was released in 2015).

这篇关于如何强制使用两个 Maven 配置文件中的一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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