Keith
Keith

Reputation: 669

scala-maven-plugin mixed compile does not include src/main/java and can not find java class

pom.xml:

  <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>

Code structure:

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

Issues : when ever i run the maven compile via the intellij, it always show below errors, which means it can not find the Hello.class .

Questions:

  1. why this pom.xml does not work ? I checked the doc of scal-maven-plugin, the layout should work, but it did not .

  2. I found it will work if i add the src/main/java as source directory via the build-helper-maven-plugin. This may explain the first question, but i realized that before the maven compile, i run the App.scala via the Intellij , so the Hello.java has already been compiled to class and i could see it under the src/main/target/classes . So Why the scala-maven-plugin can not find the class under src/main/target/classes ?

Upvotes: 0

Views: 1191

Answers (1)

David Bernard
David Bernard

Reputation: 1640

  • like in the documentation/sample linked in the question, remove <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory>
  • or set them to src/.../java (the default values), no need to use the build-helper-maven-plugin
  • or place *.java and *.scala under the same directory (my favorite)

In dual mixed java/scala (scala depends on java and java depends of scala), the scala compiler run against java source, not from binary.

If you want to "notify" the IDE that scala source are under src/.../scala "add" <goal>add-source</goal>

see add-source

Upvotes: 1

Related Questions