2

I'm trying to use Maven Checkstyle plugin in a multi module project. The default setting for <sourceDirectories> (where the plugin to start looking for code) is ${project.compileSourceRoots}. This resolves to [C:\workspace\projectname\src\main\java] in my case, i.e. a List<String>.

Now, that default path is of no value to me, since my code resides in different places, like so: [C:\workspaces\projectname\module1\src\main\java]. Hence, I need to change <sourceDirectories> to a list of directories where my code actually is.

So far, so good...

The problem is that <sourceDirectories> expects a List<String>. I tried the following:

<sourceDirectories>
    <sourceDirectory>pathToCode1</sourceDirectory>
    <sourceDirectory>pathToCode2</sourceDirectory>
</sourceDirectories>

... but that didn't work. It will take the default path. (Moreover, <sourceDirectory> is deprecated!)

Having only one <sourceDirectory> (without the surrounding <sourceDirectories>) does work, but <sourceDirectory> only takes one path and you can't have more than one <sourceDirectory>. So, no cigar. Also, keep in mind <sourceDirectory> is deprecated.

I also tried various other methods of providing a List<String> to <sourceDirectories>, but alas, no progress. Here are some examples:

<sourceDirectories>[pathToCode]</sourceDirectories>
<sourceDirectories>pathToCode</sourceDirectories>
<sourceDirectories>{pathToCode}</sourceDirectories>
<sourceDirectories>{[pathToCode]}</sourceDirectories>
<sourceDirectories>{{pathToCode}}</sourceDirectories>
<sourceDirectories>{{{pathToCode}}}</sourceDirectories>

Is there another way of (directly, without "sub-tags") providing a List<String> to maven?

Is the plugin broken?

Have I missed something?

Edits below

My project structure:

MyProject
|-- pom.xml <-- plugin runs fine here
|-- domain-module
|   |-- src
|   |   `-- main
|   |       `-- com/example/hello...
|   |           |-- TheCode.java
|   |       `-- resources
|   |           |-- checkstyle.xml
|   |           `-- LICENSE.TXT
|   `-- pom.xml
|-- poms
|   |-- parent
|   |   `-- pom.xml <-- this is my parent pom

My parent pom

<dependencies>
    <dependency>
        <groupId>com.puppycrawl.tools</groupId>
        <artifactId>checkstyle</artifactId>
        <version>${checkstyle.version}</version>
    </dependency>    
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.16</version>
                <dependencies>
                    <dependency>
                        <groupId>com.example.hello</groupId>
                        <artifactId>domain-module</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>validate</id>
                        <phase>validate</phase>
                        <configuration>
                            <configLocation>domain-module/src/main/resources/checkstyle.xml</configLocation>
                            <encoding>UTF-8</encoding>
                            <consoleOutput>true</consoleOutput>
                            <failsOnError>false</failsOnError>
                            <failOnViolation>true</failOnViolation>
                            <violationSeverity>warning</violationSeverity>
                            <logViolationsToConsole>true</logViolationsToConsole>
                            <skip>false</skip>
                        </configuration>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <...>
        </build>
    </pluginManagement>
</plugins>
7
  • 1
    Why don't you simply drop sourceDirectories and run the analysis on the parent pom? By default, it looks for all Java classes. Take a look at this example. Commented Sep 14, 2015 at 15:19
  • Thanks for your suggestion! I have read that example and I've tried it. unfortunately it does not work. In fact, Checkstyle won't even start when running the plugin from the parent pom. Maybe it is because my project is not structured like that. Please see my updated question for the parent pom configuration I was using. Commented Sep 14, 2015 at 16:06
  • This is definitely due to your project not following the Maven conventions. IMO, you would save yourself a LOT of trouble by following it! Try with <sourceDirectories> being ../../**/*.java (or something like that) so that it starts at the root of your project Commented Sep 14, 2015 at 17:08
  • That's the problem. ../../**/* doesn't work because the plugin expects a List<String>, not a String. Commented Sep 15, 2015 at 5:59
  • Furthermore I'm not sure what you mean by "Maven conventions". Would you care to elaborate on that? The example I gave was only a very small part of a big project. Commented Sep 15, 2015 at 6:39

1 Answer 1

0

Since version 2.13 the use of sourceDirectories is broken (https://issues.apache.org/jira/browse/MCHECKSTYLE-260).

With 2.12 it works. Andreas Dangel mentioned in Jira:

The configuration described here Checkstyle - Exclude folder still works. Then the exclusion happens in checkstyle, rather than in maven-checkstyle-plugin.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.