Maven: mixing Java and Scala in one project

你。 提交于 2019-11-27 17:39:08

问题


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.


来源:https://stackoverflow.com/questions/33836905/maven-mixing-java-and-scala-in-one-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!