5

I have a java application with maven having below structure:

parent
| - pom.xml
| - child
    | - pom.xml
| - analyzers
    | - pmdrules.xml
    | - checkstyle.xml

I have configured both PMD and checkstyle in parent pom.xml. For PMD the rulesets are configured as below, and it works fine both for parent and child modules:

<configuration>
    <rulesets>
        <ruleset>${basedir}/../analyzers/pmdrules.xml</ruleset>
    </rulesets>
</configuration>

However, for checkstyle if I configure configLocation in the same way, it fails either in the parent or the child. I have to use a custom property to overcome that.

<configuration>
    <!-- Below config with ${projectRootDir}, a custom property always pointing to parent root works fine -->
    <configLocation>${projectRootDir}/analyzers/checkstyle.xml</configLocation>
    <!-- Below configurations with ${basedir} will fail build for child -->
    <!--<configLocation>${basedir}/analyzers/checkstyle.xml</configLocation>-->
    <!-- Below configurations with ${basedir} will fail build for parent -->
    <!--<configLocation>${basedir}/../analyzers/checkstyle.xml</configLocation>-->
</configuration>

Here is a reproducer sample - https://github.com/ramtech123/pocs/blob/master/myapp-parent-module/pom.xml

I tried running the maven build in debug mode. From the logs, for me it seems like the actual PMD execution is happening for only child module, hence it is going through without issues.

Could someone help me understand the root cause, and improve my configuration.

Thanks in advance.

ramtech
  • 757
  • 6
  • 15

1 Answers1

3

When you create child pom.xml it inherits its parent configuration, but such properties as baseDir are overrided by child, so it uses its own path when templating your plugin configuration.

As an option you can just leave <configLocation>analyzers/checkstyle.xml</configLocation> without {baseDir} or {projectRootDir}, it works fine.

However your solution is also good, as you specify projectRootDir in parent root, and it is evaluated there and child pom doesn't override your custom property, thus its correct.

Your pmd always works because pmd plugin only runs for child module.

Ruslan Akhundov
  • 2,178
  • 4
  • 20
  • 38
  • Wow.. `analyzers/checkstyle.xml` worked like a charm, when I ran the build either from parent of child directory. Thanks for that. Could you please explain a bit more about _pmd plugin only runs for child module_, because this plugin also is listed under `` of the parent pom itself, and I expected it to execute for parent as well, irrespective of packaging type. – ramtech May 02 '18 at 13:59
  • @ramtech unfortunately I don't really know how its implemented, but if you add some not existing file into ruleset pmd would fail with expected exception about file not being found, however that would happen only for child module, the parent module will succeed, the only decent explanation I see, that this plugin ignores parent modules. – Ruslan Akhundov May 02 '18 at 14:01
  • Ok. The actual checkstyle xml that I have in my project has some references to `${config_loc}` property, so I had added this configuration in pom to define that property: `config_loc=${projectRootDir}/analyzers` - the path for directory of checkstyle.xml file. This modified configuration `config_loc=analyzers` resulted in failure, again because of relative path issue. I have anyway the option to update my checkstyle xml. Accepting this as the answer, as it answers original question. Thanks a lot @ruslan-akhundov – ramtech May 02 '18 at 14:17