From 6469f901a10691e32a143dca5c5aebcd653c063b Mon Sep 17 00:00:00 2001 From: Alexander Bubenchikov Date: Sun, 29 Jun 2025 00:17:42 +0200 Subject: [PATCH] [maven][IDEA-368471] support model version interference - fix tests GitOrigin-RevId: c522fba072aab50a303a064b7415d3cf3140a651 --- .../idea/maven/model/MavenConstants.java | 2 + .../converters/MavenModelVersionConverter.kt | 9 ++-- .../MavenModelVersionMissedInspection.kt | 7 ++-- .../jetbrains/idea/maven/utils/MavenUtil.kt | 41 +++++++++++++------ .../messages/MavenDomBundle.properties | 2 +- .../maven/dom/MavenModelValidationTest.kt | 18 ++++++++ 6 files changed, 58 insertions(+), 21 deletions(-) diff --git a/plugins/maven-server-api/src/main/java/org/jetbrains/idea/maven/model/MavenConstants.java b/plugins/maven-server-api/src/main/java/org/jetbrains/idea/maven/model/MavenConstants.java index 77a1a59e12a5..c521d010766f 100644 --- a/plugins/maven-server-api/src/main/java/org/jetbrains/idea/maven/model/MavenConstants.java +++ b/plugins/maven-server-api/src/main/java/org/jetbrains/idea/maven/model/MavenConstants.java @@ -6,7 +6,9 @@ import java.util.List; public final class MavenConstants { public static final String MAVEN_4_XLMNS = "http://maven.apache.org/POM/4.1.0"; + public static final String MAVEN_4_XLMNS_HTTPS = "https://maven.apache.org/POM/4.1.0"; public static final String MAVEN_4_XSD = "https://maven.apache.org/xsd/maven-4.1.0.xsd"; + public static final String MAVEN_4_XSD_HTTPS = "http://maven.apache.org/xsd/maven-4.1.0.xsd"; public static final String POM_EXTENSION = "pom"; public static final String POM_XML = "pom.xml"; diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/converters/MavenModelVersionConverter.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/converters/MavenModelVersionConverter.kt index 2a59eeb75e07..0905a924ebc1 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/converters/MavenModelVersionConverter.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/converters/MavenModelVersionConverter.kt @@ -20,7 +20,8 @@ import org.jetbrains.annotations.NonNls import org.jetbrains.idea.maven.dom.MavenDomBundle import org.jetbrains.idea.maven.dom.MavenDomUtil.isAtLeastMaven4 import org.jetbrains.idea.maven.model.MavenConstants -import org.jetbrains.idea.maven.model.MavenConstants.* +import org.jetbrains.idea.maven.model.MavenConstants.MODEL_VERSION_4_1_0 +import org.jetbrains.idea.maven.utils.MavenUtil class MavenModelVersionConverter : MavenConstantListConverter() { override fun getValues(context: ConvertContext): Collection { @@ -35,9 +36,9 @@ class MavenModelVersionConverter : MavenConstantListConverter() { override fun fromString(s: @NonNls String?, context: ConvertContext): String? { if (s != null) return super.fromString(s, context) val rootTag = context.file.rootTag - val xmlns = rootTag?.getAttribute("xmlns")?.value - val schemaLocation = rootTag?.getAttribute("xsi:schemaLocation")?.value?.split(' ') - if (xmlns == MAVEN_4_XLMNS && schemaLocation != null && schemaLocation.all { it == MAVEN_4_XLMNS || it == MAVEN_4_XSD }) return MODEL_VERSION_4_1_0 + if (MavenUtil.isMaven410( + rootTag?.getAttribute("xmlns")?.value, + rootTag?.getAttribute("xsi:schemaLocation")?.value)) return MODEL_VERSION_4_1_0 return null } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/inspections/MavenModelVersionMissedInspection.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/inspections/MavenModelVersionMissedInspection.kt index 13acba01990a..96d5f18c0a0c 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/inspections/MavenModelVersionMissedInspection.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/inspections/MavenModelVersionMissedInspection.kt @@ -17,6 +17,7 @@ import org.jetbrains.idea.maven.dom.MavenDomBundle import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel import org.jetbrains.idea.maven.model.MavenConstants.* import org.jetbrains.idea.maven.project.MavenProjectBundle +import org.jetbrains.idea.maven.utils.MavenUtil class MavenModelVersionMissedInspection : BasicDomElementsInspection(MavenDomProjectModel::class.java) { override fun getGroupDisplayName(): String { @@ -34,9 +35,9 @@ class MavenModelVersionMissedInspection : BasicDomElementsInspection?> getOrCreate(map: MutableMap, key: K?): V { - var res = map.get(key) - if (res == null) { - res = HashMap() as V - map.put(key, res) - } + /* @JvmStatic + fun ?> getOrCreate(map: MutableMap, key: K?): V { + var res = map.get(key) + if (res == null) { + res = HashMap() as V + map.put(key, res) + } - return res - }*/ + return res + }*/ @JvmStatic fun isMavenModule(module: Module?): Boolean { @@ -1974,7 +1975,8 @@ object MavenUtil { if (!isRunningFromSources()) return null if (archivedClassesLocation != null && mapping != null) { return mapping["production/$moduleName"]?.toNioPathOrNull() - } else { + } + else { return path?.resolve(moduleName) } } @@ -1983,4 +1985,17 @@ object MavenUtil { fun isRunningFromSources(): Boolean { return path != null && (path.endsWith("production") || path.parent.endsWith("production")) } + + fun isMaven410(xmlns: String?, schemaLocation: String?): Boolean { + if (xmlns == null || schemaLocation == null) return false + val schemaLocations = schemaLocation.split(' ') + return (xmlns == MAVEN_4_XLMNS || xmlns == MAVEN_4_XLMNS_HTTPS) + && schemaLocations.all { + it == MAVEN_4_XLMNS || + it == MAVEN_4_XLMNS_HTTPS || + it == MAVEN_4_XSD || + it == MAVEN_4_XSD_HTTPS + } + + } } diff --git a/plugins/maven/src/main/resources/messages/MavenDomBundle.properties b/plugins/maven/src/main/resources/messages/MavenDomBundle.properties index 1d3b0ee9d895..4e4798bb25a0 100644 --- a/plugins/maven/src/main/resources/messages/MavenDomBundle.properties +++ b/plugins/maven/src/main/resources/messages/MavenDomBundle.properties @@ -82,4 +82,4 @@ plugin.model.doc.expression=Expression: plugin.model.doc.required=Required -inspection.missed.model.version=Model version tag is missed +inspection.missed.model.version='modelVersion' child tag should be defined diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModelValidationTest.kt b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModelValidationTest.kt index c2d9199f1dac..a0323afc82fa 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModelValidationTest.kt +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModelValidationTest.kt @@ -1,6 +1,7 @@ // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.idea.maven.dom +import com.intellij.codeInspection.LocalInspectionTool import com.intellij.openapi.application.EDT import com.intellij.openapi.application.writeIntentReadAction import com.intellij.psi.PsiFile @@ -8,7 +9,10 @@ import com.intellij.testFramework.UsefulTestCase import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext +import org.jetbrains.idea.maven.dom.inspections.MavenModelVersionMissedInspection +import org.jetbrains.idea.maven.dom.inspections.MavenParentMissedVersionInspection import org.junit.Test +import java.lang.Class class MavenModelValidationTest : MavenDomWithIndicesTestCase() { override fun setUp() = runBlocking { @@ -110,9 +114,23 @@ class MavenModelValidationTest : MavenDomWithIndicesTestCase() { foo """.trimIndent()) + fixture.enableInspections(listOf(MavenModelVersionMissedInspection ::class.java)) checkHighlighting() } + @Test + fun testAbsentModelVersionFor410XsdNoError() = runBlocking { + fixture.saveText(projectPom, + """ + + foo + + """.trimIndent()) + fixture.enableInspections(listOf(MavenModelVersionMissedInspection ::class.java)) + checkHighlighting() + } + + @Test fun testAbsentArtifactId() = runBlocking { fixture.saveText(projectPom,