Building and running an executable jar of spock groovy scripts - gradle

I have a multi-module Gradle application:
root setting.gralde.kts:
include(
":common-config-qa",
":app-spock-tests",
)
the common-config-qa application contains
main spock-groovy code which sets up required data for the tests to be executed.
java util classes
the app-spock-tests contains spock-groovy code to run spock tests for multiple apps separated into each folder.
When I execute ./gradlew clean test the tests are getting executed.
I tried to create an executable jar out of this project by specifying the following in the build.gradle.kts file under common-config-qa project:
application {
mainClass.set("com.test.qa.CommonSpockSpec")
}
tasks.jar {
manifest {
attributes(mapOf("Implementation-Title" to project.name,
"Implementation-Version" to project.version))
}
}
Also included the plugin: id("com.github.johnrengelman.shadow") version "7.1.2" to create a shadow jar.
I included the same plugin on the other app.
The uber-jar is getting created but when I execute the command from the root folder:
./gradlew jar common-config-qa/build/libs/common-config-qa-0.1-all.jar: I am getting the error as
FAILURE: Build failed with an exception.
* What went wrong:
Task 'common-config-qa/build/libs/common-config-qa-0.1-all.jar' not found in root project 'root-spock-project'.
If I execute the same command from inside the folder common-config-qa, I am getting the error as:
* What went wrong:
Task 'build/libs/common-config-qa-0.1-all.jar' not found in root project 'common-config-qa'.
I would like to have a jar which I can use to execute the tests from command line. How can I get an executable jar for this application and its runs properly.

Related

Jib Main class error with spring boot multi module (gradle)

root-projec
ㄴ sub1-project
build.gradle
ㄴ sub2-project
build.gradle
build.gradle
I'm trying to build all subprojects as jibs in the root project.
The following script is the build.gradle file of the root and sub modules.
...
jib {
from {
image = "eclipse-temurin:17"
}
to {
image = "abc/${project.name}:${project.version}"
tags = ["latest"]
}
}
cd root-project
./gradlew jib
Execution failed for task ':jib'.
> com.google.cloud.tools.jib.plugins.common.MainClassInferenceException: Main class was not found, perhaps you should add a `mainClass` configuration to jib
Of course there are no classes in the root project. Why do you want to include the main class in the root project when building the jib? I'd like to find a way to exclude this.
Additionally, is there any way to build with jib when building with a specific profile as follows?
There is a way to write a script in maven, but gradle has grammatical difficulties.
./gradlew clean build -P build-docker-image

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

Gradle Build single module

I have a problem with build single module from Gradle multimodule project.
I have a structure:
-root-project:
-first-module
-second-module
-modules:
-first-submodule
-second-submodule
setting.gradle.kts
rootProject.name = "root"
include(
"first-module",
"second-module",
"modules",
"modules/first-submodule",
"modules/second-submodule"
)
When I run ./gradlew clean build, each module builds.
When I run ./gradlew first-module:clean first-module:build, builds only first-module.
But when I run ./gradlew modules:first-submodules:clean modules:first-build, I got error:
* What went wrong:
Project 'first-submodules' not found in project ':modules'.
Why?

Unable to run jar file. (Error: Could not find or load main class co.pissarra.Mainkt)

I was trying to create a small webserver using spark and kotlin.
But I am stuck at the step where I should be able to create a jar of the project and run it from the command line. But I get the following error on running java -jar pissarra-core-all-1.0-SNAPSHOT.jar
Error: Could not find or load main class co.pissarra.Mainkt
I tried using intellij idea's artifact creation without success, and moved on to creating jar using build.gradle. Following is the code for the same
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Version': version,
'Main-Class': 'co.pissarra.Mainkt'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
After running ./gradlew clean followed by ./gradlew fatJar, I am able to create jar file in build/libs/ directory. This jar also gives the same error.
On extracting the jar file, I am able to see the directory structure co/pissarra/ with the Mainkt.class file inside it. Also it has the META-INF directory with MANIFEST.MF file whose content are as follows
Manifest-Version: 1.0
Implementation-Version: 1.0-SNAPSHOT
Main-Class: co.pissarra.Mainkt
Since stackoverflow does not allows to upload files, you can find the jar file here. You can also build the jar file from the github project here.
Check your MANIFEST file. The class name should be "MainKt" and not "Mainkt"

Gradle - Could not find or load main class src.main.java.Test

I have the following folder structure and want to play with gradle testing.
Before starting any tests I want to see what tasks are available.
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that
depend on it.
buildNeeded - Assembles and tests this project and all projects it depends
on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root
project 'GradleTesting'.
components - Displays the components produced by root project
'GradleTesting'. [incubating]
dependencies - Displays all dependencies declared in root project
'GradleTesting'.
dependencyInsight - Displays the insight into a specific dependency in root
project 'GradleTesting'.
help - Displays a help message.
model - Displays the configuration model of root project 'GradleTesting'.
[incubating]
projects - Displays the sub-projects of root project 'GradleTesting'.
properties - Displays the properties of root project 'GradleTesting'.
tasks - Displays the tasks runnable from root project 'GradleTesting'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Other tasks
-----------
execute
So I know that there isn't any problect with gradle. Now I write this code in my build.gradle file.
apply plugin: 'java'
task execute(type: JavaExec) {
//This line throws me an exception.
main = "src.main.java.Test"
classpath = sourceSets.main.runtimeClasspath
}
And get this message.
$ gradle execute
:compileJava
:processResources UP-TO-DATE
:classes
:executeError: Could not find or load main class src.main.java.Test
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':execute'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe''
finished with non-zero exit value 1
Any ideas how to fix this error?
Thanks.
'src/main/java' is the file system path to the java files.
The package names start here.
In your case there is no package name, the fully-qualified class name is "Test" so, change your build.gradle to:
apply plugin: 'java'
task execute(type: JavaExec) {
main = 'Test'
classpath = sourceSets.main.runtimeClasspath
}

Resources