简体   繁体   中英

maven build uses jdk home\src.zip!\javax\annotation\Resource.java not dependency

Here is the error show in intellij, but it is the same in maven :

在此处输入图片说明

[34,14] cannot find symbol
[ERROR] symbol  : method lookup()
[ERROR] location: @interface javax.annotation.Resource
[ERROR] -> [Help 1]

My pom has the depedncy included like so :

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-api</artifactId>
  <version>6.0</version>
</dependency>

This results in the lookup method, on Resource annoation not being found.

I think this is a class loading issue. The JDK classes will load first before anything else in the class path. If you want to override this, there is the standard endorsed lib mechanism. Trying copying the API JAR to /lib/endorsed at your JDK home.

Java EE has some different versions of APIs which are shipped with the JDK.

To overcome this issue there is the concept of "endorsed APIs".

Java SE 7 lists the "Common Annotations for the Java Platform" as an endorsed API: http://docs.oracle.com/javase/7/docs/technotes/guides/standards/

Java SE 8 doesn't list the "Common Annotations for the Java Platform" as an endorsed API anymore for some reason, but AFAIK it is still supported (documentation bug?): http://docs.oracle.com/javase/8/docs/technotes/guides/standards/

Note however that the endorsed API concept has been deprecated and likely won't work with Java SE 9 anymore: https://www.java.com/en/download/faq/release_changes.xml

It will likely be replaced by Jigsaw modules.

Unfortunately, there doesn't seem to be any migration path, so you will have to use the endorsed API concept for now, even if it's deprecated.

Here is what NetBeans generates (slightly modified):

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    ...
</properties>

 <dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
...
</dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        ...
        </plugins>
        ...
    </build>

Of course your application server also needs to specify the endorsed dir, but this should usually be configured out-of-the-box for most application servers.

 [34,14] cannot find symbol [ERROR] symbol : method lookup() [ERROR] location: @interface javax.annotation.Resource [ERROR] -> [Help 1] 

That said, Resource#lookup() is supported by the JDK version since Java SE 7, so I guess you're using a smaller JDK version.

Please note that if you're using the JDK/ JRE version from Oracle, Java SE 8 is the only version which is still supported. All other version reached End of Life and you'll need support contracts with Oracle if you want bug/ security updates.

If this is the case I would strongly suggest to upgrade to Java SE 8 if possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM