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.


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

xflin said...

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

David Perez said...

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

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

xflin 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

rmouniak said...

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


Java Online Training

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

jefrin said...

Nice blog very impressive to read
blue prism training in chennai

jefrin said...

Best blog thanks for posting
power BI training institute in chennai

service care said...

Outstanding blog thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
samsung service centres in chennai

Priyanka said...

Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore

xflin said...

jvmimal, Glad that you find it useful. Yes, go ahead and share it.