Fork me on GitHub

Usage

Using with Maven Enforcer

Add the following to your project’s pom.xml file:

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.github.mikkoi.maven.enforcer.rule</groupId>
            <artifactId>property-usage-rule</artifactId>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>enforce-property-usage</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <useAllProperties implementation="com.github.mikkoi.maven.plugins.enforcer.rule.propertyusage.PropertyUsageRule">
                        <definitionsOnlyOnce>true</definitionsOnlyOnce>
                        <definedPropertiesAreUsed>true</definedPropertiesAreUsed>
                        <usedPropertiesAreDefined>false</usedPropertiesAreDefined>
                        <reportDuplicateDefinitions></reportDuplicateDefinitions>
                    </useAllProperties>
                </rules>
                <!-- Fail build if wrong encoding encountered. -->
                <fail>true</fail>
            </configuration>
        </execution>
    </executions>
</plugin>
...

The Maven Enforcer plugin needs the <dependencies> element to access custom rules.

It is good to always use a custom rule in a specific <execution> element. Within the <rules> element, you can give any name to the element which holds the implementation clause. In this case, it is called useAllProperties because we specifically want to be sure that all properties defined in src/main/resources/**/*.properties files are also used in source code in src/main/java/**/*.java files. These paths are defaults. The marking **/* means any file in any subdirectory.

The rule has three different functions. These are accessed via the elements <definitionsOnlyOnce>, <definedPropertiesAreUsed> and <usedPropertiesAreDefined. Here we want the simpliest configuration so we only activate <definitionsOnlyOnce> and <definedPropertiesAreUsed>. Default for all is true.

Besides the three functions, user can ask for more verbose reporting with option <reportDuplicateDefinitions>.