Pralay Ghosh
Pralay Ghosh

Reputation: 15

Gradle Error while building a scala code

I am facing one issue with gradle while building scala code.

$gradle makeJar

Error : org.gradle.api.tasks.TaskExecutionException :

Execution failed for task ':compileScala'.

Caused by : java.lang.NoClassDefFoundError: scala/Function1

$gradle -v

Gradle version - 1.6

groovy - 1.8.6

Ant - 1.8.4

ivy - 2.2.0

jvm - 1.7.0_55

OS - Linux 2.6x

My build.gradle file is below -

sourceCompatibility = '1.6'
apply plugin: 'scala'
def mypath = 'file://'+new File('test/lib').absolutePath
repositories {
flatDir dirs:"${mypath}"
}
configurations{
 scalaPackage
}
sourceSets{
 main{
  scala{
    srcDirs = ['test/src/scala']
  }
 }
}
dependencies {
  compile fileTree(dir: mypath, includes: ['*.jar'])
  }
  task sourcePath{
    sourceSets.main.scala.srcDirs = sourceSets.main.scala.srcDirs
    sourceSets.main.java.srcDirs = []
  }
  task makeJar(type: Jar, dependsOn: compileScala){
    archivename = "mytest.jar"
    destinationDir = file("test/oplib")
    from "build/classes"
    classpath = configurations.scalaPackage
  }
  compileScala.dependsOn sourcePath

==========================================================

Here, my scala source code is present in - ./test/src/scala/test.scala

scala jar files present in - ./test/lib

expected output location - ./test/oplib

Is there anything wrong with build.gradle file which might be resulting in this error. Kindly suggest.

Many Thanks, Pralay

Upvotes: 0

Views: 4321

Answers (3)

Robert Halter
Robert Halter

Reputation: 382

If you use spark-core_2.11 Version 1.2.0 you can define your dependencies as following:

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.apache.spark:spark-core_2.11:1.2.0'
}

This dependency has Scala

org.scala-lang / scala-library / from 2.11.2 to 2.11.7

as a indirect dependency see

Maven Repository Search for spark-core

Upvotes: 1

Robert Halter
Robert Halter

Reputation: 382

Scala projects need to declare a scala-library dependency.
Add this in your build.gralde File:

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.scala-lang:scala-library:2.11.1'
}

Upvotes: 0

Robert Halter
Robert Halter

Reputation: 382

i take your build.gradle File and start it with a Gradle Wrapper with Gradle Version 2.3

first just with gradlew

Then i make 3 Corrections:

apply plugin: 'scala'
sourceCompatibility = '1.6'
def mypath = 'file://'+new File('test/lib').absolutePath
repositories {
flatDir dirs:"${mypath}"
}
configurations{
 scalaPackage
}
sourceSets{
 main{
  scala{
    srcDirs = ['test/src/scala']
  }
 }
}
dependencies {
  compile fileTree(dir: mypath, includes: ['*.jar'])
  }
  task sourcePath{
    sourceSets.main.scala.srcDirs = sourceSets.main.scala.srcDirs
    sourceSets.main.java.srcDirs = []
  }
  task makeJar(type: Jar, dependsOn: compileScala){
    archiveName = "mytest.jar"
    destinationDir = file("test/oplib")
    from "build/classes"
    // classpath = configurations.scalaPackage
  }
  compileScala.dependsOn sourcePath
  1. Move the Line sourceCompatibility after the Scala Plugin Import
  2. Write archiveName instaed of archivename
  3. Comment out classpath

Then i run gradlew tasks and gradlew makeJar without Error.

Upvotes: 0

Related Questions