Fork me on GitHub

Require Property Diverges

This rule checks that a property in a project diverges from one given in an ancestor project.

Say you want to enforce a policy in your company's company-parent-pom that all your projects' project.urls should point to a link in your company's wiki. Maven automatically appends the artifactId to the inherited project.url from your company-parent-pom, which will probably just be an invalid url. So you specify that derived projects have to override the project.url property and should not match http://company/wiki/company-parent-pom/.* Or you want to make sure projects do not "reuse" the project.groupId of the company-parent-pom.

The following parameters are supported by this rule:

  • property - name of the property which should diverge. Must be given.
  • regex - match the property value to a given regular expression. When not given, this rule checks that the property in the child does not equal the one from the defining ancestor.

Note that certain properties (e.g. project.url or project.scm.connection are automatically extended with the child's project.artifactId, so using a regex will help in this case, see sample below.

Another caveat: properties defined in the properties section will be overwritten with those from inheriting projects so you have to:

  • define a regex for these as well, see the rule definition for the property just.a.property.with.dots in the mojo-1853 integration test when the property name contains a dot.
  • use an alternative syntax ala project.properties(justAProperty) when the property name does not contain a dot.

Sample Plugin Configuration:

<project>
  <groupId>company</groupId>
  <artifactId>company-parent-pom</artifactId>
  <version>1.0</version>
  <url>http://company/wiki/company-parent-pom</url>
  [...]
  <properties>
    <just.a.property.with.dots>just a value</just.a.property.with.dots>
    <justAPropertyWithoutDots>just another value</justAPropertyWithoutDots>
  </properties>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.0</version> <!-- find the latest version at http://maven.apache.org/plugins/maven-enforcer-plugin/ -->
        <executions>
          <execution>
            <id>enforce-require-property-diverges</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requirePropertyDiverges>
                  <property>project.url</property>
                  <regex>http://company/wiki/company-parent-pom/.*</regex>
                </requirePropertyDiverges>
                <requirePropertyDiverges>
                  <property>project.groupId</property><!-- children must have a diverging groupId -->
                </requirePropertyDiverges>
                <requirePropertyDiverges>
                  <property>just.a.property.with.dots</property>
                  <regex>just a value</regex><!-- need to use a regex here as children's value will override the definition -->
                </requirePropertyDiverges>
                <requirePropertyDiverges>
                  <property>project.properties(justAPropertyWithoutDots)</property>
                </requirePropertyDiverges>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>extra-enforcer-rules</artifactId>
            <version>1.0-beta-6</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Trademarks

Apache, Apache Maven, Maven and the Apache feather logo are trademarks of The Apache Software Foundation.