maven之如何使非配置文件的 Maven 构建失败
我同意这个问题有点奇怪。
假设我有一个具有以下配置文件的 Maven 项目。
$ mvn help:all-profiles
Profile Id: profile1 (...)
Profile Id: profile2 (...)
Profile Id: profile3 (...)
$
无论如何,我可以在没有任何特定配置文件选择的情况下强制失败吗?
$ mvn clean package
FAIL
$ mvn -Pprofile1 clean package
SUCCESS
$ mvn -Pprofile2 clean package
SUCCESS
$ mvn -Pprofile3 clean package
SUCCESS
$
更新(个人结论)
同样,这个问题不是一个好问题,即使有人有确切的问题。
初衷是通过明确的配置文件选择来承担构建项目的责任。
@khmarbaise的评论和@AngeloNeuschitzer的回答都可以解决问题。
但在这里我认为我应该更新我最终得到的内容。
正如@khmarbaise 评论的那样,我们可以这样做。
- 制作简介
- 将其中一个配置文件设置为 activeByDefault
$ mvn help:all-profiles
Profile Id: development
Profile Id: integration
Profile Id: staging
Profile Id: production (active by default)
$ mvm clean package
Profile -> production
$ mvn -Pdevelopment clean package
Profile -> development
我最初面临的一个问题是如果有人不小心、错误地为开发构建并部署到生产怎么办?
对于这种情况,我选择默认激活的生产配置文件。使用分类器 是我可以使用的其他方法之一。
请您参考如下方法:
您可以使用 maven-enforcer-plugin当没有配置文件处于事件状态时构建失败。它比拥有一个总是失败的特殊配置文件要干净得多(接受的答案中的代码甚至看起来有点恶意)。
使用内置 requireActiveProfile用 <all>false</all> 规则强制至少有一个配置文件处于事件状态:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<executions>
<execution>
<id>enforce-first-or-second-profile-is-active</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireActiveProfile>
<profiles>first,second</profiles>
<all>false</all>
</requireActiveProfile>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
您甚至可以在配置中添加自定义消息。
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



