4

I am trying to add import-control to our checkstyle in such a way that the import-control file exists in the project making the checstyle.xml file and not in the projects we build later on.

We have a specific gradle project where we define all our rules and it is in this project our import-control.xml. My issue is that when I try to run mvn clean install on another project that uses this checkstyle it tries to locate import-control.xml in that project.

I did the following configuration in the checkstyle.xml:

        <module name="ImportControl">
            <property name="file" value="import-control.xml"/>
        </module>

and the import-control.xml is placed next to checkstyle.xml.

Can anyone tell me what I need to do so that I can tell maven that this file exists in our checkstyle project and not in the root project that is being built?

Errors I have gotten are: Cannot initialize module TreeWalker - cannot initialize module ImportControl - illegal value 'import-control.xml' for property 'file' Unable to find: import-control.xml

In v 2.17

Unable to load import-control.xml: unable to find file:/C://import-control.xml: \import-control.xml

What I have tried: Upgrade checkstyle version to 3.1.0 (we used to have 2.17) Use import-control.xml but didn't work. Tried to read documentation and code but to no help.

Thanks for any help

Write you later / Mårten

mvn configuration:

      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>3.1.0</version>
          <executions>
            <execution>
              <id>do checkstyle</id>
              <phase>process-sources</phase>
              <goals>
                <goal>check</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <includes>projectA/**/*</includes>
            <configLocation>checkstyle.xml</configLocation>
            <consoleOutput>true</consoleOutput>
            <failOnViolation>false</failOnViolation>
            <failsOnError>true</failsOnError>
            <includeTestSourceDirectory>true</includeTestSourceDirectory>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>company.checkstyle</groupId>
              <artifactId>company-checkstyle</artifactId>
              <version>0.2-SNAPSHOT</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </pluginManagement>```
  • Just tried it but apparently ${config_loc} was not defined. Is there a way I can/should define it? But thanks for a place to look at for this! – Mårten Carlzon Jul 01 '19 at 05:12
  • It wants an absolute path, e.g. `C:/path/to/import-control.xml`. It seems Maven does not define the variable as most IDE plugins do. You can try setting the absolute path manually, just to verify that the rest works. – barfuin Jul 01 '19 at 10:35
  • Hmmm... I guess one solution would have to be to unpack the checkstyle jar in the buildproject and give a relative path to target since it will be for multiple projects on differnt peoples computers so the path can't be to static. But thanks for the help! – Mårten Carlzon Jul 02 '19 at 11:28

1 Answers1

3

Thanks again barfuin, it seemed like ${config_loc} was the answer but we needed one more thing for it to fully work.

So, for adding resources from the checkstyle project, as in this file an import_control.xml I did as follow in my checkstyle.xml:

    <module name="ImportControl">
        <property name="file" value="${config_loc}/config/import_control.xml"/>
    </module>

What I also needed to do was to add:

<propertyExpansion>config_loc=</propertyExpansion>

in my pom.xml configuration, this solved the issue with config_loc not being defined and for checkstyle to find the file as a resource and gave me the following pom.xml configuration:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <executions>
        <execution>
          <id>do checkstyle</id>
          <phase>process-sources</phase>
          <goals>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <includes>projectA/**/*</includes>
        <configLocation>checkstyle.xml</configLocation>
        <consoleOutput>true</consoleOutput>
        <failOnViolation>false</failOnViolation>
        <failsOnError>true</failsOnError>
        <includeTestSourceDirectory>true</includeTestSourceDirectory>
        <propertyExpansion>config_loc=</propertyExpansion>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>company.checkstyle</groupId>
          <artifactId>company-checkstyle</artifactId>
          <version>0.2-SNAPSHOT</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>