Issue
I am getting the following output when executing spring boot tests using java 11.
I am also getting the exact same error when I execute
java -jar target\application.jar
So it's not just a maven issue.
***************************
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
It seems that the wrong dependency is being loaded?
I have these configurations in 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>
with build configuration...
<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>
What is the correct method to fix this, so that the tests execute correctly out of the box?
Action:
Correct the classpath of your application so that it contains a single, compatible version of javax.annotation.Resource
edit
I have also tried:
<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>
Solution
You can try something like this if you will;
<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>
I'm using this kind declaration to add javax.activation-api
and plexus-archiver
to dockerfile-maven-plugin
plugin. But my spring-boot-starter-parent
version is 2.1.4.RELEASE
and maven-compiler-plugin
version is 3.8.0
.
Answered By - Sachith Dickwella
Answer Checked By - Candace Johnson (JavaFixing Volunteer)