Maven: mixing Java and Scala in one project

2020-02-10 03:10发布

问题:

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?

回答1:

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