【问题标题】:spring boot fails to load and execute tests in java 11spring boot 无法在 java 11 中加载和执行测试
【发布时间】:2019-07-03 08:26:27
【问题描述】:

使用 java 11 执行 Spring Boot 测试时,我得到以下输出。
当我执行时,我也得到了完全相同的错误

java -jar target\application.jar

所以这不仅仅是一个 maven 问题。

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. 
The attempt was made from the following location:

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.<init>(CommonAnnotationBeanPostProcessor.java:627)
javax.annotation.Resource.lookup()Ljava/lang/String; but it does not exist. Its class, javax.annotation.Resource, is available from the following locations:


    jar:file:/C:/Users/X/.m2/repository/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar!/javax/annotation/Resource.class
    jar:file:/C:/Users/X/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar!/javax/annotation/Resource.class

It was loaded from the following location:

    file:/C:/Users/X/.m2/repository/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar

似乎加载了错误的依赖项? 我在pom.xml中有这些配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/>
    <!-- lookup parent from repository -->
</parent>
<dependencies>
    ...
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
</dependencies>

使用构建配置...

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>                        <!-- or newer version -->
    <configuration>
        <source>11</source>                            <!-- depending on your project -->
        <target>11</target>                            <!-- depending on your project -->
        <compilerArgs>
        </compilerArgs>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${org.mapstruct.version}</version>
            </path>
            <!-- other annotation processors -->
        </annotationProcessorPaths>
    </configuration>
</plugin>

解决这个问题的正确方法是什么,以便测试可以开箱即用地正确执行?

行动:

更正应用程序的类路径,使其包含一个兼容的 javax.annotation.Resource 版本

编辑

我也试过了:

<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>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • 它适用于旧版本/其他版本的 java 吗?
  • 你用的是什么版本的spring boot?并非所有这些都与 java9+ 兼容
  • 我已使用最新版本更新了问题的详细信息。

标签: java spring-boot java-11


【解决方案1】:

如果你愿意,你可以尝试这样的事情;

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>                      
    <configuration>
        <source>11</source>         
        <target>11</target>          
    </configuration>
    <dependencies>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>   
    </dependencies>
</plugin>

我正在使用这种声明将javax.activation-apiplexus-archiver 添加到dockerfile-maven-plugin 插件。但是我的spring-boot-starter-parent 版本是2.1.4.RELEASEmaven-compiler-plugin 版本是3.8.0

【讨论】:

  • 我尝试了你的建议,但它仍然加载了错误的 Resource.class
  • 我也更新到了 maven-compiler-plugin 的 3.8.0 版本
  • 我已经更新了问题我也有这个问题只是执行 jar。
  • 它是由 POM mvn -e -X 中的另一个依赖引起的,我找到了它。
猜你喜欢
  • 2021-10-18
  • 2020-09-26
  • 2019-03-18
  • 2020-01-22
  • 2019-01-30
  • 1970-01-01
  • 2019-11-24
  • 2020-01-02
  • 2019-04-26
相关资源
最近更新 更多