Search code examples
mavencheckstylemaven-checkstyle-plugin

maven error "Property ${samedir} has not been set"


How to fix the maven checkstyle plugin error

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.2:check (validate) on project yourproject: 
Failed during checkstyle execution: 
Failed during checkstyle configuration: unable to parse configuration stream:
com.puppycrawl.tools.checkstyle.api.CheckstyleException: 
Property ${samedir} has not been set -> [Help 1]

${samedir} is a property working well with eclipse plugin and is required to get related files mentioned in the checkstyle configuration working well with eclipse IDE. So either I need a consistent replacement, or I can tell maven to define the property

Any ideas how to fix this issue?


Solution

  • You will have to set these values via the plugin configuration in your pom.

    Example configuration of pom.xml if you have a directory named checks in your project root:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>3.1.2</version>
        <configuration>
            <configLocation>${basedir}/checks/checkstyle.xml</configLocation>
            <propertiesLocation>${basedir}/checks/checkstyle_maven.properties</propertiesLocation>
        </configuration>
    </plugin>
    

    And the checkstyle_maven.properties contents:

    samedir=checks
    

    As an alternative to the <propertiesLocation> you can also use the following, in which case you don't need the .properties file:

    <propertyExpansion>samedir=${basedir}/checks</propertyExpansion>
    

    This will make sure any configuration in checkstyle.xml will work like this:

    <module name="Checker">
        <module name="SuppressionFilter">
            <property name="file" value="${samedir}/suppress.xml"/>
        </module>
    </module>
    

    See for reference: