Monday, 4 April 2016

Maven project with scala and Java sources

Some of the best tech I learned these days are scala, spark, spark-MLlib and graphx.
My work with spark forced me to learn scala even though I was very comfortable with Java8. I admit I had attempted to learn scala earlier but gave up with the prejudice that Java8 caught up with scala in terms of richness of language features. Actually now I figured out that Scala is 10x improvement over java (< 1.8). Java language with version 8 upgrade caught upto 4-5x and still missing bunch of rich features of scala language.

That is being said, i wanted to document how to setup a maven project which can build both the scala and java source code. (Do I need to say that we can call scala routines from java and viceversa?). This is very useful because we can just add the scala plugin to existing maven java projects and start to make use of scala without any extra effort.

mkdir my-project
cd my-project
mkdir -p src/main/java src/main/scala src/main/resources/ \
                src/test/java src/test/scala/ src/test/resource
emacs/edit pom.xml  # and copy paste the below content to

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>test-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <packaging>jar</packaging>

    <name>Test Project</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <scala.version>2.11.8</scala.version>
        <exec.mainClass>JavaMain</exec.mainClass>
    </properties>

    <dependencies>
      <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>${scala.version}</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
      </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>3.2.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                  <executions>
                    <execution>
                        <id>scala-compile-first</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>
</project>

Let's put a sample Scala code
emacs src/main/scala/ScalaMain.scala # paste the below conents

object ScalaMain{
     def hello(){
           println("Hello there from scala")
     }
}
Lets call scala code from java :
emacs src/main/java/JavaMain.java  # paste the below contents
public class JavaMain {
     public static void main(String[] args){
          System.out.println("Hello From Java");
     ScalaMain.hello();
     }
}
Lets Compile and run
mvn compile exec:java  # the class is already specified in pom.xml as exec.mainClass

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Test Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
.........
[INFO] 
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ test-project ---
Hello From Java
Hello there from scala
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.239s
[INFO] Finished at: Mon Apr 04 00:42:25 PDT 2016
[INFO] Final Memory: 14M/216M
[INFO] ------------------------------------------------------------------------


Summary : 
I just documented the pom.xml configuration for a maven project which can compile both scala and java sources.

No comments:

Post a Comment