Spring Boot Gradle application "Error: Could not find or load main class" - spring-boot

I have Spring boot Gradle project that runs perfectly local by Gradle, but not throw the jar file created.
The project has two targets: update data at Postgres DB from another api by scheduling, and let users get the data.
Application class:
I tried to deploy my project on Heroku, Google cloud and via java -jar command, I'm getting the same behavior.
I don't know if it's the problem but on the cloud the build runs successfuly, and after that I get the error: Error: Could not find or load main class com.Application . and ofcourse all the get requests gets denied and server is not accepts any requests.
my build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}
plugins {
id 'java'
id 'idea'
id 'application'
id 'java-library'
id 'java-gradle-plugin'
id "com.heroku.sdk.heroku-gradle" version "2.0.0"
id 'org.springframework.boot' version '2.7.2'
id "io.spring.dependency-management" version "1.0.4.RELEASE"
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'application'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
mainClassName = 'com.Application'
sourceSets.main.java.srcDirs = ['src/main/java']
springBoot{
mainClass = "com.Application"
}
repositories {
mavenCentral()
}
configurations {
all*.exclude module: "spring-boot-starter-logging"
all*.exclude group: "ch.qos.logback"
all*.exclude module: "log4j-over-slf4j" // allow using log4j 2.x
all*.exclude module: "slf4j-simple" // log4j is the configured backend
}
dependencies {
implementation group: 'org.jsoup', name: 'jsoup', version: '1.14.3'
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.apache.maven.plugins:maven-shade-plugin:3.0.0')
implementation('com.google.code.geocoder-java:geocoder-java:0.16')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
runtimeOnly('org.postgresql:postgresql')
implementation ( 'org.projectlombok:lombok:1.18.20')
annotationProcessor ( 'org.projectlombok:lombok:1.18.20')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
task cleanBuildPublish{
build.mustRunAfter clean
}
group 'com'
version '1.0-SNAPSHOT'
jar {
enabled =false
manifest {
attributes(
'Main-Class': 'com.Application',
)
}
}
bootJar {
manifest {
attributes(
'Main-Class': 'com.Application',
)
}
}
test {
useJUnitPlatform()
}
and my project structure :
the error I get on google cloud:

Related

Spring framework import failed

I am starting on Spring and learning its foundations. I created a project in intellij
It was giving an error stating that web cannot be imported.
so i guessed it must have been . my gradle build file which does not have web
I tried to import in manually via the build.gradle file as below but it throws me an error saying it cannot be found.
WHat am i doing wrong and how can i resolve this?
plugins {
id 'org.springframework.boot' version '2.2.2.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'io.codementor.gtommee'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
any help?
You are missing the spring-boot-starter-web dependency in your build.gradle file.
To get started, have a look at https://start.spring.io/

unable to run the springboot jar from command line

My requirement is to build a spring boot jar in standalone tomcat executable.I am not able to run the jar from command line.
I am using gradle
buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile project(':testProject')
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
configurations {
all*.exclude module: 'spring-boot-starter-logging'
}
jar {
manifest {
attributes 'Implementation-Title': 'SpringAppApplication',
'Implementation-Version': version
}
}
running java -jar target/SpringAppApplication-0.0.1-SNAPSHOT.jar
I get error Invalid or corrupt jarfile SpringApp-0.0.1-SNAPSHOT.jar
.Tried giving the complete path also.

Gradle build doesn't add version to package

I'm creating a REST microservice using Spring Boot 2.0.0.M7 with Gradle 4.2.
When I build from Eclipse or run from console ./gradlew build, the produced package in build/libs is named $app.jar instead of $app-$version.jar.
What am I missing? my build.gradle is the same as the Spring Boot Docker GS guide, and this problem prevent docker image to be built because the jar can't be found.
Here is my build.gradle file:
buildscript {
ext {
springBootVersion = '2.0.0.M7'
springCloudVersion = 'Finchley.M5'
gradleDockerVersion = '0.13.0'
}
repositories {
mavenCentral()
maven { url 'https://plugins.gradle.org/m2/' }
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/libs-milestone' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("gradle.plugin.com.palantir.gradle.docker:gradle-docker:${gradleDockerVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'com.palantir.docker'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'networks'
version = '0.9'
}
group = 'test'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/libs-milestone' }
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-data-rest'
compile 'org.springframework.boot:spring-boot-starter-json'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
runtime 'org.springframework.boot:spring-boot-devtools'
runtime 'org.postgresql:postgresql'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'junit:junit'
}
docker {
name "${project.group}/${jar.baseName}"
files jar.archivePath
buildArgs(['JAR_FILE': "${jar.archiveName}"])
}
The version should be specified outside jar:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'com.palantir.docker'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
version = "0.9"
jar {
baseName = 'networks'
}

Gradle - Exclude provided scope jars from War file

Gradle seems to claim that it doesnt include the providedCompile and providedRuntime scopes when building the war file.
But, when i do the build with below war config, a folder named "lib-provided" seems to be containing all the provided scoped jars. How do I limit this functionality to NOT include the provided scoped jars.
configure(rootProject) {
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
targetCompatibility = 1.8
sourceCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repository.jboss.org/nexus/content/groups/public-jboss/" }
}
// Import Spring Boot's bom, spring-boot-dependencies
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-dependencies:1.2.5.RELEASE'
}
}
// Override the spring-data-releasetrain.version property
ext['spring-data-releasetrain.version'] = 'Fowler-SR1'
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-security")
............ Other Spring Boot based projects
testCompile("org.springframework.boot:spring-boot-starter-test")
compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:2.4.6")
......... Below are the "Provided" Scoped packages
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
providedCompile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.23")
providedCompile("org.hibernate:hibernate-core:4.3.7.Final")
providedCompile("org.hibernate:hibernate-infinispan:4.3.7.Final")
providedCompile("org.hibernate:hibernate-entitymanager:4.3.7.Final")
providedCompile("org.hibernate:hibernate-validator:5.1.3.Final")
providedCompile("org.hibernate:hibernate-search-orm:5.0.1.Final")
providedCompile("org.hibernate.common:hibernate-commons-annotations:4.0.4.Final")
providedCompile("org.infinispan:infinispan-core:7.1.1.Final")
providedCompile("org.infinispan:infinispan-query:7.1.1.Final")
testCompile("com.microsoft.sqlserver:sqljdbc41:4.1")
}
configurations {
providedCompile
// replaced with jcl-over-slf4j
all*.exclude group: 'commons-logging', module: 'commons-logging'
// replaced with log4j-over-slf4j
all*.exclude group: 'log4j', module: 'log4j'
}
}
war {
baseName = 'abc'
version = '5.0.0-SNAPSHOT-' + + System.currentTimeMillis();
doFirst {
manifest {
attributes("Implementation-Title": project.name, "Implementation-Version": version, "Implementation-Timestamp": new Date())
}
}
webAppDirName = 'web'
includeEmptyDirs false
archiveName 'abc.war'
}
Thanks!
In the below configuration, Gradle 4.6
None of the below depndnecies will be in the War/WEB-INF lib.
If you have an Ear build.gradle can contain entry like below , which will ensure all depndence jar in the Ear/lib folder.
if the below entry is made, The generated Ear file will have .war file and the War/lib folder will not be having the specified dependent files.
apply plugin: 'war'
earlib project(path: ':MyWebProject', configuration: 'compile')
deploy project(path: ':MyWebProject', configuration: 'archives')
apply plugin: 'war'
dependencies {
providedCompile('org.apache.logging.log4j:log4j-web')
providedCompile('org.springframework.boot:spring-boot-starter-web') {
exclude module: "spring-boot-starter-tomcat"
}
}

spring-boot gradle plugin can't be found

I have a separate gradle script that is just adding spring-boot plugin. It looks like this:
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url 'http://repo.spring.io/libs-release' }
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE'
}
}
apply plugin: 'spring-boot'
Then, in another project, it is referenced like this:
apply from: '../../food-orders-online-main/spring-boot.gradle'
When I run build task I'm getting the following error:
A problem occurred evaluating script.
> Failed to apply plugin [id 'spring-boot']
> Plugin with id 'spring-boot' not found.
Someone knows what I'm doing wrong?
Applying a plugin by plugin id is not supported in script plugins. You must use the plugin's fully qualified class name.
apply plugin: org.springframework.boot.gradle.plugin.SpringBootPlugin
See this thread for more information.
UPDATE: Updating plugin class name.
These are the Plugins that I am using on spring boot 2.0.1
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
My complete vanilla gradle file here (Spring boot 2.0.5)
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
OR
there is an even better option, go to the spring starter (spring boot template generating tool) start.spring.io And generate a template project from there, and build step-by-step from there.
This code works for me
plugins{
id 'org.springframework.boot' version '2.0.3.RELEASE'
}
Add:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.0.4.RELEASE"
}}
Change:
apply plugin: 'spring-boot'
to:
apply plugin: "org.springframework.boot"
Starting from SpringBoot 1.4.0.RELEASE the plugin package has been slightly changed.
apply plugin: org.springframework.boot.gradle.plugin.SpringBootPlugin
Check your syntax and where your parentheses close } I had the same issue and it turned out my repositories { didnt contain } at the end
Got a similar exception
Caused by: org.gradle.internal.resolve.ArtifactNotFoundException: Could not find spring-boot-gradle-plugin-3.0.0-SNAPSHOT.jar (org.springframework.boot:spring-boot-gradle-plugin:3.0.0-SNAPSHOT:20220216.230329-214)
Running the gradle build with --refresh-dependencies fixed it
Below code works for me:-
buildscript {
ext { springBootVersion = '2.0.5.RELEASE'}
repositories { mavenCentral() }
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-
starter-web', version: '2.0.5.RELEASE'
}
After proxy setting ( What is the proxy IP in your organization), I have added following code in build.gradle file. The problem has gone.
sourceSets {
main {
output.setResourcesDir(java.outputDir)
}
}
As of now July first 2020, this is how I was able to configure it, and it worked! Hopefully, this can help anyone facing this problem.
Note: Pay attention to the versions
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath('org.springframework.boot:spring-boot-gradle-plugin:2.2.8.RELEASE')
}
}
plugins {
id 'org.springframework.boot' version '2.2.8.RELEASE'
id 'java'
}
group 'org.capfer'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile('org.springframework.boot:spring-boot-starter-web:2.2.8.RELEASE')
}
could it be, that you don't have a #SpringBootApplication with a main(String[] main) on your classpath ??

Resources