fix(maven): run plugin prefix goals under the plugin's coordinates (#6807)

A goal typed or picked as prefix:goal (spring-javaformat:apply) reached mvn verbatim, and Maven only resolves prefixes of its default plugin groups when the plugin metadata is missing from the repositories, so plugins such as io.spring.javaformat failed with No plugin found for prefix. Goals whose prefix matches a plugin of the project's effective model (inherited plugins included) now run as groupId:artifactId[:version]:goal, keeping @execution; flags and their values, phases, full coordinates and unknown prefixes stay as typed. The configuration name keeps the prefix form.

Fixes #5570

Co-authored-by: arookieofc <[email protected]>
Цей коміт міститься в:
arookieofcтаarookieofc authored and GitHub committed 2026-11-29 22:52:46 +08:00
1 джерело 1ec634619c
коміт 73ee2117e9
3 files changed
+196 -2

No files matched your search

+54
Переглянути файл
@@ -4461,4 +4461,58 @@ mod linkage_tests {
assert_eq!(poms, expected.map(|e| e.iter().map(|s| s.to_string()).collect::<Vec<_>>()), "{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#"<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules><module>child</module></modules>
<build>
<plugins>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.38</version>
</plugin>
</plugins>
</build>
</project>"#,
)
.unwrap();
std::fs::write(
root.join("child/pom.xml"),
r#"<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>child</artifactId>
</project>"#,
)
.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"));
}
}
}
+83 -1
Переглянути файл
@@ -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"]);
+59 -1
Переглянути файл
@@ -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 `<project> [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,
);