Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maven compiler plugin configuration should not include source and target as maven.compiler.release is already set #34761

Closed
wants to merge 1 commit into from

Conversation

arend-von-reinersdorff
Copy link
Contributor

Follow-up on #34365.

When the compiler's release argument is set, arguments source and target are not used. No need to keep them around.

See https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#option-release

Note: When using --release, you cannot also use the --source/-source or --target/-target options.

Maven actually allows setting both. But using both directly with javac would result in an error.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 25, 2023
@paulvi
Copy link

paulvi commented Mar 26, 2023

Indeed a073ef8 was not enough,
removing 2 older option in 1f71fb5 is correct

Note that with javac --release option
it is possible to to target even older releases 7,8 (while --release was introduced in JDK 9)

e.g. for JDK 19

  --release <release>
        Compile for the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19

I wonder it is possible to target Spring and Spring Boot to JDK 8 via this cross compilation?

But for sure simpler project can use this to target jdk 8 or jdk 11

@paulvi
Copy link

paulvi commented Mar 26, 2023

And I am sure this switch to --release option should be refrected somewhere in docs
as in real world Spring Boot projects ther eis still mix of using

    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>

or configuration section for maven-compiler-plugin
https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

I wonder what exact error maven or javac give when there is mix of --target and --release options?

@khmarbaise
Copy link
Contributor

There is also an explicit reference for JDK9+ https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-release.html

@paulvi
Copy link

paulvi commented Mar 29, 2023

Maven docs however do not make it clear that there is big difference between --source --target vs --release
as now in Java it became common that APIs can appear and go away, e.g. Nashorn,
so it is imperative to compare against what APIs are actually avaiable for targeted release

https://stackoverflow.com/questions/43102787/what-is-the-release-flag-in-the-java-9-compiler

@paulvi
Copy link

paulvi commented Mar 29, 2023

I have tried Spring Boot 3.0 with added section like

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.11.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<release>17</release>
				</configuration>
			</plugin>

In this case maven-compiler-plugin 3.11.0 just ignores source and target

[INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ ecbrates ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 10 source files with javac [debug release 17] to target/classes

But when <release>8</release> the error message is misleading:
records are not supported in -source 8

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project ecbrates: Compilation failure
[ERROR] /Users/paulverest/Workspaces/Try/ecbrates/src/main/java/com/currencies/ecbrates/model/Quote.java:[7,8] records are not supported in -source 8
[ERROR]   (use -source 16 or higher to enable records)

That is telling about --source option not about --release;
so of course configuration like

					<source>17</source>
					<target>17</target>
					<release>8</release>

will not make difference.

https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html page is also misleading
it says there is Default value is: 1.8. for
<source>
But says nothing about default value for
<release>
or that it will override <source> configuration.


In short this PR will make --release work, though some developer will come through not so nice hints when running into errors. But that is actually problem for maven-compiler-plugin to solve.

But whatever wording of error messages, the Stackoverflow.com solution will be:

for Spring Boot 3.1+ use

<properties>
		<java.version>17</java.version>

@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 5, 2023
@snicoll snicoll added this to the 3.1.0-RC1 milestone Apr 5, 2023
@snicoll snicoll self-assigned this Apr 5, 2023
@snicoll snicoll changed the title Remove source and target as maven.compiler.release is already set Maven compiler plugin configuration should not include source and target as maven.compiler.release is already set Apr 5, 2023
@snicoll
Copy link
Member

snicoll commented Apr 5, 2023

@arend-von-reinersdorff thank you for making your first contribution to Spring Boot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants