使用原型的新Maven项目:为什么要在POM中复制javaee-endorsed-api.jar?

kin*_*nti 17 java maven

我已经使用Maven archetype(webapp-javaee6)来创建一个新的Java EE 6项目,但是不明白为什么某些东西放在buildPOM 的元素中.具体来说,我不明白为什么将javaee-endorsed-api.jar其复制到背书目录.根据这个问题的答案,这是编译所需要的,但是当我删除相关plugin元素时,我的项目编译得很好build.

既然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>
Run Code Online (Sandbox Code Playgroud)

Are*_*rff 15

[ 通过查看webapp-javaee6原型来源发现MARCHETYPES-35 ]

背景
JSR 250:Common Annotations中的包javax.annotation不仅存在于Java EE中,还存在于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编译时,它通常会失败.

Common Annotations 1.1 引入了新元素 Resource.lookup().但通常编译器只会看到JDK 6中的Resource注释,它使用没有此元素的 Common Annotations 1.0 .

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

JAVA EE 7
正如我理解Java EE 7的Common Annotations 1.2更改日志一样,注释上没有新的元素.所以在实践中,Java EE 7和JDK 7可能没有这样的问题.


Seb*_*iec 5

它应该编译,因为还存在对此工件的依赖:

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

Maven的手册页描述提供如下:

这很像compile,但表示您希望JDK或容器在运行时提供依赖性.例如,在为Java Enterprise Edition构建Web应用程序时,您可以将Servlet API和相关Java EE API的依赖关系设置为提供的范围,因为Web容器提供了这些类.此范围仅在编译和测试类路径中可用,并且不可传递.

因此,在我看来,复制此依赖项对编译没有影响.

但是,archetype的作者出于某种原因希望将Java EE 6 API包复制到endorsed目录.如果您决定启动Jetty服务器并在"测试阶段"(例如使用JUnit)中进行一些测试,这可能会有所帮助.

如果您没有使用它 - 只需将其删除即可.

  • 恕我直言,这个答案没有解决主要问题:“为什么 javaee-endorsed-api.jar 被复制到认可目录”。 (2认同)