【问题标题】:Maven compile error for Junit and SeleniumJunit 和 Selenium 的 Maven 编译错误
【发布时间】:2014-05-23 13:36:54
【问题描述】:

为了与 m2e 错误/问题保持一致,我遵循了提到的解决方案 @SL4j m2e fails to load in Eclipse IDE

我的 Pom.xml 看起来像这样(对于我的 JUnit 和 Selenium 依赖项):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TestJUnit</groupId>
  <artifactId>TestJUnit</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <type>jar</type>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>2.41.0</version>
  <type>jar</type>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.10-FINAL</version>
</dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>2.41.0</version>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

但是,当我执行mvn compile 时,会出现以下错误:

[ERROR] /path:/to/file/<foldername>/<JUnitClassName>.java:[9,17] package org.junit does
 not exist

我是 maven 新手,我在这里做错了什么吗?

【问题讨论】:

  • 你能在eclipse中打开调试器的情况下运行命令吗?看起来junit jar没有正确下载或junit版本在m2中崩溃。你可以谷歌如何在eclipse中调试选项
  • 您是否在位于“main”包中的类中使用 JUnit? stackoverflow.com/questions/12403497/…
  • @Trichoko 感谢您的指出。问题是因为我使用了 test。当我根据下面提供的解决方案解决了这个问题时,我可以解决这个问题。

标签: java eclipse maven selenium junit


【解决方案1】:

在您的&lt;dependencies&gt; 列表中,您指定JUnit 的范围为test,这意味着它仅在您执行mvn test 时包含在jar 中,但不包含在mvn compile 中。如果您希望 JUnit 包含在您的最终 jar 中,请将 &lt;scope&gt;test&lt;/scope&gt; 更改为 &lt;scope&gt;compile&lt;/scope&gt; 或者您可以移除尖齿,它们都做同样的事情。

TL;DR 使用mvn test 测试您的应用程序;或在 junit 依赖声明中将 &lt;scope&gt;test&lt;/scope&gt; 更改为 &lt;scope&gt;compile&lt;/scope&gt;

当我第一次开始使用 maven 时,我做了同样的事情,几乎每个人都这样做:P

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 2012-10-28
    • 2015-08-30
    • 1970-01-01
    • 2014-09-24
    • 2015-11-25
    • 2012-01-29
    • 2013-02-19
    相关资源
    最近更新 更多