spring 启动无法在 java 11 中加载和执行测试

spring boot fails to load and execute tests in java 11

使用 java 11.
执行 spring 引导测试时,我得到以下输出 我在执行

时也遇到了完全相同的错误

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>

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

<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.