diff --git a/docs/maven-gradle-metadata.md b/docs/maven-gradle-metadata.md index 65c6fbabf8..0d4a6347e6 100644 --- a/docs/maven-gradle-metadata.md +++ b/docs/maven-gradle-metadata.md @@ -21,9 +21,15 @@ requires an absolute filesystem path and preserves that exact selection: } ``` -REA classifies files by their content. A Maven `` uses the Maven POM -namespace or legacy unqualified elements. REA reads structural declarations -in the project's namespace. A Gradle metadata object starts with `formatVersion`. +REA classifies files by their content. A Maven `` uses a Maven POM +namespace or legacy unqualified elements. REA reads the 4.0.0 and +[4.1.0](https://maven.apache.org/whatsnewinmaven4.html) POM models. A project +in another `http://maven.apache.org/POM/` model namespace is +`unsupported_target`, and so is a project in a Maven POM namespace whose +`modelVersion` names another model. A legacy unqualified project that declares +another `modelVersion` is read with the 4.0.0 model rules. REA reads structural +declarations in the project's namespace. A Gradle metadata object starts with +`formatVersion`. Version `1.1` can omit `component` and `variants`. For other revisions, REA also requires a `component` object or `variants` array as format evidence. [Gradle Module Metadata 1.1 specification](https://github.com/gradle/gradle/blob/v9.2.1/platforms/documentation/docs/src/docs/design/gradle-module-metadata-latest-specification.md#contents) @@ -40,10 +46,11 @@ spelled. Missing project coordinates stay `null`. A `${property}` spelling is kept as written and listed in `unresolved`; this reader does not interpolate properties or inherit parent or BOM values. POM text goes through XML end-of-line handling, as Maven's reader applies it: CRLF and lone CR read as LF. -POM coordinates, parent fields, dependency fields, and property values use -Maven's trimming rules, retaining non-ASCII characters at their edges. -Coordinate, parent, dependency and property values can contain text and CDATA. -REA reports nested elements inside those values as malformed input. +POM coordinates, `modelVersion`, parent fields, dependency fields, and +property values use Maven's trimming rules, retaining non-ASCII characters at +their edges. Coordinate, `modelVersion`, parent, dependency and property values +can contain text and CDATA. REA reports nested elements inside those values as +malformed input. When a parent is declared, omitted project group/version fields point to the parent as possible sources. An omitted project `artifactId` is a missing child coordinate: [only group/version coordinates may be inherited](https://maven.apache.org/pom.html#Maven_Coordinates). @@ -60,12 +67,17 @@ is `true`; when the credential span cannot be located, `value` is `null`. `dependencies` comes from the project's `` list. `dependency_management` comes from ``. A managed dependency with `type` `pom` and `scope` `import` is a BOM import -(`bom_import: true`). Each row keeps scope, type, optional, classifier, and -exclusions as present. `optional` is the interpreted boolean and -`optional_spelling` keeps the text as written, so a `${property}` value stays -visible and is listed in `unresolved` with `field` `optional`. `version_kind` -is `literal`, `range`, `dynamic`, `property`, or `missing`; `dynamic` is -Gradle's `+` prefix (`1.+`) or a `latest.` selector +(`bom_import: true`). Maven 4 also imports a managed dependency with `type` +`bom`, whatever its scope. REA marks that row as a BOM import only in a 4.1.0 +model POM, which Maven 3 cannot build. In a 4.0.0 model POM, such a row stays +`bom_import: false`, as Maven 3 reads it, and no limitation is added. The model +version is `modelVersion` when declared, otherwise the namespace version. Each +row keeps scope, type, optional, classifier, and exclusions as present. +`optional` is the interpreted boolean and `optional_spelling` keeps the text as +written, so a `${property}` value stays visible and is listed in `unresolved` +with `field` `optional`. +`version_kind` is `literal`, `range`, `dynamic`, `property`, or `missing`; +`dynamic` is Gradle's `+` prefix (`1.+`) or a `latest.` selector (`latest.release`). For Gradle module metadata, `gradle` records `format_version`, @@ -118,8 +130,15 @@ selected file-read budget; raising `max_file_bytes` does not raise them. POM profiles, when present, stay unread and unactivated. Declared repositories stay unread and are not contacted. Packaging is not defaulted to `jar` when -omitted. Gradle variant attributes other than category and usage are not -decoded. +omitted. Modules, 4.1.0 `subprojects`, and the 4.1.0 `root` attribute are not +reported. Maven 4 can fill omitted parent coordinates from the POM at the +parent's `relativePath`, whatever the model version. In a 4.1.0 model POM, it +can also infer an omitted parent version and omitted dependency and managed +dependency group IDs and versions from other projects in the build. REA does +not apply that inference, and those omissions stay in `unresolved`. The result +for a 4.1.0 model POM states this limitation. For any POM with a parent, the +result states that the parent POM was not read. Gradle variant attributes other +than category and usage are not decoded. Complete coverage means the selected document was read. It does not establish a resolved dependency tree, a repository fetch, or that Maven or Gradle would diff --git a/src/java/MavenGradleMetadataParser.test.ts b/src/java/MavenGradleMetadataParser.test.ts index fcedab4f97..ec861bd40e 100644 --- a/src/java/MavenGradleMetadataParser.test.ts +++ b/src/java/MavenGradleMetadataParser.test.ts @@ -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(` + + ${model} + ga1 + xy2 + +`); + 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(` + ${modelVersion === null ? "" : `${modelVersion}`} + demo + + com.exampleplatform3bom + + + com.examplelib3bom + +`); + 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 = ` + 4.2.0 + demo +`; + 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 = ` + 4.2.0 + demo +`; + 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(` + 4.2.0 + demo + + com.exampleplatform3bom + +`); + 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( ` @@ -668,6 +775,17 @@ it("refuses a POM dependency with two version children", () => { ); }); +it("refuses a POM with two modelVersion children", () => { + expectFailure( + ` + 4.0.0 + 4.1.0 + demo +`, + "malformed", + ); +}); + it("refuses malformed Gradle JSON", () => { expectFailure("{", "malformed"); }); diff --git a/src/java/MavenGradleMetadataParser.ts b/src/java/MavenGradleMetadataParser.ts index 16395f0f2a..199510b5a9 100644 --- a/src/java/MavenGradleMetadataParser.ts +++ b/src/java/MavenGradleMetadataParser.ts @@ -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 => { 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.", diff --git a/tests/acceptance/mavenGradleMetadataCli.test.ts b/tests/acceptance/mavenGradleMetadataCli.test.ts index 6e1a2fdba1..3aceed06fd 100644 --- a/tests/acceptance/mavenGradleMetadataCli.test.ts +++ b/tests/acceptance/mavenGradleMetadataCli.test.ts @@ -565,6 +565,38 @@ cliTest( }, ); +cliTest("reads a Maven 4.1.0 model POM", async ({ cli }) => { + const root = await createTestTempDirectory("rea-maven-cli-model-41-"); + const path = join(root, "pom.xml"); + await writeFile( + path, + ` + + 4.1.0 + ga1 + xy2 + +`, + ); + const result = await cli.run({ + arguments: ["inspect-maven-gradle-metadata", path, "--json"], + }); + expect(result.exitCode, result.stdout + result.stderr).toBe(0); + const report = outputSchema.parse(result.json).normalized_result; + expect(report.format).toBe("maven-pom"); + expect(report.coordinates).toMatchObject({ + group_id: "g", + artifact_id: "a", + version: "1", + }); + expect(report.dependencies).toEqual([ + expect.objectContaining({ group_id: "x", artifact_id: "y", version: "2" }), + ]); + expect(report.limitations).toContain( + "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.", + ); +}); + cliTest( "fails closed on a file that is not Maven POM or Gradle module metadata", async ({ cli }) => {