Gradle: Setting up Scala project with Apache Spark in Eclipse - eclipse

I am not able to setup a Scala project with Apache Spark dependency in Eclipse. Using a Scala IDE plugin and Gradle plugins in Eclipse. build.gradle project looks like this:
apply plugin: 'scala'
apply plugin: 'eclipse'
repositories{
mavenCentral()
mavenLocal()
}
dependencies{
compile 'org.slf4j:slf4j-api:1.7.5'
compile "org.scala-lang:scala-library:2.11.2"
compile 'com.sparkjava:spark-core:2.3'
testCompile "junit:junit:4.11"
}
task run(type: JavaExec, dependsOn: classes) {
main = 'Main'
classpath sourceSets.main.runtimeClasspath
classpath configurations.runtime
}
Under the Referenced Libraries I can see spark-core-2.3.jar. But I can't import any Spark library into Scala class.
I did try running gradle eclipse command but no luck.

You're referencing the wrong dependency - instead of com.sparkjava:spark-core:2.3 (which belongs to another project, Spark web framework), you should include:
compile 'org.apache.spark:spark-core_2.11:2.0.1'
This uses latest stable version (2.0.1).

Related

How do I run a scala application using gradle?

I added the scala plugin to gradle, but i don't how to run it. There's no run task when i create a scala project.
How do I run the scala project?
My gradle build script:
apply plugin: 'idea'
apply plugin: 'scala'
repositories {
mavenLocal()
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
mavenCentral()
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.13.1'
}
You need to provide the classpath as well. Change run task declaration to:
task run(type: JavaExec, dependsOn: classes) {
main = 'Demo'
classpath = sourceSets.main.runtimeClasspath
}
And it will work fine. Demo.

Eclipse is not using gradle dependencies jar during run or debug

I am trying to do basic hibernate task using Gradle project.
Dependency jars are download by Gradle and placed in Project and External Dependencies library.
I don't get any compile time error. But when I try to run or debug the main class in Eclipse, I am getting class not found NoClassDefFoundError.
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
When I checked the build path, I could see the dependency library is configured with required jars but still eclipse is not using it.
But when I add the jar manually in the build path, I am not getting this exception.
Build.gradle File
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
apply plugin: "eclipse"
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:23.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-core
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.6.Final'
// https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc6
runtime group: 'com.oracle.jdbc', name: 'ojdbc6', version: '11.1.0.6.0'
// https://mvnrepository.com/artifact/com.oracle/ojdbc6
runtime group: 'com.oracle', name: 'ojdbc6', version: '11.2.0.4.0-atlassian-hosted'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
Thanks in advance!!!
I think you should update the classpath with the latest changes in the build file. Eclipse does not do that automatically in all versions.
Go to package explorer, right-click the build.gradle file, then from the context menu select gradle->refresh gradle project.
You can also enable auto sync from the preferences menus, go to gradle, and check the "Automatic Project Synchronization" checkbox.
Remove/comment this dependency and try.
runtime group: 'com.oracle.jdbc', name: 'ojdbc6', version: '11.1.0.6.0'
My Build.gradle:
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:23.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-core
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.6.Final'
// https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc6
// runtime group: 'com.oracle.jdbc', name: 'ojdbc6', version: '11.1.0.6.0'
// https://mvnrepository.com/artifact/com.oracle/ojdbc6
runtime group: 'com.oracle', name: 'ojdbc6', version: '11.2.0.4.0-atlassian-hosted'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
With the eclipse plugin, there are different build tasks being added:
./gradlew cleanEclipse eclipse

IntelliJ "Cannot infer Scala class path..." but Gradle testCompile is correct?

I've got a Spring Boot project building through Gradle that recently saw the addition of some Gatling tests. The Gatlings stuff, which needs Scala support, is all down in src/test/scala. The build.gradle file got a new testCompile dependency to support it and, from a gradle perspective, all is well...
build.gradle
apply plugin: 'scala'
...
dependencies {
...
testCompile "org.scala-lang:scala-library:2.11.1"
testCompile "io.gatling.highcharts:gatling-charts-highcharts:2.2.5"
...
}
The gradle docs suggest that testCompile is all we need here: https://docs.gradle.org/current/userguide/scala_plugin.html
IntelliJ is unhappy with this configuration insisting
Warning:<i><b>root project 'tenderfoot': Unable to build Scala project configuration</b>
Details: org.gradle.api.GradleException: Cannot infer Scala class path because no Scala library Jar was found. Does root project 'tenderfoot' declare dependency to scala-library? Searched classpath: configuration ':compileClasspath'.</i>
If I lift the dependency up from testCompile to compile, the intellij warning goes away, but now my spring boot uber jar thing is unnecessarily bloated.
What's the way out? How do I get IntelliJ to stop Warning on this?
Is this actually an IntelliJ bug?
I ran into this problem also (having to set the dependency manually from IntelliJ).
I "fixed" it by setting the dependency as compileOnly as opposed to compile, this scope does not include the JAR in the final distribution.
The code I use is (please note that my dependency includes Scala as a transitive dependency):
compileOnly("io.gatling.highcharts:gatling-charts-highcharts:$gatlingVersion")

Using Gradle for Scala and ScalaTest (IntelliJ 2016.3.6)

Here is a quick build.gradle file I put together:
apply plugin: 'scala'
apply plugin: 'idea'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile "org.scala-lang:scala-library:2.12.2"
compile "org.scala-lang:scala-compiler:2.12.2"
testCompile 'org.scalatest:scalatest_2.11:3.0.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
If understood correctly, when running gradle idea, the external dependencies defined above appear in the External Libraries folder.
While I do see the dependencies in the folder, the issue I am facing is that I am unable to import anything from my external libraries provided by Gradle. Anything I manually provide (i.e. a downloaded version of the Scala SDK) works perfectly fine.
I have src and test marked as my sources root and test sources root, respectively.
What could possibly be the issue? Detailed explanations are also appreciated; I'm coming from a Maven background and struggling with the Gradle documentation.

Gradle Eclipse not working

My build.gradle script:
apply plugin: 'groovy'
apply plugin: 'eclipse'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
dependencies {
compile (
'org.apache.camel:camel-core:2.14.1',
'org.apache.camel:camel-mail:2.14.1'
)
}
When I run gradle eclipse I see:
:myapp:eclipseClasspath
:myapp:eclipseJdt
:myapp:eclipseProject
:myapp:eclipse
BUILD SUCCESSFUL
Total time: 3.694 secs
When I run gradle clean build I get a similar BUILD SUCCESSFUL message. But when I refresh my project in Eclipse, I don't see a Referenced Libraries folder with Camel Core or Camel Mail in it, instead under the Problems tab I see 3 problems:
Project 'myapp' is missing required library: 'D:\workspace\myapp\unresolved dependency - org.apache.camel camel-core 2.14.1'
Project 'myapp' is missing required library: 'D:\workspace\myapp\unresolved dependency - org.apache.camel camel-mail 2.14.1'
The project cannot be built until build path errors are resolved
What is going on here? On a perhaps-somewhat-related note, I am on Eclipse Juno, and going into Properties >> Java >> Compiler, I don't seem to have an option to set my Eclipse Java Compiler to 1.8, only 1.7. Perhaps my Eclipse instance is too old to handle Java 8?
If that's your full build script, you're missing repository definitions (where to go get the artifacts). Try adding:
repositories {
mavenCentral()
}
Here's the backing class for repositories {} in case you need to add a custom URL.