Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.ModelVersions.MODEL_VERSION_4_0_0;
import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.ModelVersions.MODEL_VERSION_4_1_0;
import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.Namespaces.MAVEN_4_0_0_NAMESPACE;
import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.Namespaces.MAVEN_4_1_0_NAMESPACE;
import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.SchemaLocations.MAVEN_4_1_0_SCHEMA_LOCATION;
import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.XmlElements.MODEL_VERSION;

Expand All @@ -40,7 +38,7 @@ private ModelVersionUtils() {

/**
* Detects the model version from a POM document.
* Uses both the modelVersion element and namespace URI for detection.
* Uses the modelVersion element for detection.
*
* @param pomDocument the POM document
* @return the detected model version
Expand All @@ -49,7 +47,7 @@ public static String detectModelVersion(Document pomDocument) {
Element root = pomDocument.getRootElement();
Namespace namespace = root.getNamespace();

// First try to get from modelVersion element
// Try to get from modelVersion element
Element modelVersionElement = root.getChild(MODEL_VERSION, namespace);
if (modelVersionElement != null) {
String modelVersion = modelVersionElement.getTextTrim();
Expand All @@ -58,14 +56,6 @@ public static String detectModelVersion(Document pomDocument) {
}
}

// Fallback to namespace URI detection
String namespaceUri = namespace.getURI();
if (MAVEN_4_1_0_NAMESPACE.equals(namespaceUri)) {
return MODEL_VERSION_4_1_0;
} else if (MAVEN_4_0_0_NAMESPACE.equals(namespaceUri)) {
return MODEL_VERSION_4_0_0;
}

// Default fallback
return MODEL_VERSION_4_0_0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ private Files() {
* Maven namespace constants.
*/
public static final class Namespaces {
/** Maven 4.0.0 namespace URI */
/** Maven namespace URI */
public static final String MAVEN_4_0_0_NAMESPACE = "http://maven.apache.org/POM/4.0.0";

/** Maven 4.1.0 namespace URI */
public static final String MAVEN_4_1_0_NAMESPACE = "http://maven.apache.org/POM/4.1.0";
/** Maven namespace URI */
public static final String MAVEN_4_1_0_NAMESPACE = "http://maven.apache.org/POM/4.0.0";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so no need for this const


private Namespaces() {
// Utility class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class DependencyInferenceTests {
@DisplayName("should remove dependency version for project artifact")
void shouldRemoveDependencyVersionForProjectArtifact() throws Exception {
String parentPomXml = PomBuilder.create()
.namespace("http://maven.apache.org/POM/4.1.0")
.namespace("http://maven.apache.org/POM/4.0.0")
.modelVersion("4.1.0")
.groupId("com.example")
.artifactId("parent-project")
Expand All @@ -140,7 +140,7 @@ void shouldRemoveDependencyVersionForProjectArtifact() throws Exception {
.build();

String moduleAPomXml = PomBuilder.create()
.namespace("http://maven.apache.org/POM/4.1.0")
.namespace("http://maven.apache.org/POM/4.0.0")
.modelVersion("4.1.0")
.parent("com.example", "parent-project", "1.0.0")
.artifactId("module-a")
Expand All @@ -149,7 +149,8 @@ void shouldRemoveDependencyVersionForProjectArtifact() throws Exception {
String moduleBPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
Expand Down Expand Up @@ -202,7 +203,7 @@ void shouldKeepDependencyVersionForExternalArtifact() throws Exception {
String modulePomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<groupId>com.example</groupId>
<artifactId>my-module</artifactId>
<version>1.0.0</version>
Expand Down Expand Up @@ -237,15 +238,15 @@ void shouldKeepDependencyVersionForExternalArtifact() throws Exception {
@DisplayName("should keep dependency version when version mismatch")
void shouldKeepDependencyVersionWhenVersionMismatch() throws Exception {
String moduleAPomXml = PomBuilder.create()
.namespace("http://maven.apache.org/POM/4.1.0")
.namespace("http://maven.apache.org/POM/4.0.0")
.modelVersion("4.1.0")
.groupId("com.example")
.artifactId("module-a")
.version("1.0.0")
.build();

String moduleBPomXml = PomBuilder.create()
.namespace("http://maven.apache.org/POM/4.1.0")
.namespace("http://maven.apache.org/POM/4.0.0")
.modelVersion("4.1.0")
.groupId("com.example")
.artifactId("module-b")
Expand Down Expand Up @@ -283,7 +284,8 @@ void shouldHandlePluginDependencies() throws Exception {
String moduleAPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>module-a</artifactId>
<version>1.0.0</version>
Expand All @@ -293,7 +295,8 @@ void shouldHandlePluginDependencies() throws Exception {
String moduleBPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>module-b</artifactId>
<version>1.0.0</version>
Expand Down Expand Up @@ -350,7 +353,7 @@ void shouldRemoveParentGroupIdWhenChildDoesntHaveExplicitGroupId() throws Except
String parentPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
Expand All @@ -361,7 +364,7 @@ void shouldRemoveParentGroupIdWhenChildDoesntHaveExplicitGroupId() throws Except
String childPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<parent>
<groupId>com.example</groupId>
Expand Down Expand Up @@ -407,7 +410,7 @@ void shouldKeepParentGroupIdWhenChildHasExplicitGroupId() throws Exception {
String parentPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
Expand All @@ -418,7 +421,7 @@ void shouldKeepParentGroupIdWhenChildHasExplicitGroupId() throws Exception {
String childPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<parent>
<groupId>com.example</groupId>
Expand Down Expand Up @@ -459,7 +462,7 @@ void shouldNotTrimParentElementsWhenParentIsExternal() throws Exception {
String childPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -500,7 +503,7 @@ void shouldTrimParentElementsWhenParentIsInReactor() throws Exception {
String parentPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
Expand All @@ -513,7 +516,7 @@ void shouldTrimParentElementsWhenParentIsInReactor() throws Exception {
String childPomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<parent>
<groupId>com.example</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ void shouldHandleVariousModelVersionUpgradeScenarios(
String initialNamespace,
String initialModelVersion,
String targetModelVersion,
String expectedNamespace,
String expectedModelVersion,
int expectedModifiedCount,
String description)
Expand All @@ -158,7 +157,6 @@ void shouldHandleVariousModelVersionUpgradeScenarios(

// Verify the model version and namespace
Element root = document.getRootElement();
assertEquals(expectedNamespace, root.getNamespaceURI(), "Namespace should be updated: " + description);

Element modelVersionElement = root.getChild("modelVersion", root.getNamespace());
if (expectedModelVersion != null) {
Expand All @@ -176,83 +174,29 @@ private static Stream<Arguments> provideUpgradeScenarios() {
"http://maven.apache.org/POM/4.0.0",
"4.0.0",
"4.1.0",
"http://maven.apache.org/POM/4.1.0",
"4.1.0",
1,
"Should upgrade from 4.0.0 to 4.1.0"),
Arguments.of(
"http://maven.apache.org/POM/4.1.0",
"http://maven.apache.org/POM/4.0.0",
"4.1.0",
"4.1.0",
"http://maven.apache.org/POM/4.1.0",
"4.1.0",
0,
"Should not modify when already at target version"),
Arguments.of(
"http://maven.apache.org/POM/4.0.0",
null,
"4.1.0",
"http://maven.apache.org/POM/4.1.0",
"4.1.0",
1,
"Should add model version when missing"));
}
}

@Nested
@DisplayName("Namespace Updates")
class NamespaceUpdateTests {

@Test
@DisplayName("should update namespace recursively")
void shouldUpdateNamespaceRecursively() throws Exception {
String pomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
""";

Document document = saxBuilder.build(new StringReader(pomXml));
Map<Path, Document> pomMap = Map.of(Paths.get("pom.xml"), document);

// Create context with --model-version=4.1.0 option to trigger namespace update
UpgradeOptions options = mock(UpgradeOptions.class);
when(options.modelVersion()).thenReturn(Optional.of("4.1.0"));
when(options.all()).thenReturn(Optional.empty());
UpgradeContext context = createMockContext(options);

UpgradeResult result = strategy.apply(context, pomMap);

assertTrue(result.success(), "Model upgrade should succeed");
assertTrue(result.modifiedCount() > 0, "Should have upgraded namespace");

// Verify namespace was updated recursively
Element root = document.getRootElement();
Namespace newNamespace = Namespace.getNamespace("http://maven.apache.org/POM/4.1.0");
assertEquals(newNamespace, root.getNamespace());

// Verify child elements namespace updated recursively
Element dependencies = root.getChild("dependencies", newNamespace);
assertNotNull(dependencies);
assertEquals(newNamespace, dependencies.getNamespace());

Element dependency = dependencies.getChild("dependency", newNamespace);
assertNotNull(dependency);
assertEquals(newNamespace, dependency.getNamespace());

Element groupId = dependency.getChild("groupId", newNamespace);
assertNotNull(groupId);
assertEquals(newNamespace, groupId.getNamespace());
}
@DisplayName("Model Version Updates")
class ModelVersionUpdateTests {

@Test
@DisplayName("should convert modules to subprojects in 4.1.0")
Expand Down Expand Up @@ -334,7 +278,7 @@ void shouldFailWhenAttemptingDowngrade() throws Exception {
String pomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.1.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void shouldDetectModelVersionFromDocument() throws Exception {
@DisplayName("should detect 4.1.0 model version")
void shouldDetect410ModelVersion() throws Exception {
String pomXml = PomBuilder.create()
.namespace("http://maven.apache.org/POM/4.1.0")
.namespace("http://maven.apache.org/POM/4.0.0")
.modelVersion("4.1.0")
.groupId("test")
.artifactId("test")
Expand Down Expand Up @@ -104,24 +104,6 @@ void shouldReturnDefaultVersionWhenModelVersionMissing() throws Exception {
String result = ModelVersionUtils.detectModelVersion(document);
assertEquals("4.0.0", result); // Default version
}

@Test
@DisplayName("should detect version from namespace when model version is missing")
void shouldDetectVersionFromNamespaceWhenModelVersionMissing() throws Exception {
String pomXml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0">
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
</project>
""";

Document document = saxBuilder.build(new StringReader(pomXml));
String result = ModelVersionUtils.detectModelVersion(document);
assertEquals("4.1.0", result);
}
}

@Nested
Expand Down Expand Up @@ -417,7 +399,7 @@ void shouldHandleMissingModelVersion() throws Exception {
@ValueSource(
strings = {
"http://maven.apache.org/POM/4.0.0",
"http://maven.apache.org/POM/4.1.0",
"http://maven.apache.org/POM/4.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate of 401?

"https://maven.apache.org/POM/4.0.0",
"https://maven.apache.org/POM/4.1.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is also 4.1

})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void shouldUpgradeModelVersionWith41Option() throws Exception {
// Verify POM was upgraded
String upgradedPom = Files.readString(pomFile);
assertTrue(
upgradedPom.contains("http://maven.apache.org/POM/4.1.0"),
upgradedPom.contains("http://maven.apache.org/POM/4.0.0"),
"POM should be upgraded to 4.1.0 namespace");
}

Expand Down
5 changes: 3 additions & 2 deletions impl/maven-core/src/test/resources/consumer/simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project root="true" 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 https://maven.apache.org/xsd/maven-4.1.0.xsd">
<project root="true" 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 https://maven.apache.org/xsd/maven-4.1.0.xsd">
<modelVersion>4.1.0</modelVersion>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>parent</artifactId>
<version>0.9-${changelist}-SNAPSHOT</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<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 https://maven.apache.org/xsd/maven-4.1.0.xsd">
<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 https://maven.apache.org/xsd/maven-4.1.0.xsd">
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>parent</artifactId>
</parent>
<modelVersion>4.1.0</modelVersion>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<name>Multi Chapter Simple Parent Project</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<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 https://maven.apache.org/xsd/maven-4.1.0.xsd">
<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 https://maven.apache.org/xsd/maven-4.1.0.xsd">
<modelVersion>4.1.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>utils-parent</artifactId>
Expand Down
Loading