Skip to content

Commit d1671ad

Browse files
committed
Use effective-pom for Maven metadata
1 parent e79cdd9 commit d1671ad

1 file changed

Lines changed: 77 additions & 5 deletions

File tree

src/util/mavenUtil.ts

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { LIBERTY_MAVEN_PLUGIN_CONTAINER_VERSION, LIBERTY_MAVEN_PROJECT_CONTAINER
66
import { BuildFileImpl } from "./buildFile";
77
import { localize } from "../util/i18nUtil";
88
import * as semver from "semver";
9+
import * as vscode from "vscode";
10+
import * as Path from "path";
11+
import { pathExists } from "fs-extra";
12+
import { exec } from "child_process";
13+
import { promisify } from "util";
914

1015
/**
1116
* Look for a valid parent pom.xml
@@ -228,6 +233,62 @@ function containerVersion(plugin: any): boolean {
228233
return false;
229234
}
230235

236+
/**
237+
* Resolve effective POM using Maven help:effective-pom goal
238+
* @param pomPath Path to pom.xml file
239+
* @returns Effective POM XML string
240+
* @throws Error if Maven command fails
241+
*/
242+
export async function resolveEffectivePom(pomPath: string): Promise<string> {
243+
const execAsync = promisify(exec);
244+
245+
// Determine Maven executable
246+
const mavenConfig = vscode.workspace.getConfiguration("maven");
247+
const mavenExecutablePath = mavenConfig.get("executable.path") as string | undefined;
248+
let mvnCmd = "mvn";
249+
250+
if (mavenExecutablePath) {
251+
mvnCmd = mavenExecutablePath;
252+
} else {
253+
// Check for Maven wrapper
254+
const preferMavenWrapper = mavenConfig.get("executable.preferMavenWrapper") as boolean | undefined;
255+
if (preferMavenWrapper) {
256+
const pomDir = Path.dirname(pomPath);
257+
const mvnw = process.platform.startsWith("win") ? "mvnw.cmd" : "mvnw";
258+
let currentDir = pomDir;
259+
260+
// Walk up parent folders to find mvnw
261+
while (Path.basename(currentDir)) {
262+
const potentialMvnwPath = Path.join(currentDir, mvnw);
263+
if (await pathExists(potentialMvnwPath)) {
264+
mvnCmd = potentialMvnwPath;
265+
break;
266+
}
267+
currentDir = Path.dirname(currentDir);
268+
}
269+
}
270+
}
271+
272+
// Build the command
273+
const command = `"${mvnCmd}" help:effective-pom -f "${pomPath}" -q`;
274+
275+
try {
276+
// Execute the Maven command and capture stdout
277+
const { stdout, stderr } = await execAsync(command, {
278+
maxBuffer: 10 * 1024 * 1024, // 10MB buffer for large POMs
279+
cwd: Path.dirname(pomPath)
280+
});
281+
282+
if (stderr && stderr.trim().length > 0) {
283+
console.warn(`Maven effective-pom stderr: ${stderr}`);
284+
}
285+
286+
return stdout;
287+
} catch (error: any) {
288+
throw new Error(`Failed to resolve effective POM for ${pomPath}: ${error.message}`);
289+
}
290+
}
291+
231292
/**
232293
* Interface for Maven project metadata used in multi-module hierarchy
233294
*/
@@ -245,6 +306,7 @@ export interface MavenProjectMetadata {
245306

246307
/**
247308
* Extract metadata from a Maven POM file for multi-module support
309+
* Uses effective-pom for accurate metadata extraction with fallback to direct XML
248310
* @param pomPath Path to the pom.xml file
249311
* @param xmlString Optional XML string content (if already read)
250312
* @returns MavenProjectMetadata object
@@ -253,11 +315,21 @@ export async function extractMavenMetadata(pomPath: string, xmlString?: string):
253315
const fse = require("fs-extra");
254316
const xml = xmlString || await fse.readFile(pomPath, "utf8");
255317

256-
const metadata = parsePomXml(xml);
257-
metadata.buildFilePath = pomPath;
258-
metadata.xmlString = xml;
259-
260-
return metadata;
318+
// Try to use effective-pom for accurate metadata extraction
319+
try {
320+
const effectivePomXml = await resolveEffectivePom(pomPath);
321+
const metadata = parsePomXml(effectivePomXml);
322+
metadata.buildFilePath = pomPath;
323+
metadata.xmlString = xml; // Keep original XML for reference
324+
return metadata;
325+
} catch (error: any) {
326+
// Fallback: Use direct XML parsing if effective-pom fails
327+
console.warn(`Could not resolve effective POM for ${pomPath}, using direct XML parsing: ${error.message}`);
328+
const metadata = parsePomXml(xml);
329+
metadata.buildFilePath = pomPath;
330+
metadata.xmlString = xml;
331+
return metadata;
332+
}
261333
}
262334

263335
/**

0 commit comments

Comments
 (0)