gpt4 book ai didi

java - 使用原型(prototype) : why is javaee-endorsed-api. jar 的新 Maven 项目被复制到 POM 中?

转载 作者:太空狗 更新时间:2023-10-29 22:40:05 24 4
gpt4 key购买 nike

我使用 Maven 原型(prototype) (webapp-javaee6) 创建了一个新的 Java EE 6 项目,但不明白为什么某些东西会放在 build 中POM 的元素。具体来说,我不明白为什么将 javaee-endorsed-api.jar 复制到 endorsed 目录。根据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>

最佳答案

[发现问题MARCHETYPES-35通过查看 source of the webapp-javaee6 archetype ]

背景
来自 JSR 250 的 javax.annotation 包:Common Annotations 不仅存在 in Java EE还有in the JDK .

使用的版本
JDK 6:通用注解 1.0
Java EE 6:通用注解 1.1
JDK 7:通用注解 1.1
Java EE 7:通用注解 1.2

问题
编译 Java EE 项目时,来自 JDK 的注释优先于来自 javaee-web-api jar 的注释。当来自 javaee-web-api 的注解定义了一个新元素时,编译器可能看不到它并因错误而失败。

例如
当 Java EE 6 项目使用 @Resource(lookup = "...") 时并且是用 JDK 6 编译的,它通常会失败。

通用注释 1.1 introduces the new element Resource.lookup() .但通常编译器只会看到来自 JDK 6 的 Resource 注释,它使用 Common Annotations 1.0 without this element .

解决方案
是使用 <endorseddirs>如您所描述的编译器参数。强制编译器使用正确版本的注释。

Java EE 7
据我了解 changelog for Common Annotations 1.2对于 Java EE 7,注释中没有新元素。所以在实践中,Java EE 7 和 JDK 7 可能没有这样的问题。

关于java - 使用原型(prototype) : why is javaee-endorsed-api. jar 的新 Maven 项目被复制到 POM 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9280217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com