使用Maven构建项目时缺少JUNIT Jar异常



我正处于Maven的开始阶段。我创建了一个项目,并尝试使用该命令。我这边缺了什么?我正在尝试编译并运行一个TestSuite文件:

mvn clean package

POM.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test.myProject</groupId>
  <artifactId>myTest</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>egdsvTest</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

但使用后我得到了以下异常。

异常

 D:MVN_Shirish_ProjectTest>mvn clean package
    [INFO] Scanning for projects...
    [INFO]
    [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethrea
    ded.SingleThreadedBuilder with a thread count of 1
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Test 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Test ---
    [INFO] Deleting D:MVN_Shirish_ProjectTesttarget
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Test
    ---
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
    i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:MVN_Shirish_ProjectTestsrc
    mainresources
    [INFO]
    [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ Test ---
    [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
    uild is platform dependent!
    [INFO] Compiling 1 source file to D:MVN_Shirish_ProjectTesttargetclasse
    s
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ eg
    dsvTest ---
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
    i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:MVN_Shirish_ProjectTestsrc
    testresources
    [INFO]
    [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ 
    Test ---
    [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
    uild is platform dependent!
    [INFO] Compiling 1 source file to D:MVN_Shirish_ProjectTesttargettest-c
    lasses
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Test ---
    [INFO] Surefire report directory: D:MVN_Shirish_ProjectTesttargetsurefi
    re-reports
    [WARNING] Missing POM for org.apache.maven.surefire:surefire-junit3:jar:2.12.4
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.968 s
    [INFO] Finished at: 2014-10-14T12:28:09+05:30
    [INFO] Final Memory: 7M/247M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
    12.4:test (default-test) on project Test: Unable to generate classpath: org
    .apache.maven.artifact.resolver.MultipleArtifactsNotFoundException: Missing:
    [ERROR] ----------
    [ERROR] 1) org.apache.maven.surefire:surefire-junit3:jar:2.12.4
    [ERROR]
    [ERROR] Try downloading the file manually from the project website.
    [ERROR]
    [ERROR] Then, install it using the command:
    [ERROR] mvn install:install-file -DgroupId=org.apache.maven.surefire -DartifactI
    d=surefire-junit3 -Dversion=2.12.4 -Dpackaging=jar -Dfile=/path/to/file
    [ERROR]
    [ERROR] Alternatively, if you host your own repository you can deploy the file t
    here:
    [ERROR] mvn deploy:deploy-file -DgroupId=org.apache.maven.surefire -DartifactId=
    surefire-junit3 -Dversion=2.12.4 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url
    ] -DrepositoryId=[id]
    [ERROR]
    [ERROR] Path to dependency:
    [ERROR] 1) dummy:dummy:jar:1.0
    [ERROR] 2) org.apache.maven.surefire:surefire-junit3:jar:2.12.4
    [ERROR]
    [ERROR] ----------
    [ERROR] 1 required artifact is missing.
    [ERROR]
    [ERROR] for artifact:
    [ERROR] dummy:dummy:jar:1.0
    [ERROR]
    [ERROR] from the specified remote repositories:
    [ERROR] m1 (http://repo1.maven.org/maven2, releases=true, snapshots=false)
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
    ch.
    [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 rea
    d the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionE
    xception

Maven的工作原理是在桌面或安装了Maven的机器上创建一个本地存储库并运行此目标。

在maven运行目标的过程中,它首先尝试从Central repository中获取您在注释M1或M2中列出的所有依赖jar文件,然后将它们存储在C:\Users/yourname/.M2/repository文件夹(假设您使用的是windows机器)或其他操作系统中相应的用户文件夹下。

然后,它使用jar根据给定的目标运行所需的构建。

现在,在您的情况下,您必须找到连接到存储库的方法(检查位于User目录中.m2文件夹中的settings.xml),并检查您是否可以连接到internet中央maven存储库。如果没有,您可以获得jar文件,并按照错误中的建议手动安装它们。一旦你这样做了,我认为你应该克服这个问题。如果你还有问题,请告诉我。

在我的案例中,这是由于我正在进行离线构建(即mvn -o install),而我的本地Maven存储库中还没有安装那个jar。进行在线构建(mvn install)修复了它。

最新更新