дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 01:57:13 +02:00
fix(maven): read the Java level from the compiler plugin before properties (#8206)
A Maven module's Java level was taken from the maven.compiler.release, java.version, maven.compiler.source and maven.compiler.target properties before the compiler plugin's configuration. With spring-boot-starter-parent (java.version 17) and <release>21</release> in the module's maven-compiler-plugin, the model said 17, so Maven and javac got JDK 17 and the build failed with "release version 21 not supported". The level is now read the way maven-compiler-plugin hands it to javac: release before source and target, and the plugin's configuration (merged element by element through parents, <pluginManagement> and active profiles) before the maven.compiler.* property it defaults to. java.version is read only while the parent that passes it on is not in the local repository yet. ide-sdk's JDK pick follows the same order. Co-authored-by: ntbang0901 <[email protected]>
Цей коміт міститься в:
1 джерело
b5f7f0f472
коміт
bb7cacb357
2 files changed
+157
-31
No files matched your search
@@ -327,6 +327,8 @@ struct RawProfile {
|
||||
managed: Vec<RawDep>,
|
||||
plugins: Vec<RawPlugin>,
|
||||
managed_plugins: Vec<RawPlugin>,
|
||||
compiler: CompilerLevel,
|
||||
managed_compiler: CompilerLevel,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
@@ -344,9 +346,49 @@ struct RawPom {
|
||||
plugins: Vec<RawPlugin>,
|
||||
managed_plugins: Vec<RawPlugin>,
|
||||
profiles: Vec<RawProfile>,
|
||||
/// `maven-compiler-plugin` `<release>` / `<source>` of `<build><plugins>`, else of
|
||||
/// `<pluginManagement>`.
|
||||
compiler_release: Option<String>,
|
||||
/// Of `<build><plugins>`.
|
||||
compiler: CompilerLevel,
|
||||
/// Of `<pluginManagement>`.
|
||||
managed_compiler: CompilerLevel,
|
||||
}
|
||||
|
||||
/// The Java level of a `maven-compiler-plugin` `<configuration>`.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct CompilerLevel {
|
||||
release: Option<String>,
|
||||
source: Option<String>,
|
||||
target: Option<String>,
|
||||
}
|
||||
|
||||
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 `<pluginManagement>` adds.
|
||||
fn with_managed(&self, managed: &Self) -> Self {
|
||||
let mut config = managed.clone();
|
||||
config.merge(self);
|
||||
config
|
||||
}
|
||||
}
|
||||
|
||||
fn read_dep(e: &Element) -> Option<RawDep> {
|
||||
@@ -401,16 +443,6 @@ fn read_build(e: &Element, plugins: &mut Vec<RawPlugin>, managed: &mut Vec<RawPl
|
||||
managed.extend(e.list("build/pluginManagement/plugins", "plugin").into_iter().filter_map(read_plugin));
|
||||
}
|
||||
|
||||
/// `maven-compiler-plugin` `<release>` / `<source>` among the plugins at `path`.
|
||||
fn compiler_level(project: &Element, path: &str) -> Option<String> {
|
||||
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<RawDep>,
|
||||
plugins: Vec<RawPlugin>,
|
||||
managed_plugins: Vec<RawPlugin>,
|
||||
/// 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>,
|
||||
/// A module usually inherits its compiler level from the parent's (managed) plugin
|
||||
/// configuration.
|
||||
compiler: CompilerLevel,
|
||||
managed_compiler: CompilerLevel,
|
||||
/// `<id>` 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<T: Clone>(base: &mut Vec<T>, 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!(
|
||||
"<plugins><plugin><artifactId>maven-compiler-plugin</artifactId><configuration>{config}</configuration></plugin></plugins>"
|
||||
)
|
||||
};
|
||||
let build = |config: &str| format!("<build>{}</build>", plugins(config));
|
||||
let managed = |config: &str| format!("<build><pluginManagement>{}</pluginManagement></build>", plugins(config));
|
||||
let properties = |props: &str| format!("<properties>{props}</properties>");
|
||||
// spring-boot-starter-parent passes `java.version` on as `maven.compiler.release`.
|
||||
let boot = properties(
|
||||
"<java.version>17</java.version><maven.compiler.release>${java.version}</maven.compiler.release>",
|
||||
);
|
||||
let parent = |content: &str| {
|
||||
format!(
|
||||
"<project><groupId>demo</groupId><artifactId>parent</artifactId><version>1</version><packaging>pom</packaging>\
|
||||
<modules><module>app</module></modules>{content}</project>"
|
||||
)
|
||||
};
|
||||
let module = |parent: &str, content: &str| {
|
||||
format!("<project>{parent}<artifactId>app</artifactId><version>1</version>{content}</project>")
|
||||
};
|
||||
let local = "<parent><groupId>demo</groupId><artifactId>parent</artifactId><version>1</version></parent>";
|
||||
let unknown = "<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId>\
|
||||
<version>3.4.1</version><relativePath/></parent>";
|
||||
// (parent, module): what `mvn compile` passes to javac for each, with the parent in the project.
|
||||
let cases = [
|
||||
(boot.clone(), module(local, &build("<release>21</release>"))),
|
||||
(
|
||||
managed("<release>21</release>"),
|
||||
module(local, &properties("<maven.compiler.release>17</maven.compiler.release>")),
|
||||
),
|
||||
(
|
||||
properties("<maven.compiler.release>17</maven.compiler.release>"),
|
||||
module(local, &build("<source>11</source><target>11</target>")),
|
||||
),
|
||||
(build("<release>17</release>"), module(local, &build("<source>21</source>"))),
|
||||
(
|
||||
properties("<java.version>17</java.version>"),
|
||||
module(local, &properties("<maven.compiler.source>11</maven.compiler.source>")),
|
||||
),
|
||||
(properties("<java.version>17</java.version>"), module(local, "")),
|
||||
// Before its parent is downloaded, `java.version` is all there is to go by.
|
||||
(String::new(), module(unknown, &properties("<java.version>17</java.version>"))),
|
||||
];
|
||||
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#"<project><groupId>org.boot</groupId><artifactId>boot-parent</artifactId><version>3.0</version>
|
||||
<properties><java.version>17</java.version><lib.version>9.9</lib.version></properties>
|
||||
<properties><java.version>17</java.version><lib.version>9.9</lib.version>
|
||||
<maven.compiler.release>${java.version}</maven.compiler.release></properties>
|
||||
<dependencyManagement><dependencies>
|
||||
<dependency><groupId>org.boot</groupId><artifactId>bom</artifactId><version>${project.parent.version}</version><type>pom</type><scope>import</scope></dependency>
|
||||
<dependency><groupId>org.boot</groupId><artifactId>web</artifactId><version>${lib.version}</version></dependency>
|
||||
|
||||
@@ -680,14 +680,15 @@ pub fn pick_jdk(jdks: &[Jdk], release: Option<u32>) -> Option<&Jdk> {
|
||||
.or(newest)
|
||||
}
|
||||
|
||||
/// Java version a Maven / Gradle module targets: `<java.version>`,
|
||||
/// `<maven.compiler.release|source|target>` / `<release>`, Gradle toolchains
|
||||
/// Java version a Maven / Gradle module targets: the compiler plugin's `<release>`,
|
||||
/// `<maven.compiler.release|source|target>`, else `<java.version>` (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<u32> {
|
||||
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"), "<maven.compiler.source>1.8</maven.compiler.source>").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 = "<plugin><artifactId>maven-compiler-plugin</artifactId><configuration><release>21</release></configuration></plugin>";
|
||||
for (pom, version) in [
|
||||
(
|
||||
format!(
|
||||
"<properties><java.version>17</java.version></properties><build><plugins>{compiler}</plugins></build>"
|
||||
),
|
||||
21,
|
||||
),
|
||||
("<java.version>17</java.version><maven.compiler.release>21</maven.compiler.release>".to_owned(), 21),
|
||||
("<java.version>17</java.version><maven.compiler.source>11</maven.compiler.source>".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"),
|
||||
|
||||
Reference in new issue
Заблокувати користувача