davidB / scala-maven-plugin

The scala-maven-plugin (previously maven-scala-plugin) is used for compiling/testing/running/documenting scala code in maven.
https://davidb.github.io/scala-maven-plugin/
The Unlicense
557 stars 151 forks source link

bad option '-release' #683

Closed unihubster closed 1 year ago

unihubster commented 1 year ago

Got an issue about '-release'. Here are my test results:

Scala 2.12.17, JDK in pom: 11, build on JDK: 1.8, scala-maven-plugin 3.8.1, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.17__52.0-1.8.0_20221110T195421.jar Error message: "'11' is not a valid choice for '-release'", "bad option '-release'" Build failed

Scala 2.12.17, JDK in pom: 1.8, build on JDK: 11, scala-maven-plugin 3.8.1, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.17__52.0-1.8.0_20221110T195421.jar Works without errors. Build success

Scala 2.12.17, JDK in pom: 11, build on JDK: 11, scala-maven-plugin 3.8.1, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.17__55.0-1.8.0_20221110T195421.jar Works without errors. Build success

Scala 2.12.17, JDK in pom: 1.8, build on JDK: 1.8, scala-maven-plugin 3.8.1, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.17__52.0-1.8.0_20221110T195421.jar Works without errors. Build success

Scala 2.12.16, JDK in pom: 11, build on JDK: 1.8, scala-maven-plugin 3.8.1, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.16__52.0-1.8.0_20221110T195421.jar Error message: "-release is only supported in Java 9 and higher" Build success

Scala 2.12.16, JDK in pom: 1.8, build on JDK: 1.8, scala-maven-plugin 3.8.1, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.16__52.0-1.8.0_20221110T195421.jar Error message: "-release is only supported in Java 9 and higher" Build success

Scala 2.12.17, JDK in pom: 11, build on JDK: 1.8, scala-maven-plugin 3.8.0, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.17__52.0-1.8.0_20221110T195421.jar Error message: "-release is only supported in Java 9 and higher" Build success

Scala 2.12.17, JDK in pom: 11, build on JDK: 11, scala-maven-plugin 3.8.0, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.17__55.0-1.8.0_20221110T195421.jar Works without errors. Build success

Scala 2.12.17, JDK in pom: 11, build on JDK: 1.8, scala-maven-plugin 3.7.1, org.scala-sbt-compiler-bridge_2.12-1.8.0-bin_2.12.17__52.0-1.8.0_20221110T195421.jar Works without errors. Build success

I got the issue because DevOps didn't change JDK version for build. But maybe this will be useful for you to improve your code because, as you can see, scala-maven-plugin version=<v4.7.1 doesn't produce the issue, version=4.8.0 produces the error message but build is successful, build failed with version=4.8.1 and Scala 2.12.17, error message with successful build is with Scala 2.12.16 and plugin version=4.8.1, when JDK is v11 in pom with actually used for build JDK v1.8.

slandelle commented 1 year ago

What do you mean by "JDK in pom"?

unihubster commented 1 year ago

Of course usual properties: <properties> <java.version>11</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> </properties>

slandelle commented 1 year ago

The behavior of the previous versions of the scala-maven-plugin was broken. Its behavior should be consistent with maven-compiler-plugin and javac. In particular, trying to set a maven.compiler.target option that's higher than the javac version is non sensical and must trigger a crash and not silently discard the invalid option. You can verify this behavior with a simple java project.

Your build matrix is broken and you must remove non sensical combinations.

unihubster commented 1 year ago

Yes, as I before wrote that was unusual set up. But problem is in that, if project has no Java classes, but only Scala classes, there is no any alerts from maven-compiler-plugin and javac about wrong settings. But build crushes not because wrong target but because bad option '-release' with scala-maven-plugin 4.8.1. This is wrong behavior of scala-maven-plugin which should be fixed.

slandelle commented 1 year ago

But problem is in that, if project has no Java classes, but only Scala classes, there is no any alerts from maven-compiler-plugin and javac about wrong settings.

This statement doesn't make sense. maven-compiler-plugin only compiles Java code. What I'm saying is that f you're having a pure Java project and setting maven.compiler.target to a version that's higher than the JDK in use, you'll get the same kind of error, eg try to compile this with JDK 11:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>demo</groupId>
  <artifactId>maven-test-java</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>
  </properties>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
      </plugin>
    </plugins>
  </build>
</project>
unihubster commented 1 year ago

I'm talking about following cases: 1) Maven project has target higher than JDK version used for compilation. There are both java and scala classes in the project. Build error message is Fatal error compiling: invalid target release: 11 2) Maven project has target higher than JDK version used for compilation. There are only scala classes in the project. Build error message is: '11' is not a valid choice for '-release'\n bad option: '-release' As you can see here, maven-compiler-plugin message is more user friendly, it contains words from user experience (compiling, target release), when scala-maven-plugin message is about some internal option about which the user has no idea and what this message really means. It's especially unclear what happens when build is success with scala-maven-plugin version <=4.7.1 and build is broken only when the plugin version was changed to new one. For me personally, such message is like NPE: "you have a problem, but I won't tell you exactly where". )) And as you can see, even when target is 9 and higher scala-sbt-compiler-bridge uses specification for v1.8. So, build for scala classes is correct with JDK 8 even when target is higher in pom and this restriction doesn't really make sense.

slandelle commented 1 year ago

As you can see here, maven-compiler-plugin message is more user friendly

These messages are generated by the scala compiler.

build is broken only when the plugin version was changed to new one

Behavior was fixed and made more consistent.

Let's agree we disagree. I won't make special cases of "pure Scala" vs "pure Java" vs "mixed Scala + Java".

unihubster commented 1 year ago

These messages are generated by the scala compiler.

Why are you so sure if you wrote before that java classes aren't compiled by scala-maven-plugin? )) Here is the whole error message: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project ScalaMaven: Fatal error compiling: error: invalid target release: 11 -> [Help 1] As you can see, it's produced by maven-compiler-plugin, not by scala-maven-plugin and it's more clear. When an error message really generated by scala-maven-plugin is not clear (user can see it when project has no java classes), it's about its internal structure which is very bad practice. If you think about it a bit, this is not about supporting individual special cases, but about how to make the error messages more user friendly, and not about options that the user did not set anywhere himself.