Shri Javadekar
Shri Javadekar

Reputation: 258

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?

Upvotes: 6

Views: 3478

Answers (3)

Tyutyutyu
Tyutyutyu

Reputation: 359

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

> mvn enforcer:enforce -Denforcer.rules=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]

Upvotes: 1

Alex Shesterov
Alex Shesterov

Reputation: 27525

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>

Upvotes: 6

Alf
Alf

Reputation: 2321

You can use the Maven Enforcer Plugin

Upvotes: 1

Related Questions