2016-04-28 33 views
2

我在這裏以下教程: http://scala-tools.org/mvnsites/maven-scala-plugin/example_java.html有使用Scala的java文件在同一項目中無法看到Scala類從Main.java在Java的斯卡拉Maven項目

然而MVN編譯失敗,出現錯誤:不能找到符號
顯然抱怨只有scala類被引用的地方。

這裏是我的項目結構:

. 
├── pom.xml 
├── src 
│   ├── main 
│   │   ├── java 
│   │   │   └── com 
│   │   │    └── dummy 
│   │   │     └── foo 
│   │   │      ├── Foo.java 
│   │   │      └── Main.java 
│   │   ├── resources 
│   │   └── scala 
│   │    └── com 
│   │     └── dummy 
│   │      └── hello 
│   │       └── Hello.scala 
│   └── test 
│    ├── java 
│    ├── resources 
│    └── scala 
├── tree-pre-compile 

這裏,它運行(在失敗)後再次MVN編譯:(我可以看到Hello.class是有目標/班下)

. 
├── pom.xml 
├── src 
│   ├── main 
│   │   ├── java 
│   │   │   └── com 
│   │   │    └── dummy 
│   │   │     └── foo 
│   │   │      ├── Foo.java 
│   │   │      └── Main.java 
│   │   ├── resources 
│   │   └── scala 
│   │    └── com 
│   │     └── dummy 
│   │      └── hello 
│   │       └── Hello.scala 
│   └── test 
│    ├── java 
│    ├── resources 
│    └── scala 
├── target 
│   ├── classes 
│   │   ├── Hello.class 
│   │   └── META-INF 
│   │    ├── MANIFEST.MF 
│   │    └── maven 
│   │     └── com.dummy.java-scala-poc 
│   │      └── mixed-java-scala-poc 
│   │       ├── pom.properties 
│   │       └── pom.xml 
│   ├── classes.timestamp 
│   └── test-classes 
└── tree-text 

Main.java:

package com.dummy.foo; 

public class Main { 

    public static void main(String[] args) { 
    System.out.print("mixing java and scala"); 
    Foo foo = new Foo(); 
    foo.foo(); 
    new Hello().hello(); 
    } 

} 

Foo.java:

package com.dummy.foo; 

public class Foo { 
    public void foo() { 
    System.out.print("mixing java and scala"); 
    new Hello().hello(); 
    } 
} 

Hello.scala:

class Hello { 
    def hello() { println("Hello (class)") } 
} 

object Hello { 
    def hello() { println("Hello (object)") } 
} 

的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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.dummy.java-scala-poc</groupId> 
    <artifactId>mixed-java-scala-poc</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>mixed-java-scala-poc</name> 
    <packaging>jar</packaging> 

    <dependencies> 
     <dependency> 
      <groupId>org.scala-lang</groupId> 
      <artifactId>scala-library</artifactId> 
      <version>2.11.8</version> 
     </dependency> 
    </dependencies> 
    <repositories> 
     <repository> 
      <id>scala-tools.org</id> 
      <name>Scala-tools Maven2 Repository</name> 
      <url>http://scala-tools.org/repo-releases</url> 
     </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
      <id>scala-tools.org</id> 
      <name>Scala-tools Maven2 Repository</name> 
      <url>http://scala-tools.org/repo-releases</url> 
     </pluginRepository> 
    </pluginRepositories> 

    <build> 
     <pluginManagement> 
      <plugins> 
       <plugin> 
        <groupId>org.scala-tools</groupId> 
        <artifactId>maven-scala-plugin</artifactId> 
        <version>2.9.1</version> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>2.0.2</version> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.scala-tools</groupId> 
       <artifactId>maven-scala-plugin</artifactId> 
       <executions> 
        <execution> 
         <id>scala-compile-first</id> 
         <phase>process-resources</phase> 
         <goals> 
          <goal>add-source</goal> 
          <goal>compile</goal> 
         </goals> 
        </execution>          
        <execution> 
         <id>scala-test-compile</id> 
         <phase>process-test-resources</phase> 
         <goals> 
          <goal>testCompile</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <executions> 
        <execution> 
         <phase>compile</phase> 
         <goals> 
          <goal>compile</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

IDE是JBoss的開發工作室版本:8.1.0.GA

我可以找到其他關於SO的問題有類似的問題,但爲了我的最大努力似乎沒有任何工作。

這裏是MVN編譯輸出:

[INFO] Scanning for projects... 
[INFO]  
[INFO] ------------------------------------------------------------------------ 
[INFO] Building mixed-java-scala-poc 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ mixed-java-scala-poc --- 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-scala-plugin:2.9.1:add-source (scala-compile-first) @ mixed-java-scala-poc --- 
[INFO] Add Source directory: /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/src/main/scala 
[INFO] Add Test Source directory: /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/src/test/scala 
[INFO] 
[INFO] --- maven-scala-plugin:2.9.1:compile (scala-compile-first) @ mixed-java-scala-poc --- 
[ERROR] /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/src/main/java 
[ERROR] /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/src/main/scala 
[ERROR] /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/src/test/scala 
[INFO] Compiling 3 source files to /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/target/classes 
[INFO] 
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ mixed-java-scala-poc --- 
[INFO] Compiling 2 source files to /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/target/classes 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.301s 
[INFO] Finished at: Thu Apr 28 15:25:55 EEST 2016 
[INFO] Final Memory: 12M/145M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project mixed-java-scala-poc: Compilation failure: Compilation failure: 
[ERROR] /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/src/main/java/com/dummy/foo/Foo.java:[6,8] error: cannot find symbol 
[ERROR] 
[ERROR] could not parse error message: symbol: class Hello 
[ERROR] location: class Foo 
[ERROR] /home/hatieh/git/intelligence-ws/mixed-java-scala-poc/src/main/java/com/dummy/foo/Main.java:9: error: cannot find symbol 
[ERROR] new Hello().hello(); 
[ERROR]^
[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 

回答

2

解決它,

下載並在http://scala-ide.org/docs/current-user-doc/gettingstarted/index.html這反過來(插件)向我指出問題,安裝的Scala IDE Eclipse插件通過下面的指南。 ..

我傻,我錯過了在Hello.scala和Main.java和Foo.java進口,包聲明文件現在看起來是這樣的:

Hello.scala:

package com.dummy.hello; 

class Hello { 
    def hello() { println("Hello (class)") } 
} 

object Hello { 
    def hello() { println("Hello (object)") } 
} 

Foo.java:

package com.dummy.foo; 

import com.dummy.hello.Hello; 

public class Foo { 
    public void foo() { 
    System.out.print("mixing java and scala"); 
    new Hello().hello(); 
    } 
} 

Main.java:

package com.dummy.foo; 

import com.dummy.hello.Hello; 

public class Main { 
    public static void main(String[] args) { 
    System.out.print("mixing java and scala"); 
    Foo foo = new Foo(); 
    foo.foo(); 
    new Hello().hello(); 
    } 
} 

我希望有人認爲這很有幫助。

相關問題