Set Java source and target when compiling AOT generated sources
When compiling AOT-generated sources in the Maven plugin `process-aot` and `process-test-aot` goals, the Java compiler should be provided with the same `--source`, `--target`, and `--release` configuration values as the Maven compiler plugin uses to compile main sources. Fixes gh-33112
This commit is contained in:
parent
11652bd8e6
commit
c28c614c47
|
|
@ -130,11 +130,21 @@ public abstract class AbstractAotMojo extends AbstractDependencyFilterMojo {
|
|||
}
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
|
||||
JavaCompilerPluginConfiguration compilerConfiguration = new JavaCompilerPluginConfiguration(this.project);
|
||||
List<String> options = new ArrayList<>();
|
||||
options.add("-cp");
|
||||
options.add(ClasspathBuilder.build(Arrays.asList(classPath)));
|
||||
options.add("-d");
|
||||
options.add(outputDirectory.toPath().toAbsolutePath().toString());
|
||||
options.add("--source");
|
||||
options.add(compilerConfiguration.getSourceMajorVersion());
|
||||
options.add("--target");
|
||||
options.add(compilerConfiguration.getTargetMajorVersion());
|
||||
String releaseVersion = compilerConfiguration.getReleaseVersion();
|
||||
if (releaseVersion != null) {
|
||||
options.add("--release");
|
||||
options.add(releaseVersion);
|
||||
}
|
||||
options.addAll(new RunArguments(this.compilerArguments).getArgs());
|
||||
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromPaths(sourceFiles);
|
||||
Errors errors = new Errors();
|
||||
|
|
|
|||
|
|
@ -55,6 +55,16 @@ class JavaCompilerPluginConfiguration {
|
|||
return majorVersionFor(version);
|
||||
}
|
||||
|
||||
String getReleaseVersion() {
|
||||
String version = getConfigurationValue("release");
|
||||
|
||||
if (version == null) {
|
||||
version = getPropertyValue("maven.compiler.release");
|
||||
}
|
||||
|
||||
return majorVersionFor(version);
|
||||
}
|
||||
|
||||
private String getConfigurationValue(String propertyName) {
|
||||
Plugin plugin = this.project.getPlugin("org.apache.maven.plugins:maven-compiler-plugin");
|
||||
if (plugin != null) {
|
||||
|
|
|
|||
|
|
@ -59,19 +59,22 @@ class JavaCompilerPluginConfigurationTests {
|
|||
JavaCompilerPluginConfiguration configuration = new JavaCompilerPluginConfiguration(this.project);
|
||||
assertThat(configuration.getSourceMajorVersion()).isNull();
|
||||
assertThat(configuration.getTargetMajorVersion()).isNull();
|
||||
assertThat(configuration.getReleaseVersion()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void versionsAreReturnedFromConfiguration() throws IOException, XmlPullParserException {
|
||||
Xpp3Dom dom = buildConfigurationDom("<source>1.9</source>", "<target>11</target>");
|
||||
Xpp3Dom dom = buildConfigurationDom("<source>1.9</source>", "<target>11</target>", "<release>12</release>");
|
||||
given(this.plugin.getConfiguration()).willReturn(dom);
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("maven.compiler.source", "1.8");
|
||||
properties.setProperty("maven.compiler.target", "10");
|
||||
properties.setProperty("maven.compiler.release", "11");
|
||||
given(this.project.getProperties()).willReturn(properties);
|
||||
JavaCompilerPluginConfiguration configuration = new JavaCompilerPluginConfiguration(this.project);
|
||||
assertThat(configuration.getSourceMajorVersion()).isEqualTo("9");
|
||||
assertThat(configuration.getTargetMajorVersion()).isEqualTo("11");
|
||||
assertThat(configuration.getReleaseVersion()).isEqualTo("12");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -80,10 +83,12 @@ class JavaCompilerPluginConfigurationTests {
|
|||
Properties properties = new Properties();
|
||||
properties.setProperty("maven.compiler.source", "1.8");
|
||||
properties.setProperty("maven.compiler.target", "11");
|
||||
properties.setProperty("maven.compiler.release", "12");
|
||||
given(this.project.getProperties()).willReturn(properties);
|
||||
JavaCompilerPluginConfiguration configuration = new JavaCompilerPluginConfiguration(this.project);
|
||||
assertThat(configuration.getSourceMajorVersion()).isEqualTo("8");
|
||||
assertThat(configuration.getTargetMajorVersion()).isEqualTo("11");
|
||||
assertThat(configuration.getReleaseVersion()).isEqualTo("12");
|
||||
}
|
||||
|
||||
private Xpp3Dom buildConfigurationDom(String... properties) throws IOException, XmlPullParserException {
|
||||
|
|
|
|||
Loading…
Reference in New Issue