【问题标题】:How to exclude a Maven test-scoped dependency from Eclipse (but keep it in the path for unit tests)如何从 Eclipse 中排除 Maven 测试范围的依赖项(但将其保留在单元测试的路径中)
【发布时间】:2012-02-24 07:27:29
【问题描述】:

我在让 Eclipse 支持测试范围的 Maven 依赖项时遇到了一些问题 - 它出现在构建路径上并与 eclipse 的编译/javadoc 解析混淆。

Java EE 库示例

我一直在使用 javaee-api-6.0 库来编译我的 Java EE 应用程序。

但是,出于单元测试的目的,我希望访问的不仅仅是 api - 我需要一个实现。所以我将嵌入式 glassfish 库包含在一个测试范围内,如下所示:

<repositories>
    <repository>
        <id>glassfish-extras-repository</id>
        <url>http://download.java.net/maven/glassfish/org/glassfish/extras</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
</dependencies>

在 Maven 中按预期工作

据我了解,由于 glassfish 依赖的&lt;scope&gt;test&lt;/scope&gt;,它不会包含在常规的compile 阶段。

因为这两个依赖项都将包含在test-compile 阶段,所以我确定将 glassfish 依赖项放在 javaee-api 依赖项之前,以便在编译测试类时优先使用前者而不是后者。因此,当只使用 Maven 构建时,这种配置是没有问题的。

在 Eclipse 中无法按预期工作

但是,当使用 m2e 和 Eclipse 时,glassfish 依赖项在我的构建路径中列出:

由于 glassfish 依赖项列在 java-ee-api 依赖项之前,因此 Eclipse 似乎使用了错误的库(glassfish,而不是 java-ee-api)来验证/编译/查找 javadocs。 (实际上,我不是 100% 确定编译使用了错误的库 - 这取决于 Eclipse 是否在后台使用 Maven 来执行验证代码时使用的编译,我不知道是否是 - 但是javadoc 查找肯定引用了错误的库)

问题

除了在运行单元测试时,如何阻止 Eclipse 使用 glassfish 库?我想确保我的编译/javadoc 查找发生在 api 上,而不是该 api 的特定实现。

【问题讨论】:

    标签: jakarta-ee maven dependencies m2eclipse m2e


    【解决方案1】:

    自从我第一次发布这个问题以来已经有一段时间了,但是:

    除了运行单元测试时,我如何阻止 Eclipse 使用 glassfish 库?

    你不能。 Eclipse 与每个项目一个构建路径的概念相关联,m2e/m2e-wtp 不能(或不会)克服这个限制,如以下错误所述:

    Scope of dependencies has no effect on Eclipse compilation

    2016 年 6 月 8 日更新

    随着 JEE7 的发布,javaee-api jar 文件 now contains real usable class files。这意味着它可以在测试中使用,我不需要在 pom 文件中指定glassfish-embedded-all jar 文件。

    所以现在 Eclipse 正在从正确的 jar 文件中提取源代码和 javadoc(即 javaee-api 而不是 glassfish-embedded-all)我不太关心测试范围的 glassfish-embedded-all 仍然在 Eclipse 的类路径中.

    这不是我最初提出的问题的解决方案,但它我当时遇到的根本问题的解决方案。也许它也会帮助其他人。

    【讨论】:

      【解决方案2】:

      我也一直在努力解决这个问题,终于找到了解决方案。您已经安装了 m2e 插件,但您还需要默认未安装的 m2e wtp 连接器。根据您的 m2e 版本,这会有所不同。对于最新版本,在“首选项”->“Maven”->“发现”下。点击“打开目录”。选择 M2E - WTP 连接器。

      安装后,右击你的项目,选择'Maven' -> 'Update Project Configuration...'

      您的 pom.xml 应该如下所示:

      <dependency>
          <groupId>org.glassfish.main.extras</groupId>
          <artifactId>glassfish-embedded-all</artifactId>
          <version>3.1.2.2</version>
          <scope>test</scope>
      </dependency>
      
      <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>6.0</version>
          <scope>provided</scope>
      </dependency>
      

      Eclipse 应该可以愉快地部署,单元测试应该在 eclipse 中执行,命令行上的 maven 也应该可以接受。

      希望这会有所帮助。

      【讨论】:

      • 谢谢我尝试了许多其他解决方案,这对我来说是这样,从来没有想过这个,谢谢。
      【解决方案3】:

      如果您将surefire-plugin配置为

      ,它可能会起作用
      • 排除 API 依赖项(作为项目依赖项包含在内)和
      • 包含 glasfish 依赖项(不要将其包含到项目依赖项中)

      像这样:

      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>2.12</version>
                  <configuration>
                      <classpathDependencyExcludes>
                          <classpathDependencyExclude>javax:javaee-api
                          </classpathDependencyExclude>
                      </classpathDependencyExcludes>
                  </configuration>
                  <dependencies>
                      <dependency>
                          <groupId>org.glassfish.extras</groupId>
                          <artifactId>glassfish-embedded-all</artifactId>
                          <version>3.1.1</version>
                      </dependency>
                  </dependencies>
              </plugin>
          </plugins>
      </build>
      

      查看surefire插件(执行测试的插件)站点的Configuring the Classpath部分。


      来自文档(外部排除元素名称AND内部排除元素名称带有's')

           <classpathDependencyExcludes>
              <classpathDependencyExcludes>org.apache.commons:commons-email</classpathDependencyExcludes>
            </classpathDependencyExcludes>
      

      但我想应该是(只有带有's'的外部元素名称):

           <classpathDependencyExcludes>
              <classpathDependencyExclude>org.apache.commons:commons-email</classpathDependencyExclude>
            </classpathDependencyExcludes>
      

      【讨论】:

      • 奇怪的是,从 2.20 版开始,嵌套复数 &lt;classpathDependencyExcludes&gt; 具有所需的效果,但使用 &lt;classpathDependencyExclude&gt; 却没有。
      【解决方案4】:

      我认为,如果你在构建路径中没有 lib,那么我认为从 Eclipse 方面来说,你不能将它专门用于单元测试或资源处理等。

      从实践中 - 尝试在 pom.xml 中安排依赖项,并且在大多数情况下,eclipse buildpath 中的库顺序将相同。

      【讨论】:

        猜你喜欢
        • 2016-11-12
        • 2019-11-25
        • 2012-08-16
        • 2017-05-02
        • 1970-01-01
        • 1970-01-01
        • 2013-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多