Running a Spring Boot app using ./gradlew appRun - java

I'm using Spring Boot for a school project as a personal choice but the automated tester uses ./gradlew appRun to start the whole thing up.
This was fine before when they were using servlets but after moving to Spring Boot, I'm getting several exceptions when trying to do so.
Execution failed for task ':appRun'.
Could not get unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension.
and this is my current build.gradle.
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath "gradle.plugin.org.akhikhl.gretty:gretty:2.0.0"
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'org.gretty' version '2.2.0'
id 'war'
}
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "org.akhikhl.gretty"
apply plugin: 'application'
sourceCompatibility = 8
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
runtimeOnly('com.h2database:h2')
runtimeOnly('mysql:mysql-connector-java')
runtimeOnly('org.hsqldb:hsqldb')
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.7'
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile group: 'org.apache.ibatis', name: 'ibatis-core', version: '3.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
compile group: 'org.apache.ibatis', name: 'ibatis-core', version: '3.0'
compile group: 'org.gretty', name: 'gretty-runner-jetty94', version: '2.2.0'
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
providedRuntime ('org.springframework.boot:spring-boot-starter-tomcat')
runtimeOnly('mysql:mysql-connector-java')
compileOnly('org.projectlombok:lombok')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
gretty {
contextPath = '/'
}
This all works fine when using ./gradlew bootRun but that doesn't pass the automated tester.
Is there a way to make running ./gradlew appRun start up the SpringBoot main class similarly or directly ./gradlew bootRun?

This is a gretty question. Add this line to gretty config:
gretty {
contextPath = '/'
springBoot = true
}
Here's the doc: http://akhikhl.github.io/gretty-doc/spring-boot-support.html

Related

Migrate Spring Boot 1.5.8 to 2.1.5 -- Gradle issues

Migrating from Spring Boot 1.5.8 to 2.1.5, getting the error message:
Could not set unknown property 'sourceCompatibility' for project ':api' of type org.gradle.api.Project.
If I comment that line, I get the error message:
Could not set unknown property 'targetCompatibility' for project ':api' of type org.gradle.api.Project.
If I comment that line, I get the error message:
Could not find method jar() for arguments [build_4wobgm6qykoy29e0in3cntga8$_run_closure2#1fc9b06d] on project ':api' of type org.gradle.api.Project.
So something is just not right here. I have two build.gradle files, one in my root, one in api:
/build.gradle
plugins {
id 'idea'
id 'java'
id 'com.jfrog.bintray' version '1.8.4'
}
apply from: "$rootDir/gradle/git-version.gradle"
version getVersionFromGit()
group 'com.my_org.my_proj'
apply from: "$rootDir/gradle/bintray-vars.gradle"
subprojects {
repositories {
jcenter()
mavenCentral()
}
}
wrapper {
gradleVersion = '4.8.1'
}
/api/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
}
group = rootProject.group
version = rootProject.version
repositories {
maven { url 'https://dl.bintray.com/my-org/spring-utils' }
}
apply from: "$rootDir/gradle/checkstyle.gradle"
sourceCompatibility = 1.8
targetCompatibility = 1.8
jar {
archiveName = 'api.jar'
baseName = project.name
version = project.version
}
test {
doFirst {
environment "BUILD_NUMBER", "1"
}
}
dependencies {
def springBoot = '2.1.5.RELEASE'
runtime group: 'org.springframework.boot', name: 'spring-boot-properties-migrator', version: springBoot
compile group: 'org.flywaydb', name: 'flyway-core', version: '5.2.4'
compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.4.3.Final'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBoot
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBoot
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest', version: springBoot
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: springBoot
compile group: 'org.springframework.boot', name: 'spring-boot-starter-freemarker', version: springBoot
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBoot
testCompile group: 'org.flywaydb.flyway-test-extensions', name: 'flyway-spring-test', version: '4.2.0.2'
}
I'm clearly missing something here. I've been following the migration guide as well as Googling, of course. Suggestions?
You haven't applied the java plugin to your api sub-project so the sourceCompatibility and targetCompatibility properties that it adds to the project are missing as is the jar task that it defines.
Adding id java to the plugins block at the top of api/build.gradle should fix the problem.

Gradle with ajc doesn't read Lombok annotations

I am trying to add ajc compiler to my gradle project as gradle plugin. Unfortunately, during compilation it shows me massive amount of errors due to Lombok.
build.gradle:
group 'com.kmb.bank'
version '0.0.1-SNAPSHOT'
project.ext {
aspectjVersion = '1.9.2'
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "aspectj.gradle"
sourceCompatibility = 11
targetCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.7'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
implementation('org.springframework.boot:spring-boot-starter-amqp')
implementation('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile ('org.springframework.boot:spring-boot-starter-security')
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile group: 'org.springframework.boot', name: 'spring-boot-starter- aop', version: '2.1.1.RELEASE'
}
It shows me errors that there are no getter, setters for every model.
This is working for me:
plugins {
id 'java'
id "io.freefair.aspectj.post-compile-weaving" version "4.1.4"
}
group 'com.amdocs'
version '1.0.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.10'
annotationProcessor "org.projectlombok:lombok:1.18.10"
}

Spring cloud starter config dependency causes ApplicationContextException

I'm trying to add the spring-cloud-starter-config dependency to my project (I've already got the spring configuration server itself up and running).
When I add the compile('org.springframework.cloud:spring-cloud-starter-config') dependency to my build.gradle file my application won't start anymore; instead it throws:
project_name | org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
project_name | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
project_name | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
project_name | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
It's running inside a docker container, the docker file is as follows:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
ADD ${JAR_FILE} /project-name.jar
ARG PROPS
ADD ${PROPS} /application.properties
# Expose web port
EXPOSE 8090
# Remote debugging port for intelliJ == address
EXPOSE 50505
ENTRYPOINT [ "java", "-Xrunjdwp:transport=dt_socket,address=50505,suspend=n,server=y", "-jar", \
"/project-name.jar", "--spring.config.location=file:/project-name/application.properties"]
And the build.gradle:
buildscript {
ext {
springBootVersion = '1.5.12.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'org.owasp:dependency-check-gradle:3.3.1'
}
}
plugins {
id 'de.aaschmid.cpd' version '1.1'
id "com.github.johnrengelman.shadow" version "2.0.3"
}
group 'groupname'
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'findbugs'
apply plugin: 'cpd'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'org.owasp.dependencycheck'
sourceCompatibility = 1.8
repositories {
jcenter()
mavenCentral()
}
dependencies {
//compile('org.springframework.boot:spring-boot-starter')
compile("org.springframework.boot:spring-boot-starter-web")
compile('org.springframework.cloud:spring-cloud-starter-config')
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:5.6.8'
compile 'org.elasticsearch:elasticsearch:5.6.8'
compile 'org.elasticsearch:elasticsearch:5.6.8:javadoc'
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:5.6.8:javadoc'
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:5.6.8:sources'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore-nio
compile group: 'org.apache.httpcomponents', name: 'httpcore-nio', version: '4.4.9'
// https://mvnrepository.com/artifact/javax.validation/validation-api
compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
// https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.9.Final'
// https://mvnrepository.com/artifact/commons-io/commons-io
compile group: 'commons-io', name: 'commons-io', version: '2.6'
// https://mvnrepository.com/artifact/commons-configuration/commons-configuration
compile group: 'commons-configuration', name: 'commons-configuration', version: '1.10'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor
compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '1.5.12.RELEASE'
testCompile('org.springframework.boot:spring-boot-starter-test')
// https://mvnrepository.com/artifact/org.mockito/mockito-all
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
testCompile group: 'junit', name: 'junit', version: '4.12'
// testCompile 'info.cukes:gherkin:2.12.2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4"
}
}
repositories {
maven {
url 'https://repo.spring.io/libs-milestone'
}
maven {
url 'https://repo.spring.io/libs-snapshot'
}
}
jar {
manifest {
attributes(
'Main-Class': 'com.altran.project.MainApplication'
)
}
}
shadowJar {
// make more dynamic?
baseName = 'project-name'
archiveName = "${baseName}-${version}.${extension}"
}
Any ideas why this spring-cloud dependency causes this issue?

Run as spring boot app is not working where as run as java app working

I am using STS.
This is my main class:
#EnableZuulProxy
#SpringBootApplication
public static void main(String[] args) {
try {
SpringApplication.run(DevProxyApp.class, args);
}catch(Exception e) {
}
}
}
Below is my build.gradle :
buildscript {
ext {
springBootVersion = '2.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'pmd'
id 'org.sonarqube' version '2.6.2'
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
mainClassName = 'com.siemens.mindsphere.devproxy.DevProxyApp'
group = 'mindsphere'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
ext {
springCloudVersion = 'Finchley.BUILD-SNAPSHOT'
}
jar {
baseName = 'sdk-devproxy'
doLast {
}
destinationDir = file("$buildDir/libs/mindsphere/sdk-devproxy/$project.version/")
}
dependencies {
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-parent', version: 'Edgware.SR3', ext: 'pom'
compile('org.springframework.cloud:spring-cloud-starter-oauth2')
compile('org.springframework.cloud:spring-cloud-starter-netflix-zuul')
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'com.auth0', name: 'java-jwt', version: '3.3.0'
compile('com.auth0:java-jwt')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
i have also tried with below dependencies:
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '1.2.1.RELEASE', ext: 'pom'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-parent', version: 'Edgware.SR3', ext: 'pom'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-oauth2', version: '1.0.0.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version: '1.4.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.9.RELEASE'
implementation 'org.slf4j:slf4j-api:1.7.25'
compile group: 'com.auth0', name: 'java-jwt', version: '3.3.0'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
Whenever I run the application as spring boot app its giving me the below error:
Error: Could not find or load main class com.siemens.mindsphere.devproxy.DevProxyApp
While running as java application, it is working starting but with this kind of launch functionalities(oauth2, zuul routing functionalities) are not working.
i have tried below things, but still issue is there:
Refreshing , rebuilding, updating gradle
removing all the dependencies manually, removing gradle repo manually
installed new STS.
If you need any other info to address this issue please let me know.
FYI Previously it was a maven project and working fine, now I am making it as gradle project by adding build.gradle, gradle project and etc. and removed pom.xml. Gradle build is happening properly.
Is the issue with any jar compatibility ????
Try to add a manifest attribute:
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'com.siemens.mindsphere.devproxy.DevProxyApp'
)
}
}
Try to downgrade the springBootVersion.
I had this issue and this resolved it.

Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter

I am using spring Boot.I created Spring boot gradle application.I got following error.
Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter
build.gradle
buildscript {
ext {
springBootVersion = '1.3.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'demo'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'org.springframework', name: 'spring-web', version: '4.0.0.RELEASE'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.12'
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.12'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.2.2'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.2.2'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.2.2'
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
Am I missing any dependency in build.gradle?
How to solve this?
You are including two different versions of the jackson lib. For Spring 4+ use the com.fasterxml.jackson.core like you have. Remove the two org.codehaus.jackson dependencies.

Categories

Resources