'How to fail when maven profile does not exist?

I have a few maven profiles in my pom.xml. I have jenkins configured to run nightly tests for each of these profiles.

I figured today that there was a spelling mistake in one of the profile names in my jenkins config. Turns out that if maven cannot file a profile, it runs the default profile.

Is there a way I can force maven to throw an error if the profile doesn't exist?



Solution 1:[1]

Maven Enforcer Plugin version 3.0.0-M2 has introduced a built-in check requireProfileIdsExist for this purpose:

When running Maven with one or more unknown profile ids, Maven will give you a warning. This rule will actually break the build for that reason.

Here is how a project should be setup to use this rule

<project>
...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M2</version>
        <executions>
          <execution>
            <id>enforce</id>
            <configuration>
              <rules>
                <requireProfileIdsExist/>
              </rules>
            </configuration>
            <goals>
              <goal>enforce</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

Solution 2:[2]

You can use the Maven Enforcer Plugin

Solution 3:[3]

You can check if a profile exists without configuring Maven Enforcer Plugin in the pom.xml:

> mvn enforcer:enforce -Drules=requireProfileIdsExist -PprofileToCheck
...
[WARNING] The requested profile "profileToCheck" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0:enforce (default-cli) on project idegen: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Shesterov
Solution 2 Alf
Solution 3 Tyutyutyu