From 4069fc1d0cc732f92baf72d78425b06cc34a2fe3 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 13 Nov 2026 12:59:49 +0000 Subject: [PATCH] fix: read the Java version from maven-compiler-plugin configuration parsePom only read java.version and maven.compiler.* properties, so a POM that set the language level on maven-compiler-plugin reported no Java runtime. When those properties are absent, read the plugin's release, then source, then target, from build.plugins and build.pluginManagement, and resolve ${property} references. Signed-off-by: Cursor Agent Co-authored-by: ABIZ - Agentic Business Record Standard --- src/core-open/scanners/java-scanner.ts | 65 ++++++ test/maven-compiler-java-version.test.ts | 242 +++++++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 test/maven-compiler-java-version.test.ts diff --git a/src/core-open/scanners/java-scanner.ts b/src/core-open/scanners/java-scanner.ts index 3c3442d..b5b2609 100644 --- a/src/core-open/scanners/java-scanner.ts +++ b/src/core-open/scanners/java-scanner.ts @@ -227,6 +227,14 @@ function parsePom(xml: string, filePath: string): PomData { parentVersion: parent?.version, }); + // A `` version wins. When none of those keys is set, read + // maven-compiler-plugin `` from `build.plugins` and + // `build.pluginManagement`. `` is checked in both places before + // ``, then ``. One plugin is an object; several are an array. + if (!javaVersion) { + javaVersion = javaVersionFromCompilerPlugin(project.build, properties); + } + // Extract dependencies. Top-level blocks are stored first. The scan // keeps the first versioned coordinate, so a later profile entry does not // replace a top-level row. @@ -287,6 +295,63 @@ function parsePom(xml: string, filePath: string): PomData { }; } +const COMPILER_PLUGIN_ARTIFACT = 'maven-compiler-plugin'; +const COMPILER_VERSION_KEYS = ['release', 'source', 'target'] as const; + +/** + * Java version from `maven-compiler-plugin` when `` did not set one. + * Configurations are collected from `build.plugins`, then `build.pluginManagement`. + * `` is read across those configurations before ``, then + * ``, so a release in either place wins over source. The value is + * interpolated, and a result that still contains `${` is skipped. + */ +function javaVersionFromCompilerPlugin( + build: unknown, + properties: Record, +): string | undefined { + const configs = compilerPluginConfigurations(build); + for (const key of COMPILER_VERSION_KEYS) { + for (const config of configs) { + const raw = config[key]; + if (typeof raw !== 'string' && typeof raw !== 'number') continue; + const resolved = resolveProperty(String(raw), properties).trim(); + if (!resolved || resolved.includes('${')) continue; + return resolved; + } + } + return undefined; +} + +function compilerPluginConfigurations(build: unknown): Record[] { + if (!build || typeof build !== 'object') return []; + const record = build as Record; + const pluginNodes = [ + childPlugin(record.plugins), + childPlugin(nestedPlugins(record.pluginManagement)), + ]; + const configs: Record[] = []; + for (const plugins of pluginNodes) { + for (const plugin of xmlList>(plugins)) { + if (!plugin || typeof plugin !== 'object') continue; + if (String(plugin.artifactId ?? '') !== COMPILER_PLUGIN_ARTIFACT) continue; + const configuration = plugin.configuration; + if (!configuration || typeof configuration !== 'object') continue; + configs.push(configuration as Record); + } + } + return configs; +} + +function nestedPlugins(pluginManagement: unknown): unknown { + if (!pluginManagement || typeof pluginManagement !== 'object') return undefined; + return (pluginManagement as Record).plugins; +} + +function childPlugin(plugins: unknown): Record | Record[] | undefined { + if (!plugins || typeof plugins !== 'object') return undefined; + return (plugins as { plugin?: Record | Record[] }).plugin; +} + /** One XML child is an object; several children are an array. */ function xmlList(value: T | T[] | undefined | null): T[] { if (value == null) return []; diff --git a/test/maven-compiler-java-version.test.ts b/test/maven-compiler-java-version.test.ts new file mode 100644 index 0000000..2c989af --- /dev/null +++ b/test/maven-compiler-java-version.test.ts @@ -0,0 +1,242 @@ +/** + * The Java runtime comes from `` first. When those keys are absent, + * `maven-compiler-plugin` `` supplies ``, then + * ``, then ``, under `build.plugins` or `build.pluginManagement`. + * A single plugin is an object; several plugins are an array. `${...}` in the + * plugin value is resolved against POM properties. + */ +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { afterEach, describe, expect, it } from 'vitest'; +import { MavenCache } from '../src/core-open/scanners/maven-cache.js'; +import { scanJavaProjects } from '../src/core-open/scanners/java-scanner.js'; +import { Semaphore } from '../src/core-open/utils/semaphore.js'; + +const roots: string[] = []; + +afterEach(() => { + for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true }); +}); + +function pom(body: string): string { + return ` + + 4.0.0 + com.example + compiler-app + 1.0.0 +${body} + +`; +} + +async function runtimeOf(body: string): Promise<{ runtime?: string; targetFramework?: string }> { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'vg-maven-compiler-')); + roots.push(root); + fs.writeFileSync(path.join(root, 'pom.xml'), pom(body)); + const projects = await scanJavaProjects(root, new MavenCache(new Semaphore(1), undefined, true)); + expect(projects).toHaveLength(1); + return { runtime: projects[0]?.runtime, targetFramework: projects[0]?.targetFramework }; +} + +describe('Java version from maven-compiler-plugin', () => { + it('reads from a single plugin under build.plugins', async () => { + const facts = await runtimeOf(` + + + maven-compiler-plugin + + 17 + 11 + + + + `); + expect(facts).toEqual({ runtime: 'Java 17', targetFramework: 'Java 17' }); + }); + + it('reads from a plugin array when is absent', async () => { + const facts = await runtimeOf(` + + + maven-surefire-plugin + + 99 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + 8 + + + + `); + expect(facts).toEqual({ runtime: 'Java 11', targetFramework: 'Java 11' }); + }); + + it('reads when and are absent', async () => { + const facts = await runtimeOf(` + + + maven-compiler-plugin + + 8 + + + + `); + expect(facts).toEqual({ runtime: 'Java 8', targetFramework: 'Java 8' }); + }); + + it('reads from a single plugin under pluginManagement', async () => { + const facts = await runtimeOf(` + + + + maven-compiler-plugin + + 17 + + + + + `); + expect(facts).toEqual({ runtime: 'Java 17', targetFramework: 'Java 17' }); + }); + + it('reads from a pluginManagement plugin array', async () => { + const facts = await runtimeOf(` + + + + maven-surefire-plugin + + + maven-compiler-plugin + + 21 + + + + + `); + expect(facts).toEqual({ runtime: 'Java 21', targetFramework: 'Java 21' }); + }); + + it('uses a pluginManagement ahead of on the plugin', async () => { + const facts = await runtimeOf(` + + + maven-compiler-plugin + + 11 + + + + + + + maven-compiler-plugin + + 17 + + + + + `); + expect(facts).toEqual({ runtime: 'Java 17', targetFramework: 'Java 17' }); + }); + + it('prefers on build.plugins over pluginManagement', async () => { + const facts = await runtimeOf(` + + + maven-compiler-plugin + + 11 + + + + + + + maven-compiler-plugin + + 17 + + + + + `); + expect(facts).toEqual({ runtime: 'Java 11', targetFramework: 'Java 11' }); + }); + + it('keeps maven.compiler.release ahead of plugin configuration', async () => { + const facts = await runtimeOf(` + 17 + + + + + maven-compiler-plugin + + 11 + + + + `); + expect(facts).toEqual({ runtime: 'Java 17', targetFramework: 'Java 17' }); + }); + + it('resolves a property reference in plugin configuration', async () => { + const facts = await runtimeOf(` + 21 + + + + + maven-compiler-plugin + + \${jdk.version} + + + + `); + expect(facts).toEqual({ runtime: 'Java 21', targetFramework: 'Java 21' }); + }); + + it('skips an unresolved property and uses the next configuration key', async () => { + const facts = await runtimeOf(` + + + maven-compiler-plugin + + \${missing.version} + 11 + + + + `); + expect(facts).toEqual({ runtime: 'Java 11', targetFramework: 'Java 11' }); + }); + + it('reports no Java runtime when the only plugin value stays unresolved', async () => { + const facts = await runtimeOf(` + + + + maven-compiler-plugin + + \${missing.version} + + + + + `); + expect(facts).toEqual({ runtime: undefined, targetFramework: undefined }); + }); +});