дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
fix(maven): <os>-activated profiles show as active and can be disabled (#8180)
A profile Maven activates by `<activation><os>` showed unchecked in the Maven tool window and no deactivation was ever passed, so unticking it could not keep it out of a build (per-platform native classifiers ended up in the jar). The POM reader ignored `<os>`. It now parses `<name>`, `<family>` and `<arch>`, requires every named field to match this machine (families as Maven's OsUtils: mac/darwin, unix covering Linux and macOS, windows; amd64/x86_64 and aarch64/arm64 spellings; a leading `!` negates). A matching profile counts as active by default, so the tool window shows it implicitly enabled and unticking it sends `-P !id`; it also suppresses activeByDefault siblings as Maven does. Closes #8171 Co-authored-by: Jackzigen <[email protected]>
Цей коміт міститься в:
1 джерело
b8d0d12142
коміт
cd0fe6c8f5
1 file changed
+150
-16
@@ -316,6 +316,8 @@ struct RawProfile {
|
||||
active_by_default: bool,
|
||||
/// `<activation><jdk>` (the common `1.8`, `21` and `[17,)` forms).
|
||||
activation_jdk: Option<String>,
|
||||
/// `<activation><os>` (`<name>`, `<family>`, `<arch>`; Maven matches all present fields).
|
||||
activation_os: Option<RawOsActivation>,
|
||||
modules: Vec<String>,
|
||||
properties: Vec<(String, String)>,
|
||||
deps: Vec<RawDep>,
|
||||
@@ -324,6 +326,55 @@ struct RawProfile {
|
||||
managed_plugins: Vec<RawPlugin>,
|
||||
}
|
||||
|
||||
/// `<activation><os>`: the fields the POM names, all of which Maven requires to match.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
struct RawOsActivation {
|
||||
name: Option<String>,
|
||||
family: Option<String>,
|
||||
arch: Option<String>,
|
||||
}
|
||||
|
||||
impl RawOsActivation {
|
||||
/// Whether this machine satisfies the activation: every named field matches, as Maven's
|
||||
/// `OsActivation` does. Families map as Maven's `OsUtils` (`windows`, `unix`/`linux`,
|
||||
/// `mac`/`darwin`); a name matches the running OS name or its family alias.
|
||||
fn matches(&self) -> bool {
|
||||
let os = std::env::consts::OS; // "macos" | "windows" | "linux"
|
||||
let arch = std::env::consts::ARCH; // "x86_64" | "aarch64"
|
||||
fn family_of(name: &str) -> &str {
|
||||
match name {
|
||||
"macos" | "darwin" => "mac",
|
||||
"windows" => "windows",
|
||||
"linux" => "unix",
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
let running_family = family_of(os);
|
||||
// Absent fields impose no constraint (Maven matches only what the POM names); a leading
|
||||
// `!` negates the field, as in Maven's `<family>!windows</family>`.
|
||||
let ok = |value: Option<&String>, f: &dyn Fn(&str) -> bool| {
|
||||
value.is_none_or(|v| match v.trim().strip_prefix('!') {
|
||||
Some(negated) => !f(negated.trim()),
|
||||
None => f(v.trim()),
|
||||
})
|
||||
};
|
||||
let name_ok = ok(self.name.as_ref(), &|v| {
|
||||
v.eq_ignore_ascii_case(os) || v.eq_ignore_ascii_case(running_family) || family_of(v) == running_family
|
||||
});
|
||||
let family_ok = ok(self.family.as_ref(), &|v| {
|
||||
v.eq_ignore_ascii_case(running_family)
|
||||
|| (v.eq_ignore_ascii_case("unix") && matches!(running_family, "mac" | "unix"))
|
||||
});
|
||||
let arch_ok = ok(self.arch.as_ref(), &|v| {
|
||||
v.eq_ignore_ascii_case(arch)
|
||||
|| (v.eq_ignore_ascii_case("amd64") && arch == "x86_64")
|
||||
|| (v.eq_ignore_ascii_case("aarch64") && arch == "aarch64")
|
||||
|| (v.eq_ignore_ascii_case("arm64") && arch == "aarch64")
|
||||
});
|
||||
name_ok && family_ok && arch_ok
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct RawPom {
|
||||
group_id: Option<String>,
|
||||
@@ -443,6 +494,14 @@ fn read_raw(project: &Element) -> RawPom {
|
||||
.at("activation/activeByDefault")
|
||||
.is_some_and(|a| a.text.trim().eq_ignore_ascii_case("true")),
|
||||
activation_jdk: p.at("activation/jdk").map(|jdk| jdk.text.trim().to_owned()).filter(|jdk| !jdk.is_empty()),
|
||||
activation_os: p
|
||||
.at("activation/os")
|
||||
.map(|os| RawOsActivation {
|
||||
name: os.text_of("name").map(|n| n.trim().to_owned()).filter(|n| !n.is_empty()),
|
||||
family: os.text_of("family").map(|f| f.trim().to_owned()).filter(|f| !f.is_empty()),
|
||||
arch: os.text_of("arch").map(|a| a.trim().to_owned()).filter(|a| !a.is_empty()),
|
||||
})
|
||||
.filter(|os| os.name.is_some() || os.family.is_some() || os.arch.is_some()),
|
||||
modules: module_names(p),
|
||||
properties: read_properties(p.child("properties")),
|
||||
deps: p.list("dependencies", "dependency").into_iter().filter_map(read_dep).collect(),
|
||||
@@ -522,9 +581,9 @@ struct Inherited {
|
||||
/// The nearest `maven-compiler-plugin` `<release>` / `<source>`: a module usually
|
||||
/// inherits its compiler level from the parent's (managed) plugin configuration.
|
||||
compiler_release: Option<String>,
|
||||
/// `<id>`, `activeByDefault`: profiles of the POM and its ancestors —
|
||||
/// Maven lets a child enable a parent's profiles with `-P`.
|
||||
profiles: Vec<(String, bool)>,
|
||||
/// `<id>`, `activeByDefault`, OS-activation matched on this machine: profiles of the POM and
|
||||
/// its ancestors — Maven lets a child enable a parent's profiles with `-P`.
|
||||
profiles: Vec<(String, bool, bool)>,
|
||||
}
|
||||
|
||||
fn merge_by<T: Clone>(base: &mut Vec<T>, own: &[T], key: impl Fn(&T) -> String) {
|
||||
@@ -778,22 +837,22 @@ impl Resolver {
|
||||
Self { local_repo, profiles: profiles.to_vec(), jdk_version, poms: HashMap::new(), contexts: HashMap::new() }
|
||||
}
|
||||
|
||||
/// The active profiles of a POM: chosen ones, JDK-activated ones, and the
|
||||
/// The active profiles of a POM: chosen ones, JDK- and OS-activated ones, and the
|
||||
/// `activeByDefault` ones not turned off while another profile is active.
|
||||
fn active<'a>(&self, profiles: &'a [RawProfile]) -> impl Iterator<Item = &'a RawProfile> {
|
||||
let chosen = |id: &str| profile_choice(&self.profiles, id);
|
||||
let jdk_version = self.jdk_version;
|
||||
let auto_active = move |p: &RawProfile| {
|
||||
p.activation_jdk.as_deref().is_some_and(|spec| jdk_matches(spec, jdk_version))
|
||||
|| p.activation_os.as_ref().is_some_and(RawOsActivation::matches)
|
||||
};
|
||||
let any_active = profiles.iter().any(|p| {
|
||||
let choice = chosen(&p.id);
|
||||
choice == Some(true)
|
||||
|| (choice.is_none() && p.activation_jdk.as_deref().is_some_and(|spec| jdk_matches(spec, jdk_version)))
|
||||
choice == Some(true) || (choice.is_none() && auto_active(p))
|
||||
});
|
||||
profiles.iter().filter(move |p| match chosen(&p.id) {
|
||||
Some(value) => value,
|
||||
None => {
|
||||
p.activation_jdk.as_deref().is_some_and(|spec| jdk_matches(spec, jdk_version))
|
||||
|| (p.active_by_default && !any_active)
|
||||
}
|
||||
None => auto_active(p) || (p.active_by_default && !any_active),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -868,8 +927,13 @@ impl Resolver {
|
||||
}
|
||||
merge_by(
|
||||
&mut ctx.profiles,
|
||||
&pom.profiles.iter().map(|p| (p.id.clone(), p.active_by_default)).collect::<Vec<_>>(),
|
||||
|(id, _)| id.clone(),
|
||||
&pom.profiles
|
||||
.iter()
|
||||
.map(|p| {
|
||||
(p.id.clone(), p.active_by_default, p.activation_os.as_ref().is_some_and(RawOsActivation::matches))
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
|(id, ..)| id.clone(),
|
||||
);
|
||||
// Managed dependencies: own entries win; imported BOMs fill the gaps.
|
||||
let props = self.properties_for(&pom, &ctx, &dir);
|
||||
@@ -1807,11 +1871,15 @@ pub fn load_with_linked_jdk(
|
||||
.filter(|v| !v.is_empty() && !v.contains("${"));
|
||||
|
||||
// The project can activate the profiles of its own POM and its ancestors'.
|
||||
for (id, active) in &ctx.profiles {
|
||||
// OS-activated ones count as active by default for the tool window: a click
|
||||
// then disables them explicitly (-P !id), which is the only way to keep
|
||||
// Maven's own <os> activation from putting them in the build (#8171).
|
||||
for (id, by_default, os_active) in &ctx.profiles {
|
||||
let auto = *by_default || *os_active;
|
||||
if let Some(existing) = model.profiles.iter_mut().find(|x| x.id == *id) {
|
||||
existing.active_by_default |= *active;
|
||||
existing.active_by_default |= auto;
|
||||
} else {
|
||||
model.profiles.push(MavenProfile { id: id.clone(), active_by_default: *active, ..Default::default() });
|
||||
model.profiles.push(MavenProfile { id: id.clone(), active_by_default: auto, ..Default::default() });
|
||||
}
|
||||
}
|
||||
let artifact_id = i(&pom.artifact_id);
|
||||
@@ -1837,7 +1905,7 @@ pub fn load_with_linked_jdk(
|
||||
plugins,
|
||||
dependencies,
|
||||
system_paths,
|
||||
profiles: ctx.profiles.iter().map(|(id, _)| id.clone()).collect(),
|
||||
profiles: ctx.profiles.iter().map(|(id, ..)| id.clone()).collect(),
|
||||
java_version: jv,
|
||||
source_encoding,
|
||||
command: wrapper(&canonical_root, &dir_canon),
|
||||
@@ -3575,6 +3643,72 @@ mod tests {
|
||||
assert_eq!(profiles, [("jdk8", false), ("jdk21", true), ("own", false)]);
|
||||
}
|
||||
|
||||
/// A profile `<os>`-activated on this machine shows as active by default in the tool
|
||||
/// window, so unchecking it sends an explicit `-P !id` — the only way to keep Maven's
|
||||
/// own `<os>` activation from putting the profile in the build (#8171). An `<os>`
|
||||
/// block naming another platform stays unchecked.
|
||||
#[test]
|
||||
fn os_activated_profiles_count_as_active_by_default_here() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path();
|
||||
let (windows, this_machine) = match std::env::consts::OS {
|
||||
"macos" => ("<family>windows</family>", "<family>mac</family>"),
|
||||
"linux" => ("<family>windows</family>", "<family>unix</family>"),
|
||||
_ => ("<name>linux</name>", "<family>windows</family>"),
|
||||
};
|
||||
write(
|
||||
root,
|
||||
"pom.xml",
|
||||
format!(
|
||||
r#"<project><groupId>demo</groupId><artifactId>demo</artifactId><version>1.0</version>
|
||||
<profiles>
|
||||
<profile><id>other-platform</id><activation><os>{}</os></activation></profile>
|
||||
<profile><id>this-platform</id><activation><os>{}</os></activation></profile>
|
||||
</profiles>
|
||||
</project>"#,
|
||||
windows, this_machine
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
let model = load_with_repo(root, None);
|
||||
let profile = |id: &str| model.profiles.iter().find(|p| p.id == id).unwrap_or_else(|| panic!("missing {id}"));
|
||||
assert!(
|
||||
profile("this-platform").active_by_default,
|
||||
"<os> activation on this machine counts as active by default"
|
||||
);
|
||||
assert!(!profile("other-platform").active_by_default, "another platform's <os> activation stays off");
|
||||
|
||||
// A name without a family works too, and an empty <os> block (no fields) never matches.
|
||||
write(
|
||||
root,
|
||||
"pom.xml",
|
||||
r#"<project><groupId>demo</groupId><artifactId>demo</artifactId><version>1.0</version>
|
||||
<profiles>
|
||||
<profile><id>by-name</id><activation><os><name>not-an-os</name></os></activation></profile>
|
||||
</profiles>
|
||||
</project>"#,
|
||||
);
|
||||
let model = load_with_repo(root, None);
|
||||
assert!(!model.profiles.iter().any(|p| p.id == "by-name" && p.active_by_default));
|
||||
|
||||
// A negated family matches every other platform, as in Maven.
|
||||
let other = if std::env::consts::OS == "windows" { "mac" } else { "windows" };
|
||||
write(
|
||||
root,
|
||||
"pom.xml",
|
||||
format!(
|
||||
r#"<project><groupId>demo</groupId><artifactId>demo</artifactId><version>1.0</version>
|
||||
<profiles>
|
||||
<profile><id>not-other</id><activation><os><family>!{other}</family></os></activation></profile>
|
||||
</profiles>
|
||||
</project>"#
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
let model = load_with_repo(root, None);
|
||||
assert!(model.profiles.iter().any(|p| p.id == "not-other" && p.active_by_default));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parent_and_bom_from_the_local_repository_and_wrapper() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Заблокувати користувача