Overwriting test task in gradle - scala

I want to overwrite test task in gradle (I am using Gradle 4.3 version at this moment) to change the behavior of this task.
Specifically, I am using scoverage gradle plugin in a Scala project and I want to execute $ gradle test to call test task and testScoverage task, both at the same time.
I attached task test(overwrite: true) << { testScoverage } statement to the last of build.gradle file but I always get the same message:
gradle test
> Configure project :
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
at build_clmw9wbi7768vkj4j7g7sy8v2.run(C:\Users\sergio_rodriguez\Repositorios\my-autodevops-poc\build.gradle:52)
(Run with --stacktrace to get the full stack trace of this deprecation warning.)
I pretend to generate coverage report in a single statement to be able to use Auto Devops Gitlab.
How can I do this?
My build.gradle file is as given below:
group 'org.microservices.architecture'
version '1.0-SNAPSHOT'
apply plugin: 'distribution'
apply plugin: 'scala'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.scoverage:gradle-scoverage:2.1.0'
}
}
apply plugin: "org.scoverage"
repositories {
mavenCentral()
}
ext {
scalaVersion = '2.12'
akkaVersion = '2.5.4'
akkaHttVersion = '10.0.9'
}
dependencies {
compile 'org.scala-lang:scala-library:' + scalaVersion + '.1'
compile 'com.typesafe.akka:akka-actor_' + scalaVersion + ':' + akkaVersion
compile 'com.typesafe.akka:akka-stream_' + scalaVersion + ':' + akkaVersion
compile 'com.typesafe.akka:akka-http_' + scalaVersion + ':' + akkaHttVersion
compile 'com.typesafe.akka:akka-http-spray-json_' + scalaVersion + ':' + akkaHttVersion
compile 'ch.qos.logback:logback-classic:1.1.11'
compile 'com.typesafe.akka:akka-slf4j_' + scalaVersion + ':' + akkaVersion
compile 'com.typesafe.akka:akka-http-testkit_' + scalaVersion + ':' + akkaHttVersion
scoverage 'org.scoverage:scalac-scoverage-plugin_2.12:1.3.1', 'org.scoverage:scalac-scoverage-runtime_2.12:1.3.1'
testCompile 'junit:junit:4.12'
testCompile 'org.scalatest:scalatest_' + scalaVersion + ':3.0.1'
testCompile 'com.typesafe.akka:akka-http-testkit_' + scalaVersion + ':' + akkaHttVersion
}
sourceSets.main.scala.srcDir 'src/main/scala'
sourceSets.test.scala.srcDir 'src/test/scala'
task wrapper(type: Wrapper) {
gradleVersion = '4.3'
}
task stage(dependsOn: ['installDist'])
task test(overwrite: true) << { testScoverage }

Related

Gradle doesn't execute scalatests

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"
}
}

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.

Gradle execute Groovy script in GroovyShell with Eclipse Luna

I am getting a ClassNotFoundException: org.apache.ivy.core.report.ResolveReport when executing a gradle task, which uses Grapes to resolve dependencies.
I am using Eclipse Luna 4.4.0 with a Gradle/Groovy Project having this build.gradle:
apply plugin: 'groovy'
apply plugin:'application'
mainClassName = "de.my.app.package.Main"
version = 0.5
repositories { mavenCentral() }
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.3'
compile group: 'org.apache.ivy', name:'ivy', version:'2.2.0'
compile 'commons-io:commons-io:2.4'
compile 'commons-codec:commons-codec:1.2'
}
task myTask << {
def groovyShell = new GroovyShell()
groovyShell.run(file('/src/scripts/groovy/de/my/app/package/scripts/SomeScript.groovy'))
}
classes.finalizedBy(myTask)
My Java Build Path inside Project->Properties looks like this:
This is SomeScript.groovy inside the Folder /src/scripts/groovy/de/my/app/package/scripts:
package de.my.app.package.scripts
#Grapes(
#Grab(group='org.eclipse.birt.runtime.3_7_1', module='org.apache.commons.codec', version='1.3.0')
)
#Grapes(
#Grab(group='commons-io', module='commons-io', version='2.4')
)
import org.apache.commons.codec.binary.Hex
println Hex.toString()
Weird thing is that executing SomeScript.groovy from cmd with groovy SomeScript.groovy does not give the error. So i am guessing it is some Eclipse config I have missed.
How can SomeScript.groovy be executed by the Gradle run from the build.gradle without causing a ClassNotFoundException: org.apache.ivy.core.report.ResolveReport?
I have found a solution for my problem. I needed this build.gradle file:
apply plugin: 'groovy'
apply plugin:'application'
mainClassName = "de.my.app.package.Main"
version = 0.5
repositories { mavenCentral() }
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.3'
compile group: 'org.apache.ivy', name:'ivy', version:'2.2.0'
compile 'commons-io:commons-io:2.4'
compile 'commons-codec:commons-codec:1.2'
}
task myTask (type: Exec) {
def groovyHome = System.getenv("GROOVY_HOME")
def someScriptPath= new String(project.projectDir.toString()).toString() + "\\src\\scripts\\groovy\\de\\my\\app\\package\\main\\SomeScript.groovy"
commandLine "${groovyHome}\\bin\\groovy.bat", someScriptPath
}
classes.finalizedBy(myTask)
So I abandoned the approach of using the class GroovyShell, because I could not configure the classPath for it correctly.
My Script executes now before every run. Problem is solved.

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.

eclipse build.gradle transitive dependencies

I am new to gradle, and have written the following configuration and dependency. When I compile using eclipse, it gives me errors because it is not importing the jars mentioned in "acceptanceTestCompile".
Am I missing anything? Any help appreciated. Don't want to move to NetBeans (which is picking up the dependencies without issue :))
sourceSets {
acceptanceTest {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
}
}
configurations {
acceptanceTestCompile.extendsFrom testCompile
acceptanceTestRuntime.extendsFrom testRuntime
}
dependencies {
acceptanceTestCompile 'org.gebish:geb-spock:0.10.0',
'org.spockframework:spock-core:0.7-groovy-2.0',
'org.seleniumhq.selenium:selenium-java:2.45.0',
'org.seleniumhq.selenium:selenium-firefox-driver:2.43.1',
'org.seleniumhq.selenium:selenium-ie-driver:2.43.1',
'org.seleniumhq.selenium:selenium-support:2.43.1',
'org.jfairy:jfairy:0.3.0'
}
Thank you
Karthik