Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 292
Shri Javadekar Avatar asked Apr 02 '14 18:04

Shri Javadekar


People also ask

Why is the use of the profile required in Maven?

A profile in Maven is an alternative set of configuration values which set or override default values. Using a profile, you can customize a build for different environments. Profiles are configured in the pom. xml and are given an identifier.

What is Mvn profile?

Maven profiles can be used to create customized build configurations, like targeting a level of test granularity or a specific deployment environment. In this tutorial, we'll learn how to work with Maven profiles.


2 Answers

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>
like image 61
Alex Shesterov Avatar answered Sep 28 '22 00:09

Alex Shesterov


You can use the Maven Enforcer Plugin

like image 35
Alf Avatar answered Sep 28 '22 01:09

Alf