Maven 错误 - 包 org.junit 不存在

maven error - package org.junit does not exist

我在使用 maven 和 junit 时遇到了一些麻烦。 运行 自己的 junit 测试完美无缺,但使用 Maven mvn clean test 构建失败。我确保所有测试都在 "src/test/java" 内并且所有源都在 "src/main/java" 内,因此 junit 可以找到它们(关于其他问题,这是许多其他情况下的问题)。

那么有人知道为什么这对我不起作用吗?

错误消息被截断:

/D:/pathToProject/myProject/src/test/java/myPackage/MyFile.java:[5,17] package org.junit does not exist

Pom.xml 剪断:

<...project information.../>
  <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source />
                    <target />
                </configuration>
            </plugin>
        </plugins>
    <plugins>
    </plugins>
  </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Khmarbaise 向您指出了一个错误,您覆盖了 src 目录这一事实意味着 Maven 将尝试在 compile 阶段编译您的测试 类,但会失败,因为您的 jUnit 依赖项在 test 范围内给出,并且仅在 test phase 中可用,后者稍后出现

删除范围 'test' 或更改您的源目录位置,使其不包含您的测试 类