Fail Maven build if Maven property is not aligned to convention

Viewed 209

Maven properties in pom.xml of Java repos have to be aligned to some convention for CI processes to work correctly.

For example:

<app-name.prop-name-version>X.X.X.X</app-name-version.prop-name-version>

Is there a way to fail maven builds if maven property is not aligned to convention?

I thought about developing maven plugin from scratch, but is there another way?

1 Answers

Maven Enforcer Plugin exactly does what you need. That has a lot of built-in rules like Require Property According the documentation this rule can enforce the declared property is set and optionally fits for a regex rule.

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
          <execution>
            <id>enforce-property</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireProperty>
                  <property>app-name.prop-name-version</property>
                  <message>"Project version must be specified."</message>
                  <regex>.*[...]$</regex>
                  <regexMessage>"Invalid format."</regexMessage>
                </requireProperty>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
Related