GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [pom] Maven POM Rules don't check files named "pom.xml" · Issue #1540 · pmd/pmd

[pom] Maven POM Rules don't check files named "pom.xml" #1540

New issue
Copy link
New issue
Copy link
Open
#4945
Open
[pom] Maven POM Rules don't check files named "pom.xml"#1540
#4945
Copy link
Labels
a:bugPMD crashes or fails to analyse a file.
Milestone
7.x

Description

@kris-scheibe
kris-scheibe
opened on Dec 22, 2018
Issue body actions

Affects PMD Version:
6.10.0

Rule:
all rules for "Maven POM", tested with ProjectVersionAsDependencyVersion

Description:
When running PMD on Maven POM files (by convention named pom.xml), the files aren't being picked up for validation.

The PomLanguageModule seems to define Maven POM files as XML files having a "pom" extension.
Is there a configuration that makes PMD pick up a pom.xml instead of looking for a something.pom?

Code Sample demonstrating the issue:

Given this example pom.xml (a failing example taken from the PMD unit tests)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.sourceforge.pmd</groupId>
    <artifactId>xml-pom</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>x.y.z</groupId>
            <artifactId>z</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

and running PMD with

# should report the dependency version in ~/pmd-pom-test/pom.xml
pmd-bin-6.10.0/bin$ ./run.sh pmd -no-cache -d ~/pmd-pom-test/ -R category/pom/errorprone.xml/ProjectVersionAsDependencyVersion

Doesn't show any violations.

Renaming the file to xml.pom (or really anything ending with .pom) does report the violation:

# does report the dependency version in ~/pmd-pom-test/xml.pom
pmd-bin-6.10.0/bin$ ./run.sh pmd -no-cache -d ~/pmd-pom-test/ -R category/pom/errorprone.xml/ProjectVersionAsDependencyVersion
/home/kris/pmd-pom-test/xml.pom:11:     Do not use project.version to express a dependency version.

Running PMD through: CLI

Activity

  1. kris-scheibe commented on Dec 23, 2018

    @kris-scheibe
    kris-scheibe
    on Dec 23, 2018
    ContributorAuthor
    More actions

    OK, so I've dug a little deeper and it seems that PMD completely relies on the extension for the file-to-language mapping.
    Checking further: Without breaking compatibility (i.e. changing the definition of net.sourceforge.pmd.lang.Language) making the PomLanguageModule pick up pom.xml files doesn't work without some ugly hacks in the language discovery mechanism in pmd-core, essentially returning the extension of pom.xml as pom and when filtering for *.pom files also allowing pom.xml...

    I made the checks work in my builds by automatically copying pom.xml to pmd.pom and replacing the file name in the generated report.

    Reworking the language discovery mechanism might be a candidate for PMD 7.x, though:

    • Add a getFilePatterns() to the Language interface to return glob-style patterns of supported files. With Java 8 this can be non-breaking change by adding a default implementation to return an empty list.
    • For backwards-combatibility: interpret the existing extensions as *.<extension> in addition to defined file patterns.
    • Change the language discovery and file filtering to use the file patterns instead of extensions.
  2. oowekyala commented on Dec 23, 2018

    @oowekyala
    oowekyala
    on Dec 23, 2018
    Member
    More actions

    @kris-scheibe Your analysis is correct, and your proposed change would be welcome in PMD 7.0. Btw, it's been a long standing feature request to make the extension/file pattern mapping configurable by the user (see #461), so I think we should probably design a solution with both issues in mind. For the moment though, I think #461 has too many open questions to start implementing on any of those two fronts (see my recent comment on it)

  3. oowekyala
    added
    an:enhancementAn improvement on existing features / rules
    on Dec 23, 2018
  4. rsoesemann commented on Nov 28, 2019

    @rsoesemann
    rsoesemann
    on Nov 28, 2019
    Member
    More actions

    @oowekyala it just appeared that the Salesforce community could make enormous use of the XML engine and its XPath rules if this would be implemented. Why? Because besides Apex most of the stuff you do in Salesforce is so called XML metadata with well defined nodes.

    Reading through all the history you mentioned I thought why not add this pragmatic extension to PMD 7 with now compatibility issues: Add an extensions attribute to custom rules like this

        <rule name="CustomFieldNamingConvention" 
            language="xml" 
            extensions=".flow,.meta-xml,.object"
            message="Incorrectly named field" 
            class="net.sourceforge.pmd.lang.rule.XPathRule">
            <properties>
                <property name="xpath">
                    <value><![CDATA[
    //CustomField[
                    ]]></value>
                </property>
            </properties>
        </rule>

    if the extensions attribute is not defined everything works as before. If defined rules are only applied by the Tree Walker if the extension matches.

  5. adangel commented on Oct 6, 2022

    @adangel
    adangel
    on Oct 6, 2022
    Member
    More actions

    Note:

    Since PMD 6.37.0 there is a workaround with --force-language and the following execution indeed works:

    pmd-bin-6.50.0/bin/run.sh pmd -d pom.xml -f text -R category/pom/errorprone.xml/ProjectVersionAsDependencyVersion --force-language pom

    You'll need to mention the pom.xml files explicitly (just giving a directory doesn't work as PMD would search for *.pom files...) and add the "--force-language pom" option.

  6. adangel
    added this to the 7.x milestone on Jan 23, 2023
  7. adangel
    added
    a:bugPMD crashes or fails to analyse a file.
    and removed
    an:enhancementAn improvement on existing features / rules
    on Feb 9, 2023
  8. oowekyala
    added a commit that references this issue on Apr 9, 2024

    Add default pattern for POM

    This issue will close once commit a6caf80 is merged into the 'main' branch.
    a6caf80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugPMD crashes or fails to analyse a file.

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.xNo due date

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.