diff --git a/crates/ide-build/src/maven.rs b/crates/ide-build/src/maven.rs index 242007673..336f8c3a7 100644 --- a/crates/ide-build/src/maven.rs +++ b/crates/ide-build/src/maven.rs @@ -4461,4 +4461,58 @@ mod linkage_tests { assert_eq!(poms, expected.map(|e| e.iter().map(|s| s.to_string()).collect::>()), "{unlinked:?}"); } } + + #[test] + fn child_module_inherits_parent_declared_plugin_prefix_and_coordinates() { + let tmp = tempfile::tempdir().unwrap(); + let root = tmp.path(); + std::fs::create_dir_all(root.join("child")).unwrap(); + std::fs::write( + root.join("pom.xml"), + r#" + 4.0.0 + com.example + parent + 1.0.0-SNAPSHOT + pom + child + + + + io.spring.javaformat + spring-javaformat-maven-plugin + 0.0.38 + + + +"#, + ) + .unwrap(); + std::fs::write( + root.join("child/pom.xml"), + r#" + 4.0.0 + + com.example + parent + 1.0.0-SNAPSHOT + + child +"#, + ) + .unwrap(); + + let model = load_with_repo(root, None); + for pom in ["pom.xml", "child/pom.xml"] { + let proj = model.projects.iter().find(|p| p.pom == pom).expect(pom); + let plugin = proj + .plugins + .iter() + .find(|p| p.prefix == "spring-javaformat") + .unwrap_or_else(|| panic!("spring-javaformat plugin missing on {pom}: {:?}", proj.plugins)); + assert_eq!(plugin.group_id, "io.spring.javaformat"); + assert_eq!(plugin.artifact_id, "spring-javaformat-maven-plugin"); + assert_eq!(plugin.version.as_deref(), Some("0.0.38")); + } + } } diff --git a/src/features/build-tools/model.test.ts b/src/features/build-tools/model.test.ts index 38e485485..3e36f5cfc 100644 --- a/src/features/build-tools/model.test.ts +++ b/src/features/build-tools/model.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import type { GradleModel, MavenModel, MavenProject, RunConfig } from "@/platform/ipc"; +import type { GradleModel, MavenModel, MavenPlugin, MavenProject, RunConfig } from "@/platform/ipc"; import { expandableKeys, goalCompletions, @@ -287,6 +287,88 @@ describe("Maven runs", () => { expect(goal.env).toEqual({}); }); + // #5570: prefix goals of the project's effective plugins (inherited ones included) run under + // the plugin's coordinates, so plugins outside Maven's default groups resolve. + it("resolves plugin prefix goals from the project's effective plugins", () => { + const javaformat: MavenPlugin = { + groupId: "io.spring.javaformat", + artifactId: "spring-javaformat-maven-plugin", + version: "0.0.38", + prefix: "spring-javaformat", + goals: ["apply", "validate"], + }; + const versionless: MavenPlugin = { + groupId: "com.acme", + artifactId: "demo-maven-plugin", + version: null, + prefix: "demo", + goals: ["run"], + }; + const parentProj = project({ + pom: "pom.xml", + dir: "", + groupId: "com.example", + artifactId: "parent", + version: "1.0.0-SNAPSHOT", + packaging: "pom", + displayName: "parent", + modules: ["child/pom.xml"], + plugins: [javaformat, versionless], + }); + const childProj = project({ + pom: "child/pom.xml", + dir: "child", + groupId: "com.example", + artifactId: "child", + version: "1.0.0-SNAPSHOT", + displayName: "child", + parent: "com.example:parent:1.0.0-SNAPSHOT", + aggregated: true, + // The effective model lists inherited plugins on the module too. + plugins: [javaformat, versionless], + }); + const multi: MavenModel = { ...maven, projects: [parentProj, childProj] }; + const opts = { offline: false, skipTests: false, profiles: {} }; + + const fromParent = mavenGoalConfig(parentProj, ["spring-javaformat:apply"], multi, opts); + expect(fromParent.name).toBe("parent [spring-javaformat:apply]"); + expect(fromParent.args).toEqual(["io.spring.javaformat:spring-javaformat-maven-plugin:0.0.38:apply"]); + + const fromChild = mavenGoalConfig( + childProj, + [ + "-s", + "E:\\apache-maven-3.8.1\\conf\\settings.xml", + "spring-javaformat:apply", + "demo:run", + "spring-javaformat:validate@fmt", + ], + multi, + opts, + ); + expect(fromChild.args).toEqual([ + "-s", + "E:\\apache-maven-3.8.1\\conf\\settings.xml", + "io.spring.javaformat:spring-javaformat-maven-plugin:0.0.38:apply", + "com.acme:demo-maven-plugin:run", + "io.spring.javaformat:spring-javaformat-maven-plugin:0.0.38:validate@fmt", + ]); + + // Unknown prefixes, full coordinates, lifecycle phases, and -D properties stay verbatim. + const unchanged = mavenGoalConfig( + childProj, + ["clean", "-Dfoo=bar:baz", "unknown:goal", "io.spring.javaformat:spring-javaformat-maven-plugin:0.0.38:validate"], + multi, + opts, + ); + expect(unchanged.args).toEqual([ + "clean", + "-Dfoo=bar:baz", + "unknown:goal", + "io.spring.javaformat:spring-javaformat-maven-plugin:0.0.38:validate", + ]); + }); + it("parses typed command lines", () => { expect(parseGoalLine("mvn clean install -Dmsg='a b'")).toEqual(["clean", "install", "-Dmsg=a b"]); expect(parseGoalLine("./mvnw -q verify")).toEqual(["-q", "verify"]); diff --git a/src/features/build-tools/model.ts b/src/features/build-tools/model.ts index 2affd51db..41236c89c 100644 --- a/src/features/build-tools/model.ts +++ b/src/features/build-tools/model.ts @@ -484,6 +484,64 @@ export function mavenArgs( return args; } +/** Maven CLI flags whose value is the next argument (never rewritten as a plugin goal). */ +const MAVEN_FLAGS_WITH_VALUE = new Set([ + "-f", + "--file", + "-s", + "--settings", + "-gs", + "--global-settings", + "-P", + "--activate-profiles", + "-pl", + "--projects", + "-rf", + "--resume-from", + "-l", + "--log-file", + "-T", + "--threads", + "-t", + "--toolchains", + "-gt", + "--global-toolchains", + "-b", + "--builder", + "-emp", + "--encrypt-master-password", + "-ep", + "--encrypt-password", +]); + +/** + * Rewrites `prefix:goal` (and `prefix:goal@execution`) to the plugin's coordinates + * (`groupId:artifactId[:version]:goal`) when the project's effective model (inherited + * plugins included) has a plugin with that prefix: Maven itself only finds prefixes of + * its default plugin groups when the plugin's metadata isn't in the repositories. + */ +export function resolveMavenGoals(goals: readonly string[], project: MavenProject): string[] { + let skipNext = false; + return goals.map((token) => { + if (skipNext) { + skipNext = false; + return token; + } + if (MAVEN_FLAGS_WITH_VALUE.has(token)) { + skipNext = true; + return token; + } + if (token.startsWith("-") || token.includes("/") || token.includes("\\")) return token; + const colon = token.indexOf(":"); + if (colon <= 0 || colon !== token.lastIndexOf(":")) return token; + const prefix = token.slice(0, colon); + const goal = token.slice(colon + 1); + if (!goal) return token; + const plugin = project.plugins.find((p) => p.prefix === prefix && p.groupId && p.artifactId); + return plugin ? `${pluginCoordinates(plugin)}:${goal}` : token; + }); +} + /** IDEA names Maven configurations ` [goal1,goal2]`. */ export function mavenConfigName(project: MavenProject, goals: readonly string[]): string { return `${project.displayName} [${goals.filter((g) => !g.startsWith("-")).join(",")}]`; @@ -496,7 +554,7 @@ export function mavenGoalConfig( options: MavenRunOptions, ): RunConfig { const args = mavenArgs( - goals, + resolveMavenGoals(goals, project), { profiles: model.profiles.filter((p) => p.projectRoot === project.projectRoot) }, options, );