Sunday, August 11, 2013

Mixed Scala and Java in Maven Project

Recently we decided to embrace Scala in our Java Maven project here in Maxta Inc. Adding Scala to an existing Java Maven project should be simple with the help of scala-maven-plugin, however there are still some details that need to be taken care of to get it done right.


Add Scala to Maven Project

Leveraging scala-maven-plugin, it is as simple as invoking both compile and testCompile goals. The followings are what I added initially in the pom.xml file.

<properties>
  <scala.version>2.10.2</scala.version>
<properties>
<dependencies>
  <dependency>
    <groupid>org.scala-lang</groupId>
    <artifactid>scala-library</artifactId>
    <version>${scala.version}</version>
  </dependency>
</dependencies>
<pluginManagement>
  <plugins>
    <plugin>
      <groupid>net.alchim31.maven</groupid>
      <artifactid>scala-maven-plugin</artifactid>
      <version>3.1.5</version>
    </plugin>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
      </configuration>
    </plugin>
    ...
  </plugins>
</pluginManagement>
<build>
  <plugins>
    <plugin>
      <groupId>net.alchim31.maven</groupId>
      <artifactId>scala-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>scala-compile</id>
          <goals>
            <goal>compile</goal>
            <goal>testCompile</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>


Note that the maven-compiler-plugin setting already existed in the original project. It's shown here to let you know that the project was using Java 7 and compiled everything to byte code version 51.

Make Scala and Java See Each Other

The above approach has a problem. In the same Maven module, Scala code can see Java code but not the other way around. That is, Java code can not call Scala. If try, you'll get a "cannot find symbol" or similar compiler error. Following scala-maven-plugin documentation, I made changes like this to pom.xml,


<build>
  <plugins>
    <plugin>
      <groupId>net.alchim31.maven</groupId>
      <artifactId>scala-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>scala-compile</id>
          <phase>process-resources</phase>
          <goals>
            <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>

With the above changes, I can call from Scala to Java, and Java to Scala. Everything seemed to be working. However there were still a few more problems when I dig deeper:

  1. Scala code were compiled to JVM 1.6 version byte code (major version 50), while Java code were happily compiled to JVM 1.7 byte code (major version 51) as instructed by Maven (see the first code snippets). Scala compiler doesn't take javac "-target" options. The scalac option "-target:jvm-1.7" needs to be provided for byte code version 51 to be generated.
  2. Java code in fact have been compiled twice, first by scala compiler, then by Java compiler. Since Scala compiler already compiles both Java and Scala code, the default maven-compiler-plugin goals should be disabled.
  3. Scala compilation runs on process-resources and process-test-resources phases. It's just too hacky and can be a trouble for future. They should run in the default "compile" and "test-compile" phases.

Scalac Takes All

With the above discoveries, I came up with the last modifications to pom.xml.
<pluginManagement>
  <plugins>
    <plugin>
      <groupid>net.alchim31.maven</groupid>
      <artifactid>scala-maven-plugin</artifactid>
      <version>3.1.5</version>
      <configuration>
        <recompileMode>incremental</recompileMode>
        <args>
          <arg>-target:jvm-1.7</arg>
        </args>
        <javacArgs>
          <javacArg>-source</javacArg><javacArg>1.7</javacArg>
          <javacArg>-target</javacArg><javacArg>1.7</javacArg>
        </javacArgs>
      </configuration>
    </plugin>
    ...
  </plugins>
</pluginManagement>
<build>
  <plugins>
    <plugin>
      <groupId>net.alchim31.maven</groupId>
      <artifactId>scala-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>scala-compile</id>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
        <execution>
          <id>scala-test-compile</id>
          <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>
The key points are,
  • The scala-maven-plugin's compile and testCompile goals should run on their respective default phases, i.e. "compile" and "test-compile".
  • The maven-compiler-plugin's "default-compile" and "default-testCompile" are disabled (by setting the phase to "none"). Note the <id>'s of the two executions need to be set exactly to the two specific values.


16 comments:

Angelo said...
This comment has been removed by the author.
Angelo said...

I think you miss plugins in pluginManagement and pluginManagement should use camel style.

Xiaofeng Lin said...

Fixed pluginManagement to use camel style. The list of plugins in pluginManagement isn't meant to be a complete list.

David PĂ©rez said...

I have also a mixed Scala/Java project and your configuration doesn't work.

I receive lots of error messages when compiling. :-(

Xiaofeng Lin said...

I've created a sample project in github. You're welcome to try it out. Just make sure you installed JDK 7 or + and the latest maven.
https://github.com/xflin/mixed-scala-java-in-maven

Robbert said...

The version is a bit outdated and doesn't support java8.

I updated your version which does support java8 : https://github.com/robbertvdzon/mixed-scala-java8-in-maven

Mary Brown said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

Unknown said...

It is a very good blog and useful for students and developer,
Thanks for sharing


Java Online Training

gkr ragini said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Best Devops online Training
Online DevOps Certification Course - Gangboard

Yogayogi said...

Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
python Online training in chennai
python Online training in bangalore
python interview question and answers

sathish said...

This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

Aliya Manasa said...

Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!
python Course in Pune
python Course institute in Chennai
python Training institute in Bangalore

priya said...

I really like your blog. You make it interesting to read and entertaining at the same time. I cant wait to read more from you.
Data Science Training in Chennai
Data Science training in kalyan nagar
Data science training in Bangalore
Data Science training in marathahalli
Data Science interview questions and answers
Data science training in jaya nagar

ragul ragul said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Best Devops Training in pune
Devops Training in Bangalore
Microsoft azure training in Bangalore
Power bi training in Chennai

jefrin adams said...

Nice blog very impressive to read
blue prism training in chennai

jefrin adams said...

Best blog thanks for posting
power BI training institute in chennai