How to disable pom.xml validation in IntelliJ IDEA

2019-01-22 18:57发布

I'm using buildnumber-maven-plugin to get Mercurial changeset as project build number:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.2</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>hgchangeset</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Then I use ${changeSet} property to set build.number property in the same pom.xml:

<build.number>${changeSet}</build.number>

However, IntelliJ IDEA complains that it Cannot resolve symbol 'changeSet':

enter image description here

Also, the module root and the affected pom.xml files are underlined in red in the Project tool window.

I tried all of the offered Alt+Enter options and Maven configurations in the Settings; nothing helped.

Everything builds and works fine, I just want to get rid of the annoying error displayed in the IDE. Any suggestion?

IDE and environment:

IntelliJ IDEA 2016.1.2
Build #IC-145.972, built on May 14, 2016
JRE: 1.8.0_65-b17 amd64
JVM: Java HotSpot(TM) 64-Bit Server VM by Oracle Corporation

4条回答
甜甜的少女心
2楼-- · 2019-01-22 19:22

The question asks for how to deactivate the verification because of the unfixed IntelliJ bug causing false errors for the syntax "${dynamic.variable}" in dynamic pom.xml variables.

Instead, here is a workaround for the bug, so the false error notification disappears and the maven still accepts the variable and everyone is happy:

Change variable syntax in this order (no joke): 1. "${dynamic.variable}" 2. "{dynamic.variable}" 3. "{$dynamic.variable}"

IntelliJ will now accept it as correct and maven will also parse and accept the dynamic variable without problem. It is obviously an undocumented maven syntax which for some reason has been implemented as the correct and only syntax in IntelliJ IDE.

I hope this helps everyone, despite it not answering the actually asked question, but eliminates the reason for asking it instead.

Have fun :)

查看更多
一夜七次
3楼-- · 2019-01-22 19:29

Yeah, that looks broken

Interesting. I've tested this behavior in both IntelliJ IDEA Ultimate 14.1.7 as well as IntelliJ IDEA Community 2016.1.2, and properties that IDEA doesn't know about still show as red even with all Maven inspections turned off. This looks to be a bug (or to put it more charitably, an unimplemented feature) in IDEA, that the error it gives when it can't identify a property isn't controlled by the inspections settings.

There are a couple related issues in IDEA's bug tracker:

There's also a post on a similar issue on the JetBrains support forum:

A possible workaround

There's a workaround listed in both the comments of IDEA-96358 as well as in the support post. You can add a dummy version of your dynamic properties in Settings / Build, Execution, Development / Build Tools / Maven / Runner / Properties, like in this screenshot: Screenshot of setting Maven Runner Properties

Putting values in there will let IDEA know about them so it will not highlight them in red. However, I found in my testing that since IDEA was passing the values as properties to Maven, they weren't getting overwritten by the buildnumber-plugin and I was seeing my dummy value instead when I tried to use it. (The only way I tested using it was in a maven-help-plugin evaluate goal, but perhaps it works when using it in other ways?) In order to be able to use the dynamically-set value, in my specific build configuration I unchecked the "Use project settings" box and removed the dummy property, as in this screenshot: Screenshot of removing property from specific build configuration

This is starting to be quite an annoying workaround, as if you have a lot of Maven build configurations and like the ability for them all to default to the same runner settings, you'd now have to duplicate the "real" setup you want in each configuration, and have the default in the project settings dialog just be for the "dummy" values so that IDEA knows that it shouldn't treat referencing that property as an error. This also requires you to run all your Maven commands via build configurations, and not via other means like double-clicking lifecycles in the "Maven Projects" tool window. Though if you have few build configurations, those are the only way you run Maven from within IDEA, and the color red when editing your pom.xml file is annoying you enough, perhaps this workaround is an acceptable tradeoff for you.

Further steps

As it appears that IDEA's built-in functionality doesn't really cover dynamically-generated properties, I think the only real way to fix it is to convince JetBrains to do it, unless there's some way to add the functionality in a third-party plugin (which is now very much getting beyond my expertise). I'd suggest either voting for and commenting on one of the tickets in JIRA's tracking system I mentioned, or creating your own if you think your issue is different enough (which it may be). Also, you may want to contact JetBrains support, particularly if you have a paid subscription with them. While I suspect that they have many priorities to work on (as do we all), I also suspect that more people asking for something increases the chance of it moving toward the top of their backlog. Asking nicely for something rarely hurts.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-22 19:35

Try File->Settings->Editor->Inspections->uncheck "Maven Model Inspection" and apply.

查看更多
冷血范
5楼-- · 2019-01-22 19:36

As it is working fine. I think it would be some cache or may be little bug which has no major effect giving some info. So I want to suggest you some options

Suggestion-1:

You can rebuild your project and then check

Build > Rebuild Project

Suggestion-2:

You can clear your ide's caches

  1. File -> invalidate caches
  2. Then Restart application

Suggestion-3:

Sometimes silly trick may solve the issue.

  1. Right click on the code editor
  2. Hover on Maven and expand
  3. Click on Reimport

Suggestion-4:

There are some ways to solve the issue and also discussion what is happening version to version. You can go through

IntelliJ inspection gives "Cannot resolve symbol" but still compiles code

Suggestion-5:

You can make a try Michał Wróbel's blog: Cannot resolve symbol XYZ in IntelliJ although maven builds sources properly

Suggestion-6:

You can also disable reading pom.xml on every small changes in IntelliJ Idea: Intellij IDEA and Maven, disable reading pom.xml on every small change, even without a save

查看更多
登录 后发表回答