最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

maven - Methods not available for compile-time Java version - Stack Overflow

programmeradmin6浏览0评论

On my computer, I have Java 11. I have a Java project that uses Maven. I have two GIT branches, to generate a jar in version 11 and another in version 1.8.

On my branch, which is planned for 1.8, in the maven-compiler-plugin plugin, I have correctly specified the source and target as 1.8. Unfortunately, after cherry-picking my Java 11 branch from the Java 1.8 branch, I'm using a method that is only available for Java 11 (I use java.lang.String#stripTrailing, available since Java 11+). The compilation doesn't give me a NoSuchMethodError. Furthermore, when tests are run via the default Maven tasks, I don't get any errors, even though that should be the case. How can I make the error reappear without having to install two Java versions and change the Java runtime when I switch branches?

c:\temp>mvn -v
Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256)
Maven home: C:\Java\apache-maven-3.9.8
Java version: 11.0.16, vendor: Oracle Corporation, runtime: c:\Java\jdk-11.0.16

On my computer, I have Java 11. I have a Java project that uses Maven. I have two GIT branches, to generate a jar in version 11 and another in version 1.8.

On my branch, which is planned for 1.8, in the maven-compiler-plugin plugin, I have correctly specified the source and target as 1.8. Unfortunately, after cherry-picking my Java 11 branch from the Java 1.8 branch, I'm using a method that is only available for Java 11 (I use java.lang.String#stripTrailing, available since Java 11+). The compilation doesn't give me a NoSuchMethodError. Furthermore, when tests are run via the default Maven tasks, I don't get any errors, even though that should be the case. How can I make the error reappear without having to install two Java versions and change the Java runtime when I switch branches?

c:\temp>mvn -v
Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256)
Maven home: C:\Java\apache-maven-3.9.8
Java version: 11.0.16, vendor: Oracle Corporation, runtime: c:\Java\jdk-11.0.16
Share Improve this question edited Apr 1 at 8:13 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Apr 1 at 8:11 AnswerrerAnswerrer 3,5934 gold badges11 silver badges19 bronze badges 3
  • stackoverflow/a/19324816/984823 So indeed the real java 8 is used. And make a unit test that maven runs with java 8. – Joop Eggen Commented Apr 1 at 8:26
  • This solution requires all contributors to have this specific configuration. Therefore, the configuration is not linked to the project and must be replicated on developer workstations and in CI pipelines. Is it not possible to have a working configuration in the GIT project ? – Answerrer Commented Apr 1 at 8:35
  • 1 Something with a settings.xml you mean, stackoverflow/a/20787334/984823 – Joop Eggen Commented Apr 1 at 8:45
Add a comment  | 

1 Answer 1

Reset to default 3

You need to set

<properties>
    <mavenpiler.release>8</mavenpiler.release>
</properties>

as well so that you only compile to the Java 8 API.

发布评论

评论列表(0)

  1. 暂无评论