Error compiling class that contains trait in groovy - java

I have a module which uses Groovy (2.5.14) and Scala (2.11.6). All the dependencies are maintained by gradle (ver 6.x). While building the project I am getting some strange compilation Error as shown below:
[Error] Class A.B.C$Trait$FieldHelper not found - continuing with a stub.
(where C is some trait)
To be precise, while build the module, the task compileTestScala fails with the above error.
Here is the snapshot of the build gradle
compileTestScala {
classpath = classpath.plus(files(compileTestGroovy.destinationDir))
dependsOn compileTestGroovy
}
compileScala {
classpath = classpath.plus(files(compileGroovy.destinationDir))
dependsOn compileGroovy
}
However, the compileScala builds successfully. What could be the root cause?

Related

Running Cucumber Scenarios in parallel using cucumber-groovy

I am trying to update a project that uses Groovy's Cucumber implementation. I am getting the following error when trying to run:
./gradlew cucumber -Denv=test -Ptags=#myTest --continue --no-daemon
Error: Could not find or load main class io.cucumber.core.cli.Main
Caused by: java.lang.ClassNotFoundException: io.cucumber.core.cli.Main
I see the relevant dependency locally
and from my IntelliJ Run Configuration, I am able to set the Main class:io.cucumber.core.cli.Main with no errors. Yet when I run from this context, it gives me undefined step definition errors, despite being able to click through to the Step Defs from the feature files.
I am using Gradle (from wrapper context) 5.0
The section that declares the jar in the cucumber task in the build.gradle is:
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
systemProperty "parallel", System.properties.getProperty("parallel", setParallel)
if (setParallel == "true") cucumberArgs.addAll(cucumberThreadsOption)
args = cucumberArgs
jvmArgs = [
"-Denv=$env",
]
}
}
My dependencies:
dependencies {
implementation "io.cucumber:cucumber-core:5.1.3"
implementation "io.cucumber:cucumber-groovy:5.1.3"
compile "org.codehaus.groovy:groovy-all:2.5.6"
}
Wonder if there is anything obvious?

Gradle compileJava Task keeps failing

alright i've been hiting my head against the wall for quite sometime now, and now i dont even know what to search for to find a solution, here are my files
build.gradle
//Applying the Gradle BND Plugin for Workspace Builds
//https://github.com/bndtools/bnd/blob/master/biz.aQute.bnd.gradle/README.md
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "biz.aQute.bnd:biz.aQute.bnd.gradle:${bnd_version}"
}
}
apply plugin: 'biz.aQute.bnd.workspace'
apply plugin: 'java'
// Repositorios, aguante Maven Central.
repositories {
mavenCentral()
/* Excluded, uso la dependecia de otro lado ahora.
flatDir {
dirs '/home/feddericokz/devTools/Equinox/Equinox-Oxygen-1a/plugins'
}
*/
}
// Dependencias
dependencies {
// https://mvnrepository.com/artifact/org.osgi/org.osgi.core
compile group: 'org.osgi', name: 'org.osgi.core', version: '6.0.0'
}
settings.gradle
/*
* This settings file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
* In a single project build this file can be empty or even removed.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/4.3.1/userguide/multi_project_builds.html
*/
/*
// To declare projects as part of a multi-project build use the 'include' method
include 'shared'
include 'api'
include 'services:webservice'
*/
rootProject.name = 'bndWorkspace'
include 'com.feddericokz.helloworld'
when trying to run gradle jar from the command line, i get an error as it the compiler cant find the osgi dependencies to compile the classes
Task :com.feddericokz.helloworld:compileJava FAILED
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:3: error: package org.osgi.framework does not exist
import org.osgi.framework.BundleActivator;
^
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:4: error: package org.osgi.framework does not exist
import org.osgi.framework.BundleContext;
^
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:6: error: cannot find symbol
public class HelloWorldActivator implements BundleActivator {
^
symbol: class BundleActivator
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:8: error: cannot find symbol
public void start(BundleContext bundleContext) throws Exception {
^
symbol: class BundleContext
location: class HelloWorldActivator
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:13: error: cannot find symbol
public void stop(BundleContext bundleContext) throws Exception {
^
symbol: class BundleContext
location: class HelloWorldActivator
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:7: error: method does not override or implement a method from a supertype
#Override
^
/home/feddericokz/testingDir/bndWorkspace/com.feddericokz.helloworld/src/com/feddericokz/helloworld/HelloWorldActivator.java:12: error: method does not override or implement a method from a supertype
#Override
^
7 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':com.feddericokz.helloworld:compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
2 actionable tasks: 1 executed, 1 up-to-date
What am i doing wrong?
EDIT: a typo
If you are using a Bnd workspace model build, then you must configure the build path through each project's bnd.bnd file using the -buildpath Bnd instruction. Then the Bnd gradle plugin will use that information to program the configurations for java compilation.
Your example shows setting a compile dependency for the root gradle project which is wrong since (1) you should be using -buildpath in the project's bnd.bnd file and (2) it is done in the root project which is not meaningful to any child projects like your com.feddericokz.helloworld project.
So change your build.gradle file to not apply the 'java' plugin to the root project and not set compile dependencies for the root project and change your Bnd workspace so that the workspace (cnf) has configured repositories to access the desired bundles, for example https://github.com/osgi/enroute.workspace/blob/4070ff6668a1ee79b9b01cfa4caab86869247e7b/cnf/ext/enroute.bnd#L22-L28, and then set each project's bnd.bnd file to have the desired bundles on the -buildpath.
It is unclear if bnd_version is defined in this line:
classpath "biz.aQute.bnd:biz.aQute.bnd.gradle:${bnd_version}"
Consider adding a gradle.properties file with:
bnd_version=3.5.0
(or whatever version is apporpriate). As best as I can reproduce your situation, my example works for me with this.

Generated tests fail in Spring Cloud Contract

I am new to spring-cloud-contract. Just trying to configure my build and I am getting error.
Here is my project structure
Project
| ==> Module
I have the following dependencies for classpath in my project build.gradle
Project Build file
buildscript {
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.2.1"
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:1.1.3.RELEASE"
}
apply from: "$projectDir/gradle/app/springcloud.gradle" //Gradle build file in module
}
Module Build file
apply plugin: 'groovy'
apply plugin: 'spring-cloud-contract'
apply plugin: 'org.springframework.boot'
dependencies {
//Spring cloud contract dependencies
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.cloud:spring-cloud-starter-contract-verifier')
testCompile "org.springframework.cloud:spring-cloud-starter-contract-stub-runner"
testCompile "com.jayway.restassured:rest-assured:2.5.0"
testCompile "com.jayway.restassured:spring-mock-mvc:2.5.0"
}
//Spring cloud contract dependency management
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Dalston.RELEASE"
}
}
I have a groovy file which has a post request and a response. When I do gradle build, the tests are autogenerated but my build is feeling.
C:\Users\user\Projects\myProject\module\build\generated-test-sources\contracts\org\springframework\cloud\contract\verifier\tests\ContractVerifierTest.java:3: error: cannot find symbol
import com.jayway.jsonpath.DocumentContext;
^
symbol: class DocumentContext
location: package com.jayway.jsonpath
C:\Users\user\Projects\myProject\module\build\generated-test-sources\contracts\org\springframework\cloud\contract\verifier\tests\ContractVerifierTest.java:28: error: cannot find symbol
DocumentContext parsedJson =
JsonPath.parse(response.getBody().asString());
^
Am I doing something wrong? Is there any mismatch in the versions which I am using?
#Marcin Thanks for your inputs. This is how I solved this issue.
The build fails inside generated tests and jayway jsonpath is not showing in Red colour at the position of error. When I Ctrl + Hover (mouse) on the import it's referencing to com.jayway.jsonpath:json-path:2.2.0 which is not true. The reason is because I have imported many modules in Intellij and apparently one of the module also imports 2.2.0 and intellij is referencing it from external library.
So I did invalidate cache intellij and closed all unnecessary projects and reopened the project alone which I am working on, in a new window (Fresh session). Now gradle build and again I got the same error on the generated tests. But this time When I Ctrl + Hover (mouse) on the import its refering to the com.jayway.jsonpath:json-path:0.9.1 which is what I am expecting.
Now to solve this issue, I've examined the dependency tree using the below command and did some grep on it.
allDeps --configuration testRuntime
I found the answer, why gradle is replacing 2.2.0 with 0.9.1. I've figured out some project dependency is replacing it. Now the solution for this is modifying my module build file for jayway json-path as shown below. After that I've given build gradle command and now the errors are gone.
testCompile ('com.jayway.jsonpath:json-path:2.2.0') {
force = true
}
And finally, I've got exception saying that SpringCloudContractAssertions unable to find.
org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;
So I replace Camden release with Dalston as shown below. Now it all works fine.
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Dalston.RELEASE"
}
}

How do you compile a Gradle sub-project using debug classes from another module

I am working on the evolution of an Apache Ant project to compile it with Gradle, and I have different set of sub-projects.
My goal is to use the output of the main project as a dependency of some of the subprojects. The global configuration is the following:
MainProject:
-> src/main/java
-> src/main/resources
-> src/test/java
-> src/test/resources
-> src/mocks/java
ChildProject1:
-> src/main/java
-> src/main/resources
-> src/test/java
-> src/test/resources
-> src/mocks/java
The main project is correctly included in the child build path and all links are correctly found when it comes to Eclipse linking.
I have included the main project in the settings.gradle file for the ChildProject and its dependencies in the build:
dependencies {
compile project(':MainProject')
}
My problem is that the ChildProject needs outputs from the mocks/java to compile and though it seems that the Gradle compilation process does include the "default" output that are the main/java files, I get an error message saying that "symbols" from the mocks can not be found.
I had a similar problem with the MainProject test java files compilation that required the mocks to be used, it was solved by adding:
sourceSets {
mocks {
java {
srcDir 'src/mocks/java'
}
compileClasspath += sourceSets.main.runtimeClasspath
}
test {
compileClasspath += sourceSets.mocks.output
runtimeClasspath += sourceSets.mocks.output
}
}
The mock source is correctly added to the test classpath.
How do I tell my ChilProject to use the mocks output from MainProject to compile its tests?
I'll try to point you in correct direction. Normally you declare multi-project dependencies in Gradle like so:
dependencies {
compile project(':projA')
}
This define implicitly says use the default artifacts of projA as a dependency.
I believe what you want instead is to use the mock artifact of your projA dependency. The way to do this is:
Create a new jar task to combine the class files compiled from the main and mock sourceSet.
task mocksJar(type: Jar) {
classifier 'mocks'
group 'build'
from sourceSets.main.output
from sourceSets.mocks.output
}
Export the mock artifact in projA build by creating a new Gradle configuration called mocks and assigning the artifact to export:
configuration { mocks }
artifacts.mocks mocksJar
Import the mocks configuration in your project that requires projA using the extended dependency syntax:
dependencies {
compile project( path:':projA', configuration:'mocks')
}

Could not determine the dependencies of task ':xxx:check'

I have a project using Gradle 1.12, it goes well using gradle assemble, but exception occurs when executing gradle build as follows:
* What went wrong:
Could not determine the dependencies of task ':JavaUtil:check'.
> groovy.lang.MissingMethodException: No signature of method: org.gradle.api.internal.file.DefaultSourceDirectorySet.getTaskName() is applicable for
rgument types: (java.lang.String, null) values: [findbugs, null]
Possible solutions: getName()
Having the dependencies settings in gradle script of JavaUtil's parent folder like this:
dependencies {
compile (project (':JavaUtil')) {transitive = false}
The same error occurs when I executed gradle build in JavaUtil folder.
Any ideas what happens here?

Categories