Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/pmdPublic

How to apply a ruleset only on src/main/*.java files that do not match *Foo*.java?#5217

Answeredbyadangel
cinlloc asked this question inQ&A
Discussion options

Hi, I tried to write a ruleset that match only java files that are insrc/main and does not matchFoo. Seems to be impossible with include/exclude PMD mechanism (exclusions are applied if file does not match a inclusion file). What I tried to use something like:

...<exclude-pattern>.*</exclude-pattern><include-pattern>.*/src/main/java/com/laroueverte/.*(?!Foo).*.java</include-pattern>...

But the(?!Foo) exclusion regex does not seems to work.

You must be logged in to vote

But the (?!Foo) exclusion regex does not seems to work.

Yes, that seems to be the case. But it has something to do with regex, rather than PMD:
https://regex101.com/r/F4JBok/1

An easier way to achieve what you want: Just use exclusion without inclusion:

<exclude-pattern>.*Foo.java|.*/src/test/java/.*</exclude-pattern>

You could remove the "src/test/java"-part, if you set the maven-pmd-plugin parameter<includeTests>false</includeTests>.

Replies: 2 comments 3 replies

Comment options

I've just tested it and it works as expected… how are you running PMD?

You must be logged in to vote
1 reply
@cinlloc
Comment options

Here's the Java project showing the issue :https://github.com/cinlloc/pmd-pattern-issue-demo

I run PMD two ways: with Maven pluginmvn compile pmd:check and with IntelliJ plugin, both give the same result.

I expect only one validation warning onBar.java, but I have two (Foo.java andBar.java, i.e.Foo.java is not excluded as I expect).

The regex seems to be syntactically correct for the parser (no error at execution), but the(?!Foo) part seems to be ignored.

Comment options

But the (?!Foo) exclusion regex does not seems to work.

Yes, that seems to be the case. But it has something to do with regex, rather than PMD:
https://regex101.com/r/F4JBok/1

An easier way to achieve what you want: Just use exclusion without inclusion:

<exclude-pattern>.*Foo.java|.*/src/test/java/.*</exclude-pattern>

You could remove the "src/test/java"-part, if you set the maven-pmd-plugin parameter<includeTests>false</includeTests>.

You must be logged in to vote
2 replies
@jsotuyod
Comment options

Alternatively, a proper include regexp would be:

.*/src/main/java/.*/(?![^/]*Foo[^/]*)[^/]*\.java

Which will not include any file withFoo in the name.
The main issue is the wildcard matches around the negative lookahead in the original regex are basically annulling it, as the regex can decide any number of chars to be matched on each end, so that Foo is never found in the middle…

For instance, givenFoo.java, if the first.* matches 0 chars, Foo is found, and the match is failed, but the Regex can be retried by matching 1 char instead, so the first.* matchesF, the negative lookahead doesn't matchFoo, and the final.* can matchoo, so it's a match.

@cinlloc
Comment options

Ok. Thanks for the answer!

Answer selected byjsotuyod
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@cinlloc@jsotuyod@adangel

[8]ページ先頭

©2009-2025 Movatter.jp