22

Today I've been trying to find a proper solution to set up a maven project that contains both Java and Scala code (with two-way dependencies between them).

The solutions I've found usually consist of invoking scala-maven-plugin or maven-scala-plugin in the process-resources phase so that it runs before the default maven compiler plugin (examples: http://www.hascode.com/2012/03/snippet-mixing-scala-java-in-a-maven-project/, https://itellity.wordpress.com/2014/08/21/mixing-scala-and-java-in-a-maven-project/, the official scala-maven-plugin page: http://davidb.github.io/scala-maven-plugin/example_java.html).

That leads to the solution that looks like this:

<build>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <recompileMode>incremental</recompileMode>
            <executions>
                <execution>
                    <id>scala-compile</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>
    </plugins>
</build>

This solutions works well - Scala compilation is invoked in the process-resources phase and it compiles both Java and Scala code so .class files are all ready when the maven compiler plugin runs in the compile phase.

The problem is that this solution doesn't look clean to me. Invoking the Scala compilation process before the compilation phase just to make sure it runs before the maven compiler plugin seems "hacky".

Scala compiler compiles Java classes anyway so I thought I could just completely turn off the default maven compiler plugin and then Scala compiler could run in the compile phase. It looks much cleaner to me although the configuration is a little bit longer:

<build>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <recompileMode>incremental</recompileMode>
            <executions>
                <execution>
                    <id>scala-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>scala-test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>default-testCompile</id>
                    <phase>none</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I'm wondering why this solution is not the one advised in blog posts or on the official plugin page. Are there any downsides to this approach? Are there any problems I should expect using the second solutions instead of the first one?

Paweł Chorążyk
  • 3,584
  • 3
  • 27
  • 33

1 Answers1

12
  • Yes, the solution is "hacky" but maven-compiler-plugin is always the first plugin to run in compilation phase (it's like hard coded into maven).
  • I didn't test with scala 2.11, but previous version of scalac doens't compile .java into .class (only parse them). and scala-maven-plugin is made to run with "every version of scala" since 2.7.
David Bernard
  • 1,560
  • 13
  • 15
  • Thanks, it's great to get an answer from the plugin author himself :-) I didn't take backward compatibility into account. It looks like Scala compiler compiles Java files since Scala 2.7.2 (http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources). I tested 'my' solution with different Scala versions and confirmed that it works well only with Scala >=2.7.2. – Paweł Chorążyk Nov 22 '15 at 22:24
  • 2
    I guess you miss read the article, scalac doesn't compile java but is able to read it. (and iirc only for class used by a .scala). Anyway if it works now, it's good to know. (I prefer the shorter solution and the ability to define java option of compilation) – David Bernard Nov 23 '15 at 16:48
  • You're right, I misread an article and also my tests were not correct (I thought it works because it didn't throw any errors, but that was because scala compiler can, as you said, parse java files and read their APIs). It looks like the solution works well only when the plugin is configured to incremental so that it uses standalone zinc compiler. Zinc seems to be able to compile both java and scala and that's why it works, at least with the newest scala versions. Thanks for your engagement and sorry for confusion :-) – Paweł Chorążyk Nov 23 '15 at 21:10