我正在将我们的项目从Java 8迁移到11。
第一步,我使用源和目标兼容性1.8对其进行编译,但尝试在openjdk-11上运行该应用程序。
在开发过程中,我们正在使用jetty:run目标来启动应用程序服务器。此目标失败,并出现以下错误:
Caused by: java.lang.NoSuchMethodError: javax.annotation.Resource.lookup()Ljava/lang/String;
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.<init>(CommonAnnotationBeanPostProcessor.java:621) ~[spring-context-5.1.0.RELEASE.jar:5.1.0.RELEASE]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.lambda$buildResourceMetadata$0(CommonAnnotationBeanPostProcessor.java:383) ~[spring-context-5.1.0.RELEASE.jar:5.1.0.RELEASE]
at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:713) ~[spring-core-5.1.0.RELEASE.jar:5.1.0.RELEASE]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:365) ~[spring-context-5.1.0.RELEASE.jar:5.1.0.RELEASE]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:350) ~[spring-context-5.1.0.RELEASE.jar:5.1.0.RELEASE]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:298) ~[spring-context-5.1.0.RELEASE.jar:5.1.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1043) ~[spring-beans-5.1.0.RELEASE.jar:5.1.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:550) ~[spring-beans-5.1.0.RELEASE.jar:5.1.0.RELEASE]
... 63 more
我找到了@Resource
注释的错误版本来自:
URL [jar:file:[...]/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar!/javax/annotation/Resource.class]
URL [jar:file:[...]/Applications/apache-maven-3.5.4/lib/jsr250-api-1.0.jar!/javax/annotation/Resource.class]
看来maven会将其在/ lib /文件夹中的所有库都放到了插件类路径中
我猜想当我在jdk 1.8上运行它时,来自jdk的@Resource
注释具有优先权(因为我从未见过此错误),但是情况不再如此(因为该注释不是包括在jdk11中。)
要摆脱这个旧课程,我有什么选择?
(我可以从maven安装中升级lib,但我宁愿将其保留为万不得已,因为它要求每个从事该项目的人都对其maven安装进行调整)。
编辑:jetty:run-forked目标也可以正常工作,但是有了这个目标,我无法保留插件的<webApp>
配置部分。
这似乎确实是一个Maven类路径隔离问题。
答案 0 :(得分:3)
关于Using Extensions和Core Classloader,我们可以通过在${maven.home}/lib
处定义extensions
来覆盖pom.xml
,如下所示:-< / p>
<build>
<extensions>
<extension>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</extension>
<extension>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</extension>
</extensions>
</build>
执行mvn -e -X clean test
时,它会为我们提供以下内容:-
[DEBUG] javax.annotation:javax.annotation-api:jar:1.3.2:
[DEBUG] org.codehaus.plexus:plexus-utils:jar:1.1:runtime
[DEBUG] Created new class realm extension>
javax.annotation:javax.annotation-api:1.3.2
[DEBUG] Importing foreign packages into class realm extension>
javax.annotation:javax.annotation-api:1.3.2
[DEBUG] Imported: < maven.api
[DEBUG] Populating class realm extension>
javax.annotation:javax.annotation-api:1.3.2
[DEBUG] Included: javax.annotation:javax.annotation-api:jar:1.3.2
[DEBUG] Included: org.codehaus.plexus:plexus-utils:jar:1.1
[DEBUG] Dependency collection stats: {...}
[DEBUG] javax.annotation:jsr250-api:jar:1.0:
[DEBUG] org.codehaus.plexus:plexus-utils:jar:1.1:runtime
[DEBUG] Created new class realm extension>
javax.annotation:jsr250-api:1.0
[DEBUG] Importing foreign packages into class realm extension>
javax.annotation:jsr250-api:1.0
[DEBUG] Imported: < maven.api
[DEBUG] Populating class realm extension>
javax.annotation:jsr250-api:1.0
....
[DEBUG] Extension realms for project some-group:some-artifact:some-packing:some-version :
[ClassRealm[extension>javax.annotation:javax.annotation-api:1.3.2,
parent: sun.misc.Launcher$AppClassLoader@5c647e05],
ClassRealm[extension>javax.annotation:jsr250-api:1.0,
parent: sun.misc.Launcher$AppClassLoader@5c647e05]]
[DEBUG] Created new class realm project>some-group:some-artifact:some-version
[DEBUG] Populating class realm project>some-group:some-artifact:some-version
[DEBUG] Included: javax.annotation:javax.annotation-api:jar:1.3.2
[DEBUG] Looking up lifecycle mappings for packaging war from
ClassRealm[project>some-group:some-artifact:some-version,
parent: ClassRealm[maven.api, parent: null]]
我无法摆脱javax.annotation:jsr250-api:1.0
,然后将其放在javax.annotation:javax.annotation-api:1.3.2
之后,以使其远离类路径,并确保首先使用javax.annotation:javax.annotation-api:1.3.2
。 / p>