Gradle - build scala - Java project

buildship
eclipse
plugins

(Padmanabhan Vijendran) #1

I am calling Scala object from my java code and trying to build a runnable jar using gradle. But I am not able to do that. Below is my Java code, Scala code, and build.gradle file.

scala code:

package hello

    class Hello_World {
      def main(args: Array[String])
      {
        println("Hello World");
      }
    }

Java Code

package hello;

public class MainClass {

    public static void main(String[] args)
    {
        Hello_World hw = new Hello_World();  //Calling Scala Object
        hw.main(args);
    }

}

my build.gradle file

apply plugin: "scala"
apply plugin: "eclipse"
apply plugin: "java"

sourceSets.main.scala.srcDir "src/main/scala"
sourceSets.test.scala.srcDir "src/test/scala"
sourceSets.main.java.srcDirs = []
sourceSets.test.java.srcDirs = []



    repositories {

        flatDir {
        dirs "$projectDir/lib"
    }

}



dependencies{


    compile files("lib/scala-library-2.10.5.jar")
    compile files("lib/scala-compiler-2.10.5.jar")
    compile files("lib/scala-reflect-2.10.4.jar")


}



jar {

        manifest.attributes("Main-Class": 'hello.MainClass')
        baseName = 'hello'
        from configurations.runtime.collect { zipTree(it) }
        exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
    }

I am able to run a code in eclipse and export as jar from eclipse. But When I build it using gradle, I am getting below error.

PS C:\Users\pvijendr\Documents\hello> gradle clean build :clean :compileJava UP-TO-DATE :compileScala FAILED

FAILURE: Build failed with an exception.

What went wrong: Execution failed for task ‘:compileScala’.
xsbti/CompileFailed
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED

Total time: 4.397 secs

Can anyone help me or point out where I am doing wrong ?


(Sterling Greene) #2

Here’s a complete example of mixing Java and Scala compilation:

HTH