[maven][IDEA-368471] support model version interference - fix tests

GitOrigin-RevId: c522fba072aab50a303a064b7415d3cf3140a651
This commit is contained in:
Alexander Bubenchikov
2025-06-28 23:42:13 +00:00
committed by intellij-monorepo-bot
parent 12d19cc65a
commit 6469f901a1
6 changed files with 58 additions and 21 deletions
@@ -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";
@@ -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<String> {
@@ -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
}
@@ -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?>(MavenDomProjectModel::class.java) {
override fun getGroupDisplayName(): String {
@@ -34,9 +35,9 @@ class MavenModelVersionMissedInspection : BasicDomElementsInspection<MavenDomPro
val projectModel = domFileElement.getRootElement()
if (projectModel.modelVersion.exists()) return
val rootTag = domFileElement.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
if (MavenUtil.isMaven410(
rootTag?.getAttribute("xmlns")?.value,
rootTag?.getAttribute("xsi:schemaLocation")?.value)) return
holder.createProblem(projectModel,
HighlightSeverity.ERROR,
MavenDomBundle.message("inspection.missed.model.version"),
@@ -75,7 +75,7 @@ import org.jetbrains.idea.maven.dom.MavenDomUtil
import org.jetbrains.idea.maven.execution.MavenRunnerSettings
import org.jetbrains.idea.maven.execution.SyncBundle
import org.jetbrains.idea.maven.model.MavenConstants
import org.jetbrains.idea.maven.model.MavenConstants.MODEL_VERSION_4_0_0
import org.jetbrains.idea.maven.model.MavenConstants.*
import org.jetbrains.idea.maven.model.MavenId
import org.jetbrains.idea.maven.model.MavenProjectProblem
import org.jetbrains.idea.maven.project.*
@@ -132,8 +132,10 @@ object MavenUtil {
@ApiStatus.Experimental
const val MAVEN_NAME: @NlsSafe String = "Maven"
@JvmField
val MAVEN_NAME_UPCASE: @NonNls String = MAVEN_NAME.uppercase(Locale.getDefault())
@JvmField
val SYSTEM_ID: ProjectSystemId = ProjectSystemId(MAVEN_NAME_UPCASE)
const val MAVEN_NOTIFICATION_GROUP: String = MAVEN_NAME
@@ -937,7 +939,6 @@ object MavenUtil {
}
@JvmStatic
fun isValidMavenHome(home: Path?): Boolean {
if (home == null) return false
@@ -1130,7 +1131,7 @@ object MavenUtil {
return Path.of(forcedM2Home)
}
val api = if (path == null|| path.getEelDescriptor() is LocalEelDescriptor) localEel else path.getEelApiBlocking()
val api = if (path == null || path.getEelDescriptor() is LocalEelDescriptor) localEel else path.getEelApiBlocking()
val result: Path = api.resolveM2Dir().resolve(REPOSITORY_DIR)
try {
@@ -1624,16 +1625,16 @@ object MavenUtil {
return ModuleRootManager.getInstance(module).getSdk()
}
/* @JvmStatic
fun <K, V : MutableMap<*, *>?> getOrCreate(map: MutableMap<K?, V?>, key: K?): V {
var res = map.get(key)
if (res == null) {
res = HashMap<Any?, Any?>() as V
map.put(key, res)
}
/* @JvmStatic
fun <K, V : MutableMap<*, *>?> getOrCreate(map: MutableMap<K?, V?>, key: K?): V {
var res = map.get(key)
if (res == null) {
res = HashMap<Any?, Any?>() 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
}
}
}
@@ -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
@@ -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() {
<artifactId>foo</artifactId>
</project>
""".trimIndent())
fixture.enableInspections(listOf(MavenModelVersionMissedInspection ::class.java))
checkHighlighting()
}
@Test
fun testAbsentModelVersionFor410XsdNoError() = runBlocking {
fixture.saveText(projectPom,
"""
<project xmlns="http://maven.apache.org/POM/4.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 http://maven.apache.org/xsd/maven-4.1.0.xsd">
<artifactId>foo</artifactId>
</project>
""".trimIndent())
fixture.enableInspections(listOf(MavenModelVersionMissedInspection ::class.java))
checkHighlighting()
}
@Test
fun testAbsentArtifactId() = runBlocking {
fixture.saveText(projectPom,