简体   繁体   English

如何使非配置文件maven构建失败?

[英]How to make non-profiled maven build fail?

I would agree that this question is somewhat weird. 我同意这个问题有点奇怪。

Let's say I have a maven project which has following profiles. 假设我有一个maven项目,其中包含以下个人资料。

$ mvn help:all-profiles
Profile Id: profile1 (...)
Profile Id: profile2 (...)
Profile Id: profile3 (...)
$

Is there anyway that I can force to fail without any specific profile selection? 无论如何,如果没有任何特定的配置文件选择,我可以强制失败吗?

$ mvn clean package
FAIL
$ mvn -Pprofile1 clean package
SUCCESS
$ mvn -Pprofile2 clean package
SUCCESS
$ mvn -Pprofile3 clean package
SUCCESS
$

UPDATE(with personal conclusion) 更新(个人结论)

Again, this question is not a good one even if there would be someone who has the exact problem. 同样,即使有人确实存在问题,这个问题也不是一个好问题。

The original intention is charging the responsibility who build the project with explicit profile selection. 最初的意图是通过明确的配置文件选择来负责构建项目的责任。

Both the comment of @khmarbaise and the answer of @AngeloNeuschitzer are OK to solve the problem. @khmarbaise的评论和@AngeloNeuschitzer的答案都可以解决问题。

But here I think I should update what I ended up with. 但在这里,我认为我应该更新我最终的结果。

As @khmarbaise commented, we can do like this. 正如@khmarbaise评论的那样,我们可以这样做。

  • Make profiles 制作档案
  • Make one of those profiles activeByDefault 使其中一个配置文件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

One of original problem that I faced is what if anyone build for development and deploy to production, accidentally, by fault? 我面临的一个原始问题是,如果有人为了开发而构建,并且由于故障而意外地部署到生产中?

I choose the production profile active by default for this case. 对于这种情况,我选择默认激活的生产配置文件。 Using the classifier is one of additional method I can use. 使用分类器是我可以使用的另一种方法之一。

Add a profile that does something that has to fail on clean and make it activeByDefault . 添加一个配置文件,该配置文件执行必须在clean上失败的内容并使其成为activeByDefault You may even want to write a plugin that does nothing but fail on you for this. 您甚至可能想要编写一个插件,除此之外什么都不会失败。

CAREFUL ! 小心点 I wrote this without making certain that the directory '/' is really undeleteable in all circumstances. 我写的这个没有确定目录'/'在所有情况下都是不可删除的。 This could result in severe data loss , use at you own risk. 这可能导致严重的数据丢失 ,使用风险自负。 (It works on my machine, though) (虽然它适用于我的机器)

    <profile>
        <id>failure</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <directory>#!?:</directory>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>clean</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <failOnError>true</failOnError>
                        <filesets>
                            <fileset>
                                <directory>/</directory>
                            </fileset>
                        </filesets>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

You can use the maven-enforcer-plugin to fail a build when no profile is active. 当没有配置文件处于活动状态时,您可以使用maven-enforcer-plugin来使构建失败。 It's much cleaner than having a special profile that always fails (the code in the accepted answer even looks kind of malicious). 它比一个总是失败的特殊配置文件更清晰(接受的答案中的代码甚至看起来有点恶意)。

Use the build-in requireActiveProfile rule with <all>false</all> to enforce that at least one profile is active: 使用内置的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>

You can even add a custom message in the configuration. 您甚至可以在配置中添加自定义消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM