I need to deploy a aplication in railway, but any time returno this error:
Caused by: java.lang.ClassNotFoundException: br.com.zezinho.helpdesk
Error: Could not find or load main class br.com.zezinho.helpdesk
My path is correct.
github: https://github.com/Luciannodev/helpdesk-back
plugins {
java
id("org.springframework.boot") version "2.7.8"
id("io.spring.dependency-management") version "1.1.0"
}
group = "br.com.zezinho"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
val mainClassName = "HelpDeskApplication.java"
configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
}
}
repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("io.jsonwebtoken:jjwt-api:0.11.5")
implementation("io.jsonwebtoken:jjwt-impl:0.11.5")
implementation("io.jsonwebtoken:jjwt-jackson:0.11.5")
implementation("mysql:mysql-connector-java:8.0.28")
implementation("com.h2database:h2:1.4.200")
testImplementation("org.springframework.boot:spring-boot-starter-test")
annotationProcessor("org.projectlombok:lombok")
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.jar {
manifest.attributes["Main-Class"] = "br.com.zezinho.helpdesk"
manifest.attributes["Class-Path"] = configurations
.runtimeClasspath
.get()
.joinToString(separator = " ") { file ->
"libs/${file.name}"
}
}
Related
I have a multi-module Gradle project which is structured like so:
root
|
|_____main
|
|_____other
This is how the build.gradle.ktss files are structured:
Root:
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.8.0" apply false
kotlin("plugin.spring") version "1.8.0" apply false
kotlin("plugin.lombok") version "1.8.0" apply false
id("org.springframework.boot") version "2.7.2" apply false
id("io.spring.dependency-management") version "1.0.12.RELEASE" apply false
}
subprojects {
apply {
plugin("org.jetbrains.kotlin.jvm")
plugin("org.jetbrains.kotlin.plugin.spring")
plugin("org.jetbrains.kotlin.plugin.lombok")
plugin("org.springframework.boot")
plugin("io.spring.dependency-management")
}
configurations {
configurations["compileOnly"].extendsFrom(configurations["annotationProcessor"])
all {
exclude(group = "org.springframework.boot", module = "spring-boot-starter-logging")
}
}
repositories {
mavenCentral()
}
val implementation by configurations
val testImplementation by configurations
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
implementation("org.springframework.boot:spring-boot-starter")
//other dependencies...
}
configure<DependencyManagementExtension> {
imports {
mavenBom("org.springframework.shell:spring-shell-dependencies:2.1.0")
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict", "-Xjvm-default=all")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
}
Main:
import org.springframework.boot.gradle.tasks.bundling.BootJar
plugins {
`java-test-fixtures`
application
}
group = "my.group"
version = "0.2.0"
java.sourceCompatibility = JavaVersion.VERSION_17
application {
mainClass.set("my.group.MainKt")
}
tasks.withType<BootJar> {
manifest {
attributes["Main-Class"] = "org.springframework.boot.loader.PropertiesLauncher"
}
}
task<JavaExec>("runBootJar") {
val bootJar: BootJar by tasks
classpath = files(bootJar)
debug = true
}
springBoot {
buildInfo()
}
extra["springShellVersion"] = "2.1.0"
dependencies {
implementation("org.springframework.boot:spring-boot-loader")
api("org.springframework.boot:spring-boot-starter-web")
api("org.springframework.boot:spring-boot-starter-batch")
api("org.springframework.boot:spring-boot-starter-data-mongodb")
api("org.springframework.boot:spring-boot-starter-data-jpa")
api("org.springframework.boot:spring-boot-starter-integration")
api("org.springframework.boot:spring-boot-starter-thymeleaf")
api("org.springframework.boot:spring-boot-starter-log4j2")
api("org.springframework.integration:spring-integration-mongodb")
api("org.springframework.boot:spring-boot-starter-mail")
api("org.springframework.shell:spring-shell-starter")
//other dependencies...
}
Other:
plugins {
`java-library`
}
group = "my.group"
version = "0.2.0"
java.sourceCompatibility = JavaVersion.VERSION_17
dependencies {
compileOnly(project(":main"))
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
//other dependencies...
}
As you can see, Main has a dependency towards org.springframework.shell:spring-shell-starter, which in turns makes this piece inside the Root build.gradle.kts mandatory:
configure<DependencyManagementExtension> {
imports {
mavenBom("org.springframework.shell:spring-shell-dependencies:2.1.0")
}
}
If I try to move this part inside the Main script, the following error pops up when I try to compile Other:
Execution failed for task ':other:compileKotlin'.
> Could not resolve all files for configuration ':other:compileClasspath'.
> Could not find org.springframework.shell:spring-shell-starter:.
Required by:
project :other> project :main
Also, if I run Gradle with the stacktrace enabled, there is also this error:
2: Task failed with an exception.
-----------
* What went wrong:
java.lang.StackOverflowError (no error message)
This made me think there could be some kind of cyclic dependency, but I couldn't find any.
I'd like to move the mavenBom part inside Main because Spring Shell is only used there, and not in every submodule, but in the end I cannot get it working.
What am I missing?
I have the following error message during the build, but no compileOnly dependency in my build.gradle. Why it's happens, and how to solve?
Regards
Resolving dependency configuration 'compileOnly' is not allowed as it
is defined as 'canBeResolved=false'. Instead, a resolvable
('canBeResolved=true') dependency configuration that extends
'compileOnly' should be resolved.
buildscript {
ext.kotlin_version = '1.6.0'
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
maven {
url "https://plugins.gradle.org/m2/"
}
repositories {
flatDir {
dirs 'jars'
}
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id "org.jetbrains.intellij" version "1.10.1"
id "org.jetbrains.grammarkit" version "2020.1"
}
configurations {
configurations.implementation.setCanBeResolved(true)
}
group 'com.interfaced'
version '0.2.7'
apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.intellij'
sourceSets {
all {
java.srcDirs += ['src/main/gen']
kotlin.srcDirs += ['src/main/kotlin']
resources.srcDirs = ['src/main/resources']
}
}
grammarKit {
jflexRelease = '1.7.0-2'
}
repositories {
mavenCentral()
maven {
url "https://cache-redirector.jetbrains.com/intellij-dependencies"
}
}
dependencies {
// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib
compileClasspath group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.7.22'
compileClasspath files('jars/grammar-kit.jar')
compileClasspath group: 'junit', name: 'junit', version: '4.12'
compileClasspath "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
}
intellij {
version '2022.3'
}
import org.jetbrains.grammarkit.tasks.GenerateLexer
import org.jetbrains.grammarkit.tasks.GenerateParser
def GENERATE_GROUP = 'Generate'
task generateLexer(type: GenerateLexer) {
source = "src/main/grammar/BrightScript.flex"
targetDir = "src/main/gen/com/interfaced/brs/lang/lexer"
targetClass = "_BrsLexer"
skeleton = "src/main/grammar/idea-flex.skeleton"
purgeOldFiles = true
description = 'Generate Lexer Java sources for BrightScript'
group = GENERATE_GROUP
}
task generateParser(type: GenerateParser) {
source = "src/main/grammar/BrightScript.bnf"
targetRoot = 'src/main/gen'
pathToParser = 'src/main/gen/com/interfaced/brs/lang/BrsParser.java'
pathToPsiRoot = 'src/main/gen/com/interfaced/brs/lang/psi'
purgeOldFiles = true
description = 'Generate Parser Java sources for BrightScript'
group = GENERATE_GROUP
// patch up to date check
outputs.upToDateWhen { false }
}
compileKotlin {
kotlinOptions.jvmTarget = "17"
}
compileKotlin.dependsOn(generateLexer, generateParser)
I'm trying to implement a simple spring boot web mvc application, but requirements are:
Generate code from api.yaml (openapi 3.0)
Generate swagger's code ( I guess it's just a couple of annotations but nevertheless)
But generated code doesn't compile due to generated:
import springfox.documentation.spring.web.paths.RelativePathProvider;
in org/openapitools/configuration/OpenAPIDocumentationConfig.java
my searching results summarized:
it's due to old springfox version, migrate to 3.0.0. The RelativePathProvider was replaced by DefaultPathProvider
but I'd already have it originally. And it'seems the generator doesn't know it need to generate DefaultPathProvider instead of old RelativePathProvider
My build.gradle.kts is:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.5.6"
id("org.openapi.generator").version("5.3.0")
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.31"
kotlin("plugin.spring") version "1.5.31"
kotlin("plugin.jpa") version "1.5.31"
}
group = "com.hometask"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("javax.validation:validation-api:2.0.1.Final")
implementation("org.flywaydb:flyway-core")
api("io.springfox:springfox-swagger2:3.0.0")
api("io.springfox:springfox-swagger-ui:3.0.0")
api("org.openapitools:jackson-databind-nullable:0.2.1")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compileOnly("org.projectlombok:lombok")
runtimeOnly("com.h2database:h2")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
val spec = "$rootDir/src/main/resources/openapi/api.yml"
val generatedSourcesDir = "$buildDir/generated/openapi"
openApiGenerate {
generatorName.set("spring")
inputSpec.set(spec)
outputDir.set(generatedSourcesDir)
apiPackage.set("org.openapi.example.api")
invokerPackage.set("org.openapi.example.invoker")
modelPackage.set("org.openapi.example.model")
configOptions.set(mapOf(
"dateLibrary" to "java8"
))
}
sourceSets {
getByName("main") {
java {
srcDir("$generatedSourcesDir/src/main/java")
}
}
}
tasks {
val openApiGenerate by getting
val compileJava by getting {
dependsOn(openApiGenerate)
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I'm trying to create a multimodule springboot application on gradle, but I get an Unresolved reference error message during build. Gradle config for individual modules is below.
Unresolved reference error on import org.example.finalmultimodule.data.repositories.IPersonRepository during build:
Error:
Directory tree:
BuildTools > Gradle:
Main build.gradle:
plugins {
id("org.springframework.boot") version "2.3.3.RELEASE"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("plugin.spring") version "1.4.10"
kotlin("jvm") version "1.4.10"
}
group = "org.example"
version = "1.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
// Projekty
project(":data") {
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management")
}
project(":business") {
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management")
}
project(":api") {
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management")
}
dependencies {
// Moduly
implementation(project(":core"))
implementation(project(":data"))
implementation(project(":business"))
implementation(project(":api"))
implementation(kotlin("stdlib"))
}
allprojects {
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
}
Core build.gradle:
plugins {
kotlin("jvm")
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
// Anotace pro #JsonProperty
implementation("com.fasterxml.jackson.core:jackson-annotations:2.11.2")
implementation(kotlin("stdlib"))
}
Data build.gradle:
plugins {
kotlin("jvm")
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
// Moduly
implementation(project(":core"))
// Anotace #Component etc...
implementation("org.springframework:spring-context")
// Corutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
// Databaze
implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")
implementation("io.r2dbc:r2dbc-mssql")
implementation(kotlin("stdlib"))
}
Business build.gradle:
plugins {
kotlin("jvm")
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(project(":core"))
implementation(project(":data"))
// Kediatr variace na Mediatr
implementation("com.trendyol:kediatr-spring-starter:1.0.14")
implementation(kotlin("stdlib"))
}
Api (springboot) build.gradle:
plugins {
kotlin("jvm")
kotlin("plugin.spring")
}
group = "org.example"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
// Moduly
implementation(project(":core"))
implementation(project(":business"))
// Kediatr variace na Mediatr
implementation("com.trendyol:kediatr-spring-starter:1.0.14")
implementation("org.springframework.boot:spring-boot-starter-rsocket")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("io.projectreactor:reactor-test")
}
I had a very similar situation where my classes from other modules would not be resolved even though everything looked fine in IntelliJ.
I managed to solve it by explicitly adding
#ComponentScan(basePackages = ["my.package"])
to my spring boot application.
Also, set library projects up like this:
tasks.findByName("bootJar")?.apply {
enabled = false
}
tasks.findByName("jar")?.apply {
enabled = true
}
In my build.gradle.kts, I want to write a function that uses an external class: StrSubstitutor from Apach Commons Text. However, the import is not found, although I can see the library when I run ./gradlew dependencies.
The build.gradle.kts file is as follows:
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.apache.commons.text.StringSubstitutor // Import not found
plugins {
val kotlinVersion = "1.3.61"
kotlin("jvm") version "$kotlinVersion"
kotlin("kapt") version "$kotlinVersion"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.apache.commons:commons-text:1.8")
// SourceSets
sourceSets.main {
withConvention(KotlinSourceSet::class) {
kotlin.srcDirs("src/main/kotlin")
}
}
sourceSets.test {
withConvention(KotlinSourceSet::class) {
kotlin.srcDirs("src/main/kotlin")
}
}
}
tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
systemProperty("spring.profiles.active", "test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
// Function that uses the import
fun getProperty(properties: Properties, propertyKey: String): String {
// Use the import "StrSubstitutor"
return ""
}
Is this possible with Kotlin, and if so: how?
Yes, it is possible. The reason it does not work as written is because you put the dependency on the Apache Commons Text into implementation configuration of the project, not into the classpath of the build script itself. So, you basically need to introduce a buildscript block to your build.gradle.kts file. Below is an example1:
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.apache.commons.text.StringSubstitutor
// TL DR: Add this block to your build script to make the import above work
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.apache.commons:commons-text:1.8")
}
}
tasks.register("hello") {
doLast {
println(StringSubstitutor.replaceSystemProperties(
"You are running with Java \${java.version} on OS \${os.name}."))
}
}
plugins {
val kotlinVersion = "1.3.61"
kotlin("jvm") version "$kotlinVersion"
kotlin("kapt") version "$kotlinVersion"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
// You probably do not need this for your project, so I commented it out
// implementation("org.apache.commons:commons-text:1.8")
// SourceSets
sourceSets.main {
withConvention(KotlinSourceSet::class) {
kotlin.srcDirs("src/main/kotlin")
}
}
sourceSets.test {
withConvention(KotlinSourceSet::class) {
kotlin.srcDirs("src/main/kotlin")
}
}
}
tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
systemProperty("spring.profiles.active", "test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
Run this script with ./gradlew -q hello to check whether it works or not.
1 New task hello exists there just to demonstrate that the import works, it is not needed in the final build script that you would use in your project.