mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 02:49:12 +00:00
fix(maven): read Maven 4.1.0 model POMs (#14089)
inspect-maven-gradle-metadata accepted only the http://maven.apache.org/POM/4.0.0 namespace, so every Maven 4 POM in the 4.1.0 model namespace was refused as "not a Maven project POM". Classify a project root in any http://maven.apache.org/POM/ namespace as a POM, as Maven 4's DefaultModelXmlFactory does, and read the 4.0.0 and 4.1.0 models with the same rules. Other model namespaces are refused with a specific unsupported_target reason. A 4.1.0 model imports managed bom-typed rows and carries a limitation that Maven 4 parent and reactor coordinate inference was not applied. Co-authored-by: aadheel <250292080+aadheel@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com>
This commit is contained in:
4 files changed
+241
-21
No files matched your search
@@ -603,6 +603,113 @@ it.each([
|
||||
expectFailure(source, "unsupported");
|
||||
});
|
||||
|
||||
it.each(["4.0.0", "4.1.0"])(
|
||||
"reads a POM in the Maven %s model namespace",
|
||||
(model) => {
|
||||
const report = parse(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/${model}">
|
||||
<modelVersion>${model}</modelVersion>
|
||||
<groupId>g</groupId><artifactId>a</artifactId><version>1</version>
|
||||
<dependencies><dependency><groupId>x</groupId><artifactId>y</artifactId><version>2</version></dependency></dependencies>
|
||||
</project>
|
||||
`);
|
||||
expect(report.format).toBe("maven-pom");
|
||||
expect(report.coordinates).toEqual({
|
||||
group_id: "g",
|
||||
artifact_id: "a",
|
||||
version: "1",
|
||||
packaging: null,
|
||||
});
|
||||
expect(report.dependencies).toEqual([
|
||||
expect.objectContaining({
|
||||
group_id: "x",
|
||||
artifact_id: "y",
|
||||
version: "2",
|
||||
origin: "dependencies",
|
||||
}),
|
||||
]);
|
||||
expect(report.unresolved).toEqual([]);
|
||||
expect(
|
||||
report.limitations.some((limitation) =>
|
||||
limitation.startsWith("Maven 4 can infer omitted parent coordinates"),
|
||||
),
|
||||
).toBe(model === "4.1.0");
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ namespace: "4.1.0", modelVersion: "4.1.0", bom_import: true },
|
||||
{ namespace: "4.1.0", modelVersion: null, bom_import: true },
|
||||
{ namespace: "4.0.0", modelVersion: "4.1.0", bom_import: true },
|
||||
{ namespace: "4.0.0", modelVersion: "4.0.0", bom_import: false },
|
||||
{ namespace: "4.1.0", modelVersion: "4.0.0", bom_import: false },
|
||||
])(
|
||||
"imports a managed bom type only from a 4.1.0 model: namespace $namespace, modelVersion $modelVersion",
|
||||
({ namespace, modelVersion, bom_import }) => {
|
||||
const report =
|
||||
parse(`<project xmlns="http://maven.apache.org/POM/${namespace}">
|
||||
${modelVersion === null ? "" : `<modelVersion>${modelVersion}</modelVersion>`}
|
||||
<artifactId>demo</artifactId>
|
||||
<dependencyManagement><dependencies>
|
||||
<dependency><groupId>com.example</groupId><artifactId>platform</artifactId><version>3</version><type>bom</type></dependency>
|
||||
</dependencies></dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency><groupId>com.example</groupId><artifactId>lib</artifactId><version>3</version><type>bom</type></dependency>
|
||||
</dependencies>
|
||||
</project>`);
|
||||
expect(report.dependency_management).toEqual([
|
||||
expect.objectContaining({ type: "bom", scope: null, bom_import }),
|
||||
]);
|
||||
expect(report.dependencies).toEqual([
|
||||
expect.objectContaining({ type: "bom", bom_import: false }),
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
it("refuses a Maven POM model namespace that this reader does not cover", () => {
|
||||
const source = `<project xmlns="http://maven.apache.org/POM/4.2.0">
|
||||
<modelVersion>4.2.0</modelVersion>
|
||||
<artifactId>demo</artifactId>
|
||||
</project>`;
|
||||
expectFailure(source, "unsupported");
|
||||
expect(() => parse(source)).toThrow(
|
||||
/model namespace this reader does not support; it reads the 4\.0\.0 and 4\.1\.0 POM models/u,
|
||||
);
|
||||
});
|
||||
|
||||
it.each(["4.0.0", "4.1.0"])(
|
||||
"refuses a POM in the Maven %s model namespace that declares another modelVersion",
|
||||
(namespace) => {
|
||||
const source = `<project xmlns="http://maven.apache.org/POM/${namespace}">
|
||||
<modelVersion>4.2.0</modelVersion>
|
||||
<artifactId>demo</artifactId>
|
||||
</project>`;
|
||||
expectFailure(source, "unsupported");
|
||||
expect(() => parse(source)).toThrow(
|
||||
/declares a modelVersion this reader does not support; it reads the 4\.0\.0 and 4\.1\.0 POM models/u,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it("reads a namespace-free POM that declares another modelVersion with the 4.0.0 rules", () => {
|
||||
const report = parse(`<project>
|
||||
<modelVersion>4.2.0</modelVersion>
|
||||
<artifactId>demo</artifactId>
|
||||
<dependencyManagement><dependencies>
|
||||
<dependency><groupId>com.example</groupId><artifactId>platform</artifactId><version>3</version><type>bom</type></dependency>
|
||||
</dependencies></dependencyManagement>
|
||||
</project>`);
|
||||
expect(report.coordinates.artifact_id).toBe("demo");
|
||||
expect(report.dependency_management).toEqual([
|
||||
expect.objectContaining({ type: "bom", bom_import: false }),
|
||||
]);
|
||||
expect(
|
||||
report.limitations.some((limitation) =>
|
||||
limitation.startsWith("Maven 4 can infer"),
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("refuses a POM with two top-level dependencies blocks", () => {
|
||||
expectFailure(
|
||||
`<project>
|
||||
@@ -668,6 +775,17 @@ it("refuses a POM dependency with two version children", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("refuses a POM with two modelVersion children", () => {
|
||||
expectFailure(
|
||||
`<project xmlns="http://maven.apache.org/POM/4.0.0">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<modelVersion>4.1.0</modelVersion>
|
||||
<artifactId>demo</artifactId>
|
||||
</project>`,
|
||||
"malformed",
|
||||
);
|
||||
});
|
||||
|
||||
it("refuses malformed Gradle JSON", () => {
|
||||
expectFailure("{", "malformed");
|
||||
});
|
||||
|
||||
@@ -51,16 +51,21 @@ const soleNamed = (element: Element, local: string): Element | undefined => {
|
||||
return match;
|
||||
};
|
||||
|
||||
const MAVEN_POM_NAMESPACE = "http://maven.apache.org/POM/4.0.0";
|
||||
/** Maven names each POM model namespace by this prefix and its model version. */
|
||||
const MAVEN_POM_NAMESPACE_PREFIX = "http://maven.apache.org/POM/";
|
||||
/** Model versions released Maven reads: 4.0.0, and 4.1.0 for Maven 4 builds. */
|
||||
const MAVEN_POM_MODEL_VERSIONS: readonly string[] = ["4.0.0", "4.1.0"];
|
||||
const MAVEN_4_MODEL_VERSION = "4.1.0";
|
||||
|
||||
/**
|
||||
* A Maven POM root: `project` in the POM namespace, or a namespace-free
|
||||
* A Maven POM root: `project` in a Maven POM namespace, or a namespace-free
|
||||
* `project` that declares `modelVersion` or `artifactId`. Ant and other
|
||||
* `project` vocabularies declare neither and are not POMs.
|
||||
*/
|
||||
const isMavenProjectRoot = (root: Node | null): root is Element => {
|
||||
if (!isXmlElement(root) || root.localName !== "project") return false;
|
||||
if (root.namespaceURI === MAVEN_POM_NAMESPACE) return true;
|
||||
if (root.namespaceURI?.startsWith(MAVEN_POM_NAMESPACE_PREFIX) === true)
|
||||
return true;
|
||||
return (
|
||||
(root.namespaceURI === null || root.namespaceURI === "") &&
|
||||
(xmlFirstChildNamed(root, "modelVersion", "") !== undefined ||
|
||||
@@ -109,6 +114,31 @@ const childText = (element: Element, local: string): string | null => {
|
||||
return child === undefined ? null : mavenText(child);
|
||||
};
|
||||
|
||||
/**
|
||||
* The POM model version: `modelVersion` when declared, otherwise the version
|
||||
* named by the project namespace, as Maven 4 derives it. A namespaced POM must
|
||||
* name a supported model both ways; legacy namespace-free POMs keep any
|
||||
* declared `modelVersion`.
|
||||
*/
|
||||
const mavenModelVersion = (root: Element): string | null => {
|
||||
const namespace = root.namespaceURI ?? "";
|
||||
if (!namespace.startsWith(MAVEN_POM_NAMESPACE_PREFIX))
|
||||
return childText(root, "modelVersion");
|
||||
const namespaceVersion = namespace.slice(MAVEN_POM_NAMESPACE_PREFIX.length);
|
||||
if (!MAVEN_POM_MODEL_VERSIONS.includes(namespaceVersion))
|
||||
throw unsupportedInput(
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
"Maven POM uses a model namespace this reader does not support; it reads the 4.0.0 and 4.1.0 POM models.",
|
||||
);
|
||||
const declared = childText(root, "modelVersion");
|
||||
if (declared !== null && !MAVEN_POM_MODEL_VERSIONS.includes(declared))
|
||||
throw unsupportedInput(
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
"Maven POM declares a modelVersion this reader does not support; it reads the 4.0.0 and 4.1.0 POM models.",
|
||||
);
|
||||
return declared ?? namespaceVersion;
|
||||
};
|
||||
|
||||
/** Keep Gradle JSON spelling for Evidence and exact comparisons. */
|
||||
const gradleText = (value: unknown): string | null =>
|
||||
typeof value === "string" && value.trim() !== "" ? value : null;
|
||||
@@ -215,14 +245,18 @@ const parseMavenDependency = <
|
||||
origin: Origin,
|
||||
index: number,
|
||||
unresolved: MavenGradleUnresolvedCollector,
|
||||
maven4Model: boolean,
|
||||
): MavenGradleDependency<Origin> => {
|
||||
const group_id = childText(element, "groupId");
|
||||
const artifact_id = childText(element, "artifactId");
|
||||
const version = childText(element, "version");
|
||||
const type = childText(element, "type");
|
||||
const scope = childText(element, "scope");
|
||||
// Maven 4 also imports a managed `bom` type, whatever its scope. Only a
|
||||
// 4.1.0 model POM is certain to be built by Maven 4 rather than Maven 3.
|
||||
const bom_import =
|
||||
origin === "dependencyManagement" && type === "pom" && scope === "import";
|
||||
origin === "dependencyManagement" &&
|
||||
((type === "pom" && scope === "import") || (maven4Model && type === "bom"));
|
||||
const exclusions = mavenExclusionElements(element).map((exclusion) => ({
|
||||
group_id: childText(exclusion, "groupId"),
|
||||
artifact_id: childText(exclusion, "artifactId"),
|
||||
@@ -299,6 +333,7 @@ const parsePom = (
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
"Selected XML document is not a Maven project POM.",
|
||||
);
|
||||
const maven4Model = mavenModelVersion(root) === MAVEN_4_MODEL_VERSION;
|
||||
const unresolved = new MavenGradleUnresolvedCollector(
|
||||
limits.unresolved_records,
|
||||
);
|
||||
@@ -377,15 +412,31 @@ const parsePom = (
|
||||
),
|
||||
);
|
||||
const dependency_management = managedElements.map((element, index) =>
|
||||
parseMavenDependency(element, "dependencyManagement", index, unresolved),
|
||||
parseMavenDependency(
|
||||
element,
|
||||
"dependencyManagement",
|
||||
index,
|
||||
unresolved,
|
||||
maven4Model,
|
||||
),
|
||||
);
|
||||
const dependencies = dependencyElements.map((element, index) =>
|
||||
parseMavenDependency(element, "dependencies", index, unresolved),
|
||||
parseMavenDependency(
|
||||
element,
|
||||
"dependencies",
|
||||
index,
|
||||
unresolved,
|
||||
maven4Model,
|
||||
),
|
||||
);
|
||||
if (parent !== null)
|
||||
limitations.push(
|
||||
"Parent coordinates are recorded as spelled; the parent POM was not read and inheritance was not applied.",
|
||||
);
|
||||
if (maven4Model)
|
||||
limitations.push(
|
||||
"Maven 4 can infer omitted parent coordinates and, in model version 4.1.0, omitted dependency and managed dependency group IDs and versions from the local project tree; that inference was not applied.",
|
||||
);
|
||||
if (soleNamed(root, "repositories") !== undefined)
|
||||
limitations.push(
|
||||
"Declared repositories are recorded as unread; they were not contacted.",
|
||||
|
||||
Reference in new issue
Block a user