Gradle build comes up with java.lang.NoClassDefFoundError - spring-boot

In a project I am using gradle, Java11, SpringBoot, Junit5. The application is launching without any problem and when I run the unit tests individually everything is fine.
However, when I run gradle->build task the following error appears:
Task :test FAILED
JwtTokenProviderTest > initializationError FAILED
java.lang.NoClassDefFoundError
Caused by: java.lang.ClassNotFoundException
1 test completed, 1 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
More ever, I tried the following config in build.gradle
tasks.withType(Test) {
scanForTestClasses = false
include "**/*Test.class" // whatever Ant pattern matches your test class files
}
But it does not fix the problem.
Any idea?
Thanks

Related

Could not resolve all files for configuration `_classStructurekaptKotlin` caused by Execution failed for `StructureTransformAction`

Gradle multi-module project build fails with an unclear error. I run this command:
gradle :module:processor:integrationTest
(module:processor depends on module:processor-core, integrationTest is a custom Gradle task for running tests. I'm using kapt plugin as an annotation processor for Spring Boot configuration properties)
And I get this result:
> Task :module:processor-core:kaptGenerateStubsKotlin UP-TO-DATE
> Task :module:processor-core:kaptKotlin UP-TO-DATE
...
> Task :module:processor-core:jar SKIPPED
> Task :module:processor:kaptGenerateStubsKotlin
> Task :module:processor:kaptKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':module:processor:kaptKotlin'.
> Could not resolve all files for configuration ':module:processor:_classStructurekaptKotlin'.
> Failed to transform processor-core-SNAPSHOT.jar to match attributes {artifactType=class-structure, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
> Execution failed for StructureTransformAction: D:\dev\backend-project\module\processor-core\build\libs\processor-core-SNAPSHOT.jar.
> D:\dev\backend-project\module\processor-core\build\libs\processor-core-SNAPSHOT.jar (The system cannot find the path specified)
I don't understand why.
it was fixed by adding the following code to the build.gradle of processor-core module:
jar {
archiveBaseName = 'processor-core'
}
without these lines, I guess this step is disabled by Spring Boot, so the JAR file was not produced, and this led to the error.

Gradle multi-project only executes tests for one project

I have a multi-project gradle build with four Kotlin Multiplatform modules, two of which have tests. When I run gradle check, if any of the tests from one of the modules fails, the tests for the other module do not get executed.
I'm using Gradle 7.3, Java 17 and kotlin.test. Tests for both projects are located in the commonTest source set. Also tried Gradle 7.1 and Java 11 with the same behavior.
Excerpt from settings.gradle.kts:
include(":ProjectA")
include(":ProjectB") // B has tests and depends on D, its tests are run
include(":ProjectC")
include(":ProjectD") // D has tests but are not run
Excerpt from ProjectB build.gradle.kts:
sourceSets {
val commonMain by getting {
dependencies {
api(compose.runtime)
api(compose.foundation)
api(compose.material)
implementation(project(":ProjectD"))
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
From the output of gradle check I can see that :ProjectB:allTests gets executed and fails but :ProjectB:allTests never gets executed. This is an excerpt from the gradle output:
> Task :ProjectB:desktopTest
com.mylibrary.AppTest[desktop] > helloTestNg[desktop] FAILED
java.lang.AssertionError at AppTest.kt:8
2 tests completed, 1 failed
There were failing tests
> Task :ProjectB:allTests FAILED
FAILURE: Build failed with an exception.
If I do gradle -p ProjectD check tests for ProjectD are executed correctly.
Default Gradle behavior is to stop if any task fails, and Gradle considers a failing test as failing the check task. As a consequence, if any test fails in a certain project, the tests for the projects that have not yet been executed will not get executed.
The --continue can be useful in this case, it changes the default behavior and forces Gradle to continue executing all tasks even if some of them failed.
In this issue it is very well explained https://youtrack.jetbrains.com/issue/KT-49858p

java.lang.NoClassDefFoundError at Class.java:-2

Having following code:
https://github.com/arturmkr/myReportProblem
Using Java 11.
When I try to execute it I am having issue:
./gradlew clean ui:test
> Task :ui:test FAILED
com.db.demo.ui.DummyCheck > initializationError FAILED
java.lang.NoClassDefFoundError at Class.java:-2
Caused by: java.lang.ClassNotFoundException at BuiltinClassLoader.java:581
1 test completed, 1 failed
Who could help?
Ok now it works (but your tests fail):
Add to your non-Spring boot project's build.gradle (I mean your repository project's build.gradle)
bootJar
{
enabled = false
}
jar
{
enabled = true
}
./gradlew clean build

Difference between test and check tasks in Gradle

My build.gradle is as follows:
group 'groupName'
version 'version'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
. . .
}
dependencies {
. . .
testCompile group: 'junit', name: 'junit', version: '4.12'
}
In Gradle when doing ./gradlew tasks I receive
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
What is the difference between these two tasks? The output of ./gradlew check is identical to ./gradlew test.
andrewgazelka $ ./gradlew check
> Task :test FAILED
MathTest > testX FAILED
java.lang.AssertionError at MathTest.java:40
MathTest > testY FAILED
java.lang.AssertionError at MathTest.java:55
SimulatorTest > testZ FAILED
java.lang.IllegalArgumentException at SimulatorTest.java:71
30 tests completed, 3 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///Users/andrewgazelka/IdeaProjects/RobotCode2018/build/reports/tests/test/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 2s
3 actionable tasks: 3 executed
andrewgazelka $ ./gradlew test
> Task :test FAILED
MathTest > testX FAILED
java.lang.AssertionError at MathTest.java:40
MathTest > testY FAILED
java.lang.AssertionError at MathTest.java:55
SimulatorTest > testZ FAILED
java.lang.IllegalArgumentException at SimulatorTest.java:71
30 tests completed, 3 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///Users/andrewgazelka/IdeaProjects/RobotCode2018/build/reports/tests/test/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
3 actionable tasks: 1 executed, 2 up-to-date
From what I understand, ./gradle test ⊆ ./gradle check. Is this correct?
The Gradle check task depends on the test task which means test is executed before check is run. The documentation states that check performs all verification tasks in the project, including test and also tasks plugins add to the project:
If you for example add the checkstyle plugin to your project you can either run its tasks checkstyleMain and checkstyleTest individually or execute a full project verification using check. In this case the tasks test, checkstyleMain and checkstyleTest would be run.
Whereas test always just executes your unit tests.

Disable check for using 'kotlin' package in Gradle

When I try to gradlew build my project, I receive an error message from the compileKotlin task:
F:\IdeaProjects\walp.tinykotlintest>gradlew build
:compileKotlin
...
Only the Kotlin standard library is allowed to use the 'kotlin' package
...
BUILD FAILED
Total time: 6.214 secs
This happens because I've declared a class in a package that starts with: kotlin...
Is there a way to dissable this check by configuring my build.gradle script?
EDIT: I know for sure this is possible if I compile with the bare kotlin compiler...here is an example of using the kotlin compiler to compile a class in the kotlin package...So I think there should to be a way to do it in gradle too!....I hope...
What I've tried so far:
I tried to configure the gradle.build by configuring the KotlinCompile task:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
tasks.withType(KotlinCompile) {
it.kotlinOptions.allowKotlinPackage = true
}
The gradle script runs fine with these changes...but it seems like this is getting ignored...and I still encounter the error.
I tried to subclass the KotlinCompile class and override its beforeCompileHook() and replace the existing compileKotlin task with my version:
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
class MyKotlinCompile extends KotlinCompile {
#Override
public void beforeCompileHook(K2JVMCompilerArguments args) {
args.allowKotlinPackage = true
throw new RuntimeException("HELLO") // added to see if hook is run
}
}
tasks.create(name: "compileKotlin", type: MyKotlinCompile, overwrite: true)
Studying the source code...I was fairly certain this would work but evidently it didn't. I tried with and without throwing the RuntimeException but I always get the following error when I tried to gradle build again:
F:\IdeaProjects\walp.tinykotlintest>gradlew build --stacktrace
:compileKotlin UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:sourcesJar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestKotlin'.
> java.lang.NullPointerException (no error message)
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileTestKotlin'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
.
.
.
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Caused by: java.lang.NullPointerException
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile$populateTargetSpecificArgs$2.invoke(Tasks.kt:215)
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.populateTargetSpecificArgs(Tasks.kt:222)
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.populateTargetSpecificArgs(Tasks.kt:148)
at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.execute(Tasks.kt:105)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.doExecute(AnnotationProcessingTaskFactory.java:243)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:219)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.execute(AnnotationProcessingTaskFactory.java:230)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:208)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
... 60 more
I probably shouldn't be doing this anyways...but I really want to try it out.
thank you!
If the Kotlin standard library is incompatible with the JVM that you're using, it sounds like what you should actually do is fork Kotlin, make the changes required for compatibility with your JVM, and then use the standard Maven build script to build your fork. The script already provides the option to allow compiling files in the kotlin package.
The error has nothing to do with Gradle. The Kotlin compiler won't compile code in a kotlin package unless you are developing the Kotlin itself.
This was done for the same reason you can't write code in a java package.

Resources