Skip to content

Commit

Permalink
Set Java source and target when compiling AOT generated sources
Browse files Browse the repository at this point in the history
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
  • Loading branch information
scottfrederick committed Nov 14, 2022
1 parent 11652bd commit c28c614
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
Expand Up @@ -130,11 +130,21 @@ protected final void compileSourceFiles(URL[] classPath, File sourcesDirectory,
}
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();
Expand Down
Expand Up @@ -55,6 +55,16 @@ String getTargetMajorVersion() {
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) {
Expand Down
Expand Up @@ -59,19 +59,22 @@ void versionsAreNullWithNoConfiguration() {
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
Expand All @@ -80,10 +83,12 @@ void versionsAreReturnedFromProperties() {
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 {
Expand Down

0 comments on commit c28c614

Please sign in to comment.