2016-10-03 151 views
3

我想用maven構建一個java項目。我通過mvn -v檢查Maven的命令,並讓下面的輸出爲什麼maven編譯失敗?

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T18:41:47+02:00) 
Maven home: C:\Program Files\Apache\Maven\apache-maven-3.3.9\bin\.. 
Java version: 1.8.0_101, vendor: Oracle Corporation 
Java home: C:\Program Files\Java\jdk1.8.0_101\jre 
Default locale: en_US, platform encoding: Cp1252 
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos" 

我添加org.joda.time.LocalTimeHelloWorld.java如下

package hello; 

import org.joda.time.LocalTime; 

public class HelloWorld { 
    public static void main(String[] args) { 
     LocalTime currentTime = new LocalTime(); 
     System.out.println("The current local time is: " + currentTime); 
     Greeter greeter = new Greeter(); 
     System.out.println(greeter.sayHello()); 
    } 
} 

所以我添加joda-time依賴我pom.xml。這是我pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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>org.springframework</groupId> 
    <artifactId>gs-maven</artifactId> 
    <packaging>jar</packaging> 
    <version>0.1.0</version> 

    <dependencies> 
     <!-- tag::joda[] --> 
     <dependency> 
      <groupId>joda-time</groupId> 
      <artifactId>joda-time</artifactId> 
      <version>2.2</version> 
     </dependency> 
     <!-- end::joda[] --> 
     <!-- tag::junit[] --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.12</version> 
      <scope>test</scope> 
     </dependency> 
     <!-- end::junit[] --> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-shade-plugin</artifactId> 
       <version>2.1</version> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>shade</goal> 
         </goals> 
         <configuration> 
          <transformers> 
           <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
            <mainClass>hello.HelloWorld</mainClass> 
           </transformer> 
          </transformers> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

每當我要運行它給人的輸出mvn compile作爲

[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building gs-maven 0.1.0 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gs-maven --- 
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory H:\Projects\JAVA\web\unzipped\gs-maven\initial\src\main\resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gs-maven --- 
[INFO] Changes detected - recompiling the module! 
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! 
[INFO] Compiling 2 source files to H:\Projects\JAVA\web\unzipped\gs-maven\initial\target\classes 
[INFO] ------------------------------------------------------------- 
[ERROR] COMPILATION ERROR : 
[INFO] ------------------------------------------------------------- 
[ERROR] /H:/Projects/JAVA/web/unzipped/gs-maven/initial/src/main/java/hello/HelloWorld.java:[3,21] package org.joda.time does not exist 
[ERROR] /H:/Projects/JAVA/web/unzipped/gs-maven/initial/src/main/java/hello/HelloWorld.java:[7,9] cannot find symbol 
    symbol: class LocalTime 
    location: class hello.HelloWorld 
[ERROR] /H:/Projects/JAVA/web/unzipped/gs-maven/initial/src/main/java/hello/HelloWorld.java:[7,37] cannot find symbol 
    symbol: class LocalTime 
    location: class hello.HelloWorld 
[INFO] 3 errors 
[INFO] ------------------------------------------------------------- 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.647 s 
[INFO] Finished at: 2016-10-03T09:11:07+03:00 
[INFO] Final Memory: 14M/114M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project gs-maven: Compilation failure: Compilation failure: 
[ERROR] /H:/Projects/JAVA/web/unzipped/gs-maven/initial/src/main/java/hello/HelloWorld.java:[3,21] package org.joda.time does not exist 
[ERROR] /H:/Projects/JAVA/web/unzipped/gs-maven/initial/src/main/java/hello/HelloWorld.java:[7,9] cannot find symbol 
[ERROR] symbol: class LocalTime 
[ERROR] location: class hello.HelloWorld 
[ERROR] /H:/Projects/JAVA/web/unzipped/gs-maven/initial/src/main/java/hello/HelloWorld.java:[7,37] cannot find symbol 
[ERROR] symbol: class LocalTime 
[ERROR] location: class hello.HelloWorld 
[ERROR] -> [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/MojoFailureException 

請讓我告知我在做什麼錯。

N.B:我只是跟着Building Java Projects with Maven教程作爲初學者。

+2

檢查依賴項是否被下載到用戶文件夾下的本地m2存儲庫中。確保您已連接到互聯網 – virendrao

+0

您是否使用m2eclipse作爲此 – mhasan

+1

它在我的結尾沒有給出任何異常。我只是複製這些文件並嘗試'mvn compile' –

回答

7

Maven的是閱讀的Java版本1.8.0_101,凡爲pom.xml中未更新

1)嘗試安裝所需verison。有JAVA_HOME的pom.xml更新爲相同的jdk安裝路徑

2)mvn clean install - 這樣做是清楚你有任何編譯的文件,並確保你真的編譯從頭每個模塊。