Gradle doesn't execute scalatests - scala

I have created scalatest for my spark project and when I am running gradle task "test" then it doesn't execute a single test. It shows zero tests.
Even as part of the build process, gradle doesn't execute any test case. It seems that gradle is not able to discover any test.
Build.gradle:
============
plugins {
id "maven-publish"
id "scala"
id 'java'
id 'jacoco'
}
scala {
zincVersion = "1.2.1"
}
repositories {
mavenLocal()
jcenter()
mavenCentral()
maven {
url 'http://conjars.org/repo'
}
}
dependencies {
compile "org.scala-lang:scala-library:"+scalaVersion
compile "org.scala-lang:scala-reflect:"+scalaVersion
compile "org.scala-lang:scala-compiler:"+scalaVersion
compile('org.apache.spark:spark-core_2.12:2.4.6')
compile('org.apache.spark:spark-sql_2.12:2.4.6')
compile('com.typesafe.scala-logging:scala-logging-slf4j_2.11:2.1.2')
compile('ch.hsr:geohash:1.3.0')
compile('joda-time:joda-time:2.3')
compile('org.json4s:json4s-jackson_2.11:3.7.0-M4')
compile('org.apache.httpcomponents:httpclient:4.5.6')
compile('com.fasterxml.jackson.datatype:jackson-datatype-joda:2.5.1')
compile 'com.typesafe:config:1.4.1'
implementation 'com.google.guava:guava:29.0-jre'
testCompile 'junit:junit:4.12'
testCompile 'org.scalatest:scalatest_2.12:3.2.0'
testCompile ("org.mockito:mockito-scala_2.12:1.16.42")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.7.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
testCompile group: 'com.holdenkarau', name: 'spark-testing-base_2.12', version: '2.4.5_1.0.0'
compile 'org.scala-lang.modules:scala-xml_2.12:1.2.0'
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

Related

How to call a scala method from build.gradle file

I have just started working on the scala and gradle. I want to know how we can call any scala method from the gradle build file. Can somebody please help me?
From comment: I want to run multiple files present in a directory. So in order to get all the files in the directory, I have written a method in scala. That method I am trying to call from build.gradle file
Gradle allows to specify dependencies of build script itself inside buildscript{ dependencies { ... } } (not to be confused with project dependencies inside ordinary dependencies { ... }).
For example here I added Shapeless dependency and used an HList in build.gradle
build.gradle
buildscript{
dependencies {
classpath group: 'com.chuusai', name: 'shapeless_2.13', version: '2.4.0-M1'
}
}
plugins {
id 'java'
id 'scala'
id 'application'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// compile group: ...
}
application {
mainClassName = 'App'
}
import shapeless.*
task hello {
doLast {
HList l = new $colon$colon(1, new $colon$colon("a", new HNil$()))
println l
}
}
Terminal
$ ./gradlew hello
Output
> Task :hello
1 :: a :: HNil
BUILD SUCCESSFUL in 457ms
1 actionable task: 1 executed
So you can compile your Scala sources, package classes into jar, publish it locally and specify your local dependency in
buildscript{
dependencies {
classpath group: 'yourOrganization', name: 'yourArtifact', version: 'yourVersion'
}
}
and then call methods from your Scala code in build.gradle.

How to add generated sources for scala protobuf plugin using gradle?

I use gradle for building and want to generate some scala code for protobuf using this plugin: https://github.com/CharlesAHunt/scalapb-gradle-plugin
My gradle.build:
group 'xxxx'
version '1.0-SNAPSHOT'
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.charlesahunt:scalapb-plugin:1.2.4"
}
}
repositories {
mavenCentral()
}
apply plugin: 'com.charlesahunt.scalapb'
apply plugin: 'java'
apply plugin: 'scala'
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
implementation 'org.scala-lang:scala-library:2.12.8'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
compile group: 'com.typesafe', name: 'config', version: '1.3.4'
testCompile group: 'org.scalatest', name: 'scalatest_2.12', version: '3.0.8'
}
scalapbConfig {
protocVersion = "-v360"
grpc = false
targetDir = "build/generated/scala"
projectProtoSourceDir = "src/main/protobuf"
extractedIncludeDir = "build/external_protos"
}
sourceSets {
generated {
scala.srcDirs += "build/generated/"
}
}
compileScala {
dependsOn scalapb
}
The problem is that generated code isn't visible for my code and i can't use it. I tried to change targetDir and scala.srcDirs but it didn't help.
How can i solve this problem?
p.s. i use IntelliJ

gradle scalatest stay at "Discovery starting."

I have project that uses gradle, scala, scalatest and the gradle-scalatest-plugin.
I have several tests and they are compiled. But when the run part of them is executed, the gradle is stuck on "Dicovery starting." infinitely.
So I used gradle --debug test to see what is happening. But besides the status information about memory of jvm and lock acquiring, there is no information why it is stuck.
build.gradle File:
buildscript {
ext.scala_version = "2.12"
ext.akka_version = "2.5"
ext.monocle_version = "1.5.0"
ext.circe_version = "0.8+"
repositories {
mavenCentral()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.diffplug.gradle:goomph:3.9.0"
}
}
plugins {
id "java"
id "scala"
id "com.github.maiflai.scalatest" version "0.19"
id "com.athaydes.osgi-run" version "1.5.4"
id "org.dm.bundle" version "0.10.0"
// For interoperability with Other Company Eclipse P2 Repository
id "com.diffplug.gradle.p2.asmaven" version "3.9.0"
}
group "project.scalatest"
version 0.1
repositories {
mavenCentral()
jcenter()
}
sourceSets {
test {
java {
srcDirs = ['test/main/java']
}
scala {
srcDirs = ['test/main/scala']
}
}
}
runOsgi {
bundles += project
}
bundle {
instruction '-dsannotations', '*'
}
test {
}
// Adding the Other Company Eclipse P2 project Core Repo as dependency
p2AsMaven {
group 'project', {
repo 'https://other-company.de/p2/'
iu 'de.other-company.project.core'
}
}
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar', '*.java'])
compile "org.osgi:org.osgi.core:6.0+"
compile "org.osgi:org.osgi.service.log:1.3+"
compile "org.osgi:org.osgi.service.component:1.3+"
compile "org.osgi:org.osgi.service.component.annotations:1.3+"
compile "project:de.other-company.project.core:+"
// Include the program repository code as dependency
//compile fileTree(dir: "../repository")
compile "org.scala-lang:scala-library:$scala_version+"
compile "com.typesafe.akka:akka-osgi_$scala_version:$akka_version+"
compile "io.circe:circe-core_$scala_version:$circe_version"
compile "io.circe:circe-generic_$scala_version:$circe_version"
compile "io.circe:circe-parser_$scala_version:$circe_version"
compile "io.circe:circe-optics_$scala_version:$circe_version"
compile "com.github.julien-truffaut:monocle-core_$scala_version:$monocle_version"
compile "com.github.julien-truffaut:monocle-macro_$scala_version:$monocle_version"
compile "org.slf4j:slf4j-api:1.7.+"
compile "org.slf4j:slf4j-simple:1.7.+"
compile "org.slf4j:osgi-over-slf4j:1.7.+"
testCompile "com.typesafe.akka:akka-testkit_$scala_version:$akka_version+"
testCompile "org.scalatest:scalatest_$scala_version:3.2+"
testCompile "org.scalactic:scalactic_$scala_version:3.2+"
testCompile "org.scalacheck:scalacheck_$scala_version:1.13+"
testCompile "org.mockito:mockito-core:2.+"
testRuntime "org.pegdown:pegdown:1.4+"
osgiRuntime "org.apache.felix:org.apache.felix.configadmin:1.8+"
osgiRuntime "org.apache.felix:org.apache.felix.scr:2.0+"
osgiRuntime "org.apache.felix:org.apache.felix.log:1.0+"
}
So I found the solution myself :)
I did some crappy solution to find and use files in the test.
After a research how to do it properly getClass.getResource("...") the problem went away.

How to add dependencies to libgdx project?

I created a Libgdx project without the freetype font extension, and later realized that i needed it.
I added the dependencies to the build.gradle file and resfreshed the gradle build (in eclipse, for each project, right click->Gradle->Refresh Gradle Project) but still can't use the library. When trying to import something, i get "The import com.badlogic.gdx.graphics.g2d.freetype cannot be resolved".
What am i doing wrong or missing?
Here's my build.gradle file:
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = "X"
gdxVersion = '1.9.0'
roboVMVersion = '1.12.0'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.7.0'
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
}
}
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
}
}
tasks.eclipse.doLast {
delete ".project"
}
Thanks!
You need to refresh Gradle dependencies
Right click on your project -> Gradle -> Refresh All

New sourceSet by gradle cannot be seen in Eclipse

I have created additional source set called "integration-test" in my gradle project. Ewerything works fine, but eclipse cannot see dependency classes defined exactly for this source set.
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
configurations {
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
integrationTestCompile 'org.springframework:spring-test:4.1.7.RELEASE'
compile 'org.springframework:spring-context:4.1.7.RELEASE'
compile 'org.springframework:spring-core:4.1.7.RELEASE'
}
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
outputs.upToDateWhen { false }
}
check.dependsOn integrationTest
integrationTest.mustRunAfter test
version = '1.0'
}
When i build this project by command "build gradle", project is build, the only problem is with eclipse. If I change dependency 'org.springframework:spring-test:4.1.7.RELEASE' from "integrationTestCompile" to "testCompile", problem is gone.
It is a little late to answer your question, but I just found a solution to this, since I had the exact same problem.
Adding this:
eclipse {
classpath {
plusConfigurations.add configurations.integrationTestCompile
plusConfigurations.add configurations.integrationTestRuntime
}
}
to the gradle file solved the problem. I hope it does the same for you.
An approach that I found that worked really well for me is this test sets plugin: https://plugins.gradle.org/plugin/org.unbroken-dome.test-sets
It made it really easy to add integration tests to my module. And it works with the eclipse plugin automatically.