代码之家  ›  专栏  ›  技术社区  ›  Thierry

如何从插件类路径中删除maven库(jsr250-api-1.0.jar)(jetty:run)?

  •  1
  • Thierry  · 技术社区  · 7 年前

    我正在将我们的项目从Java8迁移到Java11。

    在开发过程中,我们使用jetty:run goal 启动应用服务器。此目标因以下错误而失败:

    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/文件夹中的所有lib放在插件类路径上

    来自jdk的注释具有优先权(正如我以前所看到的这个错误),但现在不是了(因为jdk11中不再包含这个注释)。

    我有什么办法摆脱这个旧阶级?

    (我可以从maven安装中升级lib,但我更愿意将此作为最后的手段,这将要求项目中的每个人都调整其maven安装)。

    <webApp> 插件的配置部分。

    这似乎真的是一个maven类路径隔离问题。

    1 回复  |  直到 6 年前
        1
  •  5
  •   Charlee Chitsuk    7 年前

    Using Extensions Core Classloader ,我们可以覆盖 ${maven.home}/lib 通过定义 extensions pom.xml 具体如下:-

    pom.xml文件

    <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.a公司nnotation:javax.annotation-api:1.3.2 将首先使用。