2017-08-05 30 views
0

的pom.xml:斯卡拉,Maven的插件混合編譯不包括的src/main/java和無法找到java類

<build> 
<sourceDirectory>src/main/scala</sourceDirectory> 
<testSourceDirectory>src/test/scala</testSourceDirectory> 
<plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <configuration> 
      <source>${maven.compiler.source}</source> 
      <target>${maven.compiler.target}</target> 
     </configuration> 
     <executions> 
      <execution> 
       <id>default-compile</id> 
       <phase>compile</phase> 
      </execution> 
     </executions> 
    </plugin> 

    <plugin> 
    <!-- see http://davidb.github.com/scala-maven-plugin --> 
    <groupId>net.alchim31.maven</groupId> 
    <artifactId>scala-maven-plugin</artifactId> 
    <version>3.2.0</version> 
    <executions> 
     <execution> 

     <goals> 
      <goal>compile</goal> 
      <goal>testCompile</goal> 
     </goals> 
     <configuration> 
      <args> 
      <arg>-dependencyfile</arg> 
      <arg>${project.build.directory}/.scala_dependencies</arg> 
      </args> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 

代碼結構:

src/main/java 
      Hello.java 
src/main/scala #will reference the class under /src/main/java 
      App.scala 

IDE:的IntelliJ IDEA 2017.2.1,JDK:java8

發佈: 當我運行maven編譯通過intellij,它總是顯示錯誤,這意味着它找不到Hello.class。

問題:

  1. 爲什麼這個pom.xml中不起作用?我檢查了scal-maven-plugin的文檔,佈局應該可行,但它沒有。

  2. 我發現它會工作,如果我通過build-helper-maven-plugin將src/main/java添加爲源目錄。這可以解釋第一個問題,但是我意識到在maven編譯之前,我通過Intellij運行App.scala,所以Hello.java已經編譯到類中,我可以在src/main/target /類。那麼爲什麼scala-maven-plugin在src/main/target/classes下找不到這個類?

回答

1
  • 像問題鏈接的文檔/樣品中除去 <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory>
  • 或將它們設置爲SRC /.../的java(默認值),沒有必要使用的建設 - 幫手 - Maven的插件
  • 或地區*的.java和*同一個目錄下.scala(我的最愛)

在雙混合的java /斯卡拉(Scala的依賴於Java和Java取決於階的)時,針對scala編譯器運行java源碼,而不是二進制。

如果你想 「通知」 中的IDE,Scala的來源是在src /.../斯卡拉 「添加」 <goal>add-source</goal>

看到add-source

+0

謝謝!它解決了我的擔憂。 – Keith

相關問題