4

I'm trying to share the same pmd configuration across all my submodules. I'm looking for the best way to achieve that

I thought that I could place it in the parent project, like I did it for checkstyle plugin

Parent pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.9.1</version>
            <configuration>
                 <configLocation>/src/main/config/checkstyle.xml</configLocation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>2.7.1</version>
            <configuration>
                <rulesets>
                    <ruleset>pmd.xml</ruleset>
                </rulesets>
                <linkXref>true</linkXref>
                <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
                <targetJdk>${maven.compiler.target}</targetJdk>
            </configuration>
        </plugin>
    </plugins>
</build>

Multi Module Structure

Here my structure

parent
|-- src
|   `-- main
|       `-- resources
|           |-- pmd.xml
|           `-- checkstyle.xml
|-- pom.xml
|-- model
|   `-- pom.xml
`-- webapp
    `-- pom.xml

Error

With this configuration, I only obtain the following error :

 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:2.7.1:pmd (default-cli) on project model: An error has occurred in PMD Report report generation. Could not find resource 'pmd.xml'. -> [Help 1]

Solution 1

I tried this solution that works for me, because I only have one level on submodules : But in a close future, I may have more levels, so I'm not convinced that's THE method ${basedir}/../src/main/resources/pmd.xml

Solution 2

Without writing all the solution, I can use an assembly to zip my config and use it in all my submodule as an dependency. This would work for any levels, but this is overkilling !

Here a link that would explain it: How to include resources from war to another maven project

So I'm looking for a Maven trick, but I don't what or how ! Every advice / clue is welcome.

Community
  • 1
  • 1
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65

2 Answers2

3

Have a separate module that contains those generic config files like build-tools. Then you can add this module as a dependency to your plugin config and load it.

I have implemented an example of this with a checkstyle config file across a multiple modules in the ksoap2-android project.

https://github.com/mosabua/ksoap2-android/blob/master/pom.xml

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • I will try. That's already working for checkstyle in my projects, but not for PMD, in pretty the same conf. – Jean-Rémy Revy Feb 15 '13 at 10:36
  • 1
    Hm... maybe the PMD plugin can not pick up the config .. might have to debug the plugin and see. Worst case provide a fix. Upgrade to the latest version too just in case.. – Manfred Moser Feb 15 '13 at 22:46
  • I was facing an issue yesterday, with maven-checkstyle-plugin. The 2.4 version did picked up parent configuration ... Upgrading to 2.9.1 solved the issue. This would confirm your analysis ... maybe be a bug / feature to declare ! – Jean-Rémy Revy Mar 28 '13 at 10:12
  • As soon as this may be a bug, and assuming that I don't want to depend on an http accessible scm repository, your solution seems to be the more accurate. Indeed, it deals more with maven internal mechanism, and it may be more reliable. – Jean-Rémy Revy Mar 28 '13 at 10:14
  • Just as a side note for readers, this is the approach suggested by PMD: https://maven.apache.org/plugins/maven-pmd-plugin/examples/multi-module-config.html And there we have a link to the 'Multimodule Configuration for Checkstyle' example, which uses the same approach. – Gerardo Roza Jul 11 '19 at 02:29
  • @GerardoRoza followed the documentation from maven site, but its not working as per expectation – SUMIT Aug 18 '21 at 10:38
1

I was reading the documentation for the ruleset tag and it says

The rule sets may reside in the classpath, filesystem or at a URL. For rule sets that are bundled with the PMD tool, you do not need to specificy the absolute path of the file. It will be resolved by the plugin. But if the rule set is a custom rule set, you need to specify its absolute path.

I solved the problem of sharing my PMD settings across my multi-maven build by specifying the URL to the ruleset file in my parent pom. (My repo was http accessible). Unfortunately this won't work though if you don't have your repo http accessible, but many people may.

dev
  • 2,949
  • 5
  • 37
  • 48
  • You mean ... your SCM repo ? Like http://repo.svn.corp/project/trunk/parent/src/main/config/checkstyle.xml ?Hmmm quite interesting work around ... – Jean-Rémy Revy Mar 28 '13 at 10:06
  • Or better ${project.scm.url}/parent/src/main/config/checkstyle.xml ? – Jean-Rémy Revy Mar 28 '13 at 10:10
  • @Jean-RémyRevy correct, scm repo. i think Manfred's answer would be the best practice, but this is just another option for those who may be interested – dev Mar 29 '13 at 04:40
  • Yes for sure, your workaround is pretty clean ! I was looking for any solution, and yours is really interesting ! That's why I voted it up :) – Jean-Rémy Revy Mar 29 '13 at 08:55
  • friend, can you please share this(\ or \ or \) part the your pom.xml code. thanks. It help, more, clears more confusions. – Sun Sep 17 '13 at 04:44