我有一个使用junit
实现单元测试的项目。有一个 pom.xml
片段
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
但是,该项目无法编译。 pom.xml
中的错误如下所示:
如何修复错误?
<小时/>mvn install
返回下一条错误消息
[INFO] C:\Users\loom\prj\src\main\java:-1: info: compiling
[INFO] C:\Users\loom\prj\target\generated-sources\annotations:-1: info: compiling
[INFO] Compiling 7 source files to C:\Users\loom\prj\target\classes at 1548091925026
[ERROR] error: scala.reflect.internal.MissingRequirementError: object java.lang.Object in compiler mirror not found.
[ERROR] at scala.reflect.internal.MissingRequirementError$.signal(MissingRequirementError.scala:17)
[ERROR] at scala.reflect.internal.MissingRequirementError$.notFound(MissingRequirementError.scala:18)
[INFO] at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:53)
[INFO] at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:45)
[INFO] at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:45)
[INFO] at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:66)
[INFO] at scala.reflect.internal.Mirrors$RootsBase.getClassByName(Mirrors.scala:102)
[INFO] at scala.reflect.internal.Mirrors$RootsBase.getRequiredClass(Mirrors.scala:105)
[INFO] at scala.reflect.internal.Definitions$DefinitionsClass.ObjectClass$lzycompute(Definitions.scala:257)
[INFO] at scala.reflect.internal.Definitions$DefinitionsClass.ObjectClass(Definitions.scala:257)
[INFO] at scala.reflect.internal.Definitions$DefinitionsClass.init(Definitions.scala:1394)
[INFO] at scala.tools.nsc.Global$Run.<init>(Global.scala:1215)
[INFO] at scala.tools.nsc.Driver.doCompile(Driver.scala:31)
[INFO] at scala.tools.nsc.MainClass.doCompile(Main.scala:23)
[INFO] at scala.tools.nsc.Driver.process(Driver.scala:51)
[INFO] at scala.tools.nsc.Driver.main(Driver.scala:64)
[INFO] at scala.tools.nsc.Main.main(Main.scala)
[INFO] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[INFO] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[INFO] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] at java.base/java.lang.reflect.Method.invoke(Method.java:564)
[INFO] at org_scala_tools_maven_executions.MainHelper.runMain(MainHelper.java:161)
[INFO] at org_scala_tools_maven_executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.196 s
[INFO] Finished at: 2019-01-21T20:32:06+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.scala-tools:maven-scala-plugin:2.15.2:compile (default) on project prj: wrap: org.apache.commons.exec.ExecuteException: Process exited with an error: 1(Exit value: 1) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
There是 maven install
的完整日志
Java 和 Maven 版本:
$ mvn -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T22:49:05+03:00)
Maven home: C:\ProgramData\chocolatey\lib\maven\apache-maven-3.5.3bin..
Java version: 10.0.2, vendor: Oracle Corporation
Java home: C:\Users\loom\scoop\apps\openjdk\current
Default locale: ru_RU, platform encoding: Cp1251
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
$ java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
<小时/>
插件信息如下:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDir>src/main/scala</sourceDir>
<testSourceDir>src/test/scala</testSourceDir>
</configuration>
</plugin>
</plugins>
最佳答案
根据所提供的信息,很难判断会发生什么,我将尝试提供一些可能引导您走向正确方向的要点:
Maven 有一个为默认生命周期配置的阶段概念,该生命周期在您调用 mvn install
时运行。
可以在this page底部找到完整的阶段列表。 .
junit 首先发挥作用的“测试编译”阶段(因为它需要编译必须具有 JUnit 注释的测试类)出现在列表中间的某个位置(然后是实际运行测试的“测试”阶段)
我的观点是,从附加的堆栈跟踪来看,构建在到达那里之前似乎失败了。 可能在初始编译甚至源代码生成阶段的某个地方。 最好是检查完整的堆栈跟踪,而不仅仅是部分片段,maven 总是写出当前正在运行的内容。
验证阶段(第一个阶段)显然已经通过,因此 pom.xml 中没有问题。
此外,JUnit 依赖项本身的定义(如果将其放入 <dependencies>
部分也看起来不错)。
是什么让您认为构建失败仅仅是因为 JUnit 库?
也许是 Java/Scala/Maven 版本有些冲突? 另一个可能的原因是负责 Scala 源代码编译的插件配置错误,或者在源代码生成阶段配置错误。
关于java - Maven无法获取junit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54294923/