我使用Maven原型(webapp-javaee6)创建了一个新的JavaEE6项目,但是不理解为什么要将某些东西放在build元素中。具体地说,我不明白为什么要把javaee-endorsed-api.jar复制到批注目录中。根据对this问题的回答,这是编译所必需的,但是当我删除build下的相关plugin元素时,我的项目编译得很好。
既然javax:javaee-web-api已经在POM中作为依赖项提供了,那么它不能用于编译吗?
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<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>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>发布于 2012-02-17 20:28:04
它应该会编译,因为还有对此工件的依赖关系:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>Maven manual page对提供的的描述如下:
这很像编译,但表示您希望JDK或容器在运行时提供依赖项。例如,在为Java企业版构建web应用程序时,您可以将对Servlet API和相关Java EE API的依赖项设置为所提供的范围,因为web容器提供了这些类。此作用域仅在编译和测试类路径上可用,并且不可传递。
所以在我看来,复制这个依赖项对编译没有影响。
然而,由于某种原因,原型的作者希望将JavaEE6API包复制到批注目录。如果您决定启动Jetty server并在“测试阶段”(例如使用JUnit)进行一些测试,这可能会很有帮助。
如果你不使用它,那就把它去掉。
发布于 2013-08-21 19:35:15
[通过查看source of the webapp-javaee6 archetype在问题MARCHETYPES-35中找到]
背景
JSR250中的包javax.annotation : Common Annotation不仅存在于in Java EE中,还存在于in the JDK中。
使用的版本
JDK 6:公共注释1.0
Java EE 6:通用批注1.1
JDK 7:公共注解1.1
Java EE 7:通用批注1.2
Problem
编译Java EE项目时,来自JDK的批注优先于来自javaee-web-api jar的批注。当javaee-web-api中的注释定义了一个新元素时,编译器可能看不到它,并出现错误。
例如
当JavaEE6项目使用@Resource(lookup = "...")并使用JDK6编译时,通常会失败。
Common Annotation 1.1 introduces the new element Resource.lookup()。但通常情况下,编译器只能看到JDK6中的资源注释,它使用Common annotation 1.0 without this element。
解决方案
就是按照您所描述的那样使用<endorseddirs>编译器参数。以强制编译器使用正确版本的注释。
JAVA EE 7
据我所知,JavaEE7的changelog for Common Annotations 1.2中没有关于注释的新元素。因此,在实践中,Java EE 7和JDK 7可能不存在这样的问题。
https://stackoverflow.com/questions/9280217
复制相似问题