diff --git a/crates/ide-build/src/maven.rs b/crates/ide-build/src/maven.rs index 404646103c..6090340667 100644 --- a/crates/ide-build/src/maven.rs +++ b/crates/ide-build/src/maven.rs @@ -327,6 +327,8 @@ struct RawProfile { managed: Vec, plugins: Vec, managed_plugins: Vec, + compiler: CompilerLevel, + managed_compiler: CompilerLevel, } #[derive(Clone, Debug, Default)] @@ -344,9 +346,49 @@ struct RawPom { plugins: Vec, managed_plugins: Vec, profiles: Vec, - /// `maven-compiler-plugin` `` / `` of ``, else of - /// ``. - compiler_release: Option, + /// Of ``. + compiler: CompilerLevel, + /// Of ``. + managed_compiler: CompilerLevel, +} + +/// The Java level of a `maven-compiler-plugin` ``. +#[derive(Clone, Debug, Default)] +struct CompilerLevel { + release: Option, + source: Option, + target: Option, +} + +impl CompilerLevel { + /// The configuration of the compiler plugin among the plugins at `path` of `e`. + fn read(e: &Element, path: &str) -> Self { + let config = e + .list(path, "plugin") + .into_iter() + .find(|p| p.text_of("artifactId").as_deref() == Some("maven-compiler-plugin")) + .and_then(|p| p.child("configuration")); + let value = |name: &str| config.and_then(|c| c.text_of(name)); + Self { release: value("release"), source: value("source"), target: value("target") } + } + + /// Maven merges a plugin's configuration element by element: what `own` sets wins. + fn merge(&mut self, own: &Self) { + for (value, own) in + [(&mut self.release, &own.release), (&mut self.source, &own.source), (&mut self.target, &own.target)] + { + if own.is_some() { + value.clone_from(own); + } + } + } + + /// The configuration the plugin runs with: its own, then what `` adds. + fn with_managed(&self, managed: &Self) -> Self { + let mut config = managed.clone(); + config.merge(self); + config + } } fn read_dep(e: &Element) -> Option { @@ -401,16 +443,6 @@ fn read_build(e: &Element, plugins: &mut Vec, managed: &mut Vec` / `` among the plugins at `path`. -fn compiler_level(project: &Element, path: &str) -> Option { - project - .list(path, "plugin") - .into_iter() - .find(|p| p.text_of("artifactId").as_deref() == Some("maven-compiler-plugin")) - .and_then(|p| p.child("configuration")) - .and_then(|c| c.text_of("release").or_else(|| c.text_of("source"))) -} - fn read_raw(project: &Element) -> RawPom { let parent = project.child("parent").and_then(|p| { Some(ParentRef { @@ -439,8 +471,8 @@ fn read_raw(project: &Element) -> RawPom { ..RawPom::default() }; read_build(project, &mut pom.plugins, &mut pom.managed_plugins); - pom.compiler_release = - compiler_level(project, "build/plugins").or_else(|| compiler_level(project, "build/pluginManagement/plugins")); + pom.compiler = CompilerLevel::read(project, "build/plugins"); + pom.managed_compiler = CompilerLevel::read(project, "build/pluginManagement/plugins"); for p in project.list("profiles", "profile") { let mut profile = RawProfile { id: p.text_of("id").unwrap_or_else(|| "default".into()), @@ -453,6 +485,8 @@ fn read_raw(project: &Element) -> RawPom { .into_iter() .filter_map(read_dep) .collect(), + compiler: CompilerLevel::read(p, "build/plugins"), + managed_compiler: CompilerLevel::read(p, "build/pluginManagement/plugins"), ..RawProfile::default() }; read_build(p, &mut profile.plugins, &mut profile.managed_plugins); @@ -521,15 +555,19 @@ struct Inherited { managed: Vec, plugins: Vec, managed_plugins: Vec, - /// The nearest `maven-compiler-plugin` `` / ``: a module usually - /// inherits its compiler level from the parent's (managed) plugin configuration. - compiler_release: Option, + /// A module usually inherits its compiler level from the parent's (managed) plugin + /// configuration. + compiler: CompilerLevel, + managed_compiler: CompilerLevel, /// `` and whether it is on unless the Maven tool window turns it off: profiles of the POM /// and its ancestors — Maven lets a child enable a parent's profiles with `-P`. profiles: Vec<(String, bool)>, /// A profile of the POM or an ancestor has a file condition, which looks at the folder of /// the project being built. per_project: bool, + /// The POM or an ancestor names a parent that is neither in the project nor in the local + /// repository, so what it sets is unknown. + parent_missing: bool, } fn merge_by(base: &mut Vec, own: &[T], key: impl Fn(&T) -> String) { @@ -858,7 +896,7 @@ impl Resolver { let mut ctx = match &pom.parent { Some(parent) if depth < MAX_PARENT_DEPTH => match self.parent_path(&dir, parent) { Some(pp) => self.context(&pp, project_dir, depth + 1), - None => Inherited::default(), + None => Inherited { parent_missing: true, ..Inherited::default() }, }, _ => Inherited::default(), }; @@ -869,9 +907,8 @@ impl Resolver { merge_by(&mut ctx.deps, &pom.deps, RawDep::key); merge_plugins(&mut ctx.plugins, &pom.plugins); merge_plugins(&mut ctx.managed_plugins, &pom.managed_plugins); - if pom.compiler_release.is_some() { - ctx.compiler_release = pom.compiler_release.clone(); - } + ctx.compiler.merge(&pom.compiler); + ctx.managed_compiler.merge(&pom.managed_compiler); let (on, implicit) = self.selection(&pom, project_dir); let active: Vec<&RawProfile> = pom.profiles.iter().zip(on).filter_map(|(p, on)| on.then_some(p)).collect(); for p in &active { @@ -879,6 +916,8 @@ impl Resolver { merge_by(&mut ctx.deps, &p.deps, RawDep::key); merge_plugins(&mut ctx.plugins, &p.plugins); merge_plugins(&mut ctx.managed_plugins, &p.managed_plugins); + ctx.compiler.merge(&p.compiler); + ctx.managed_compiler.merge(&p.managed_compiler); } // A parent and a child declaring the same id are two profiles; `-Pid` turns on both. for (p, on) in pom.profiles.iter().zip(implicit) { @@ -1827,11 +1866,23 @@ pub fn load_with_activation( }) .collect(); - let jv = ["maven.compiler.release", "java.version", "maven.compiler.source", "maven.compiler.target"] - .iter() - .find_map(|k| props.get(*k).map(|v| i(v))) - .or_else(|| ctx.compiler_release.as_deref().map(i)) - .and_then(|v| java_version(&v)); + // As maven-compiler-plugin hands it to javac: `release` wins over `source` and `target`, and + // the plugin's configuration over the `maven.compiler.*` property it defaults to. + let compiler = ctx.compiler.with_managed(&ctx.managed_compiler); + let mut levels = vec![ + compiler.release, + props.get("maven.compiler.release").cloned(), + compiler.source, + props.get("maven.compiler.source").cloned(), + compiler.target, + props.get("maven.compiler.target").cloned(), + ]; + // `java.version` reaches the compiler only through a parent such as spring-boot-starter-parent, + // which is unknown until it is in the local repository. + if ctx.parent_missing { + levels.push(props.get("java.version").cloned()); + } + let jv = levels.into_iter().flatten().find_map(|v| java_version(&i(&v))); let source_encoding = props .get("project.build.sourceEncoding") .map(|v| i(v).trim().to_owned()) @@ -3200,6 +3251,63 @@ mod tests { } } + #[test] + fn the_compiler_plugin_decides_the_java_level_before_the_properties() { + let tmp = tempfile::tempdir().unwrap(); + let plugins = |config: &str| { + format!( + "maven-compiler-plugin{config}" + ) + }; + let build = |config: &str| format!("{}", plugins(config)); + let managed = |config: &str| format!("{}", plugins(config)); + let properties = |props: &str| format!("{props}"); + // spring-boot-starter-parent passes `java.version` on as `maven.compiler.release`. + let boot = properties( + "17${java.version}", + ); + let parent = |content: &str| { + format!( + "demoparent1pom\ + app{content}" + ) + }; + let module = |parent: &str, content: &str| { + format!("{parent}app1{content}") + }; + let local = "demoparent1"; + let unknown = "org.springframework.bootspring-boot-starter-parent\ + 3.4.1"; + // (parent, module): what `mvn compile` passes to javac for each, with the parent in the project. + let cases = [ + (boot.clone(), module(local, &build("21"))), + ( + managed("21"), + module(local, &properties("17")), + ), + ( + properties("17"), + module(local, &build("1111")), + ), + (build("17"), module(local, &build("21"))), + ( + properties("17"), + module(local, &properties("11")), + ), + (properties("17"), module(local, "")), + // Before its parent is downloaded, `java.version` is all there is to go by. + (String::new(), module(unknown, &properties("17"))), + ]; + let mut levels = Vec::new(); + for (i, (parent_content, module)) in cases.iter().enumerate() { + let root = tmp.path().join(i.to_string()); + write(&root, "pom.xml", &parent(parent_content)); + write(&root, "app/pom.xml", module); + levels.push(load_with_repo(&root, None).projects.iter().find(|p| p.dir == "app").unwrap().java_version); + } + assert_eq!(levels, [Some(21), Some(21), Some(17), Some(17), Some(11), None, Some(17)]); + } + #[test] fn source_encoding_comes_from_the_pom_properties_and_is_inherited() { let tmp = tempfile::tempdir().unwrap(); @@ -3694,7 +3802,8 @@ mod tests { &repo, "org/boot/boot-parent/3.0/boot-parent-3.0.pom", r#"org.bootboot-parent3.0 - 179.9 + 179.9 + ${java.version} org.bootbom${project.parent.version}pomimport org.bootweb${lib.version} diff --git a/crates/ide-sdk/src/java.rs b/crates/ide-sdk/src/java.rs index bb4588977a..bffa21ae23 100644 --- a/crates/ide-sdk/src/java.rs +++ b/crates/ide-sdk/src/java.rs @@ -680,14 +680,15 @@ pub fn pick_jdk(jdks: &[Jdk], release: Option) -> Option<&Jdk> { .or(newest) } -/// Java version a Maven / Gradle module targets: ``, -/// `` / ``, Gradle toolchains +/// Java version a Maven / Gradle module targets: the compiler plugin's ``, +/// ``, else `` (which reaches the compiler +/// through a parent such as spring-boot-starter-parent), Gradle toolchains /// (`JavaLanguageVersion.of(21)`) or `sourceCompatibility`. pub fn project_java_version(module_dir: &Path) -> Option { let read = |name: &str| std::fs::read_to_string(module_dir.join(name)).ok(); if let Some(pom) = read("pom.xml") { for tag in - ["java.version", "maven.compiler.release", "release", "maven.compiler.source", "maven.compiler.target"] + ["release", "maven.compiler.release", "maven.compiler.source", "maven.compiler.target", "java.version"] { if let Some(v) = xml_value(&pom, tag).and_then(|v| parse_java_version(&v)) { return Some(v); @@ -1595,6 +1596,22 @@ echo 'openjdk version 1.8' assert_eq!(project_java_version(dir), Some(21)); std::fs::write(dir.join("pom.xml"), "1.8").unwrap(); assert_eq!(project_java_version(dir), Some(8)); + // The compiler plugin's level, then `maven.compiler.*`; `java.version` reaches the compiler + // only through a parent such as spring-boot-starter-parent. + let compiler = "maven-compiler-plugin21"; + for (pom, version) in [ + ( + format!( + "17{compiler}" + ), + 21, + ), + ("1721".to_owned(), 21), + ("1711".to_owned(), 11), + ] { + std::fs::write(dir.join("pom.xml"), &pom).unwrap(); + assert_eq!(project_java_version(dir), Some(version), "{pom}"); + } std::fs::remove_file(dir.join("pom.xml")).unwrap(); std::fs::write( dir.join("build.gradle.kts"),