Sonntag, 30. Januar 2011

@Resource annotation and lookup

The problem...
In the last hours I unfortunately had to fight a bit with the @Resource annotation in combination with the parameter "lookup". Many examples out there mention this new added parameter in JavaEE6. And i wanted to try. So an example webapp I downloaded worked fine. But as soon as I tried to use it in my project, i kept getting a "cannot find symbol" error message. I examined further and found out that the resource annotation can be found in the latest JDK 1.6, but also inside my maven dependency "javaee-api-6.0.jar". Unfortunately NetBeans always took the wrong version.

The solution...
is quite simple, if you use maven. I switched from Eclipse to Maven some weeks ago and used it´s wizzard to create my EJB modules. The netbeans modules unfortunately handle the pom.xml a bit different than the default archetypes. If you try to create a standard maven ejb module, the following xml is being created and even documented. This one will help you out:


<profiles>
<profile>
<id>endorsed</id>
<activation>
<property>
<name>sun.boot.class.path</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<!-- javaee6 contains upgrades of APIs contained within the JDK itself.
As such these need to be placed on the bootclasspath, rather than classpath of the
compiler.
If you don't make use of these new updated API, you can delete the profile.
On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.-->
<compilerArguments>
<bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Kommentare:

  1. Thank you, Frederik!
    This solution worked for me.

    AntwortenLöschen
  2. Thanks, I spent hours going round in circles on this

    AntwortenLöschen

Hier kann jeder (auch unregistrierte) Leser seine Meinung kundtun...