How to pass gradle properties into Spring Boot and debug in Eclipse? - java

The application run as expected when running as bootRun task under Gradle Tasks in Eclipse, however, right clicking on project --> Run As --> Spring Boot App doesn't replace the value of the property in the following prototype.
build.gradle file
import org.apache.tools.ant.filters.ReplaceTokens
plugins {
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'eclipse'
}
group = 'com.sample.auto.expanson'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
// enable auto property expansion for passing gradle property to spring boot
// https://www.baeldung.com/spring-boot-auto-property-expansion
processResources {
duplicatesStrategy = 'include'
with copySpec {
from 'src/main/resources'
include '**/application*.properties'
include '**/application*.yaml'
include '**/application*.yml'
project.properties.findAll().each {
prop ->
if (prop.key != null) {
filter(ReplaceTokens, tokens: [(prop.key): prop.value.toString()])
filter(ReplaceTokens, tokens: [('project.' + prop.key): prop.value.toString()])
}
}
}
}
gradle.properties
expansion.property=Hello Expansion Property!
application.properties
com.test.value=#expansion.property#
DemoApplication.java
package com.sample.auto.expanson.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication implements CommandLineRunner {
#Value("${com.test.value}")
String expansionProperty;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println(expansionProperty);
}
}
The bootRun task results in expected output:
Hello Expansion Property!
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
Running it as Spring Boot App in Eclipse results literal output without the value being replaced.
2021-12-04 21:49:35.598 INFO 27293 --- [ main] c.s.auto.expanson.demo.DemoApplication : Started DemoApplication in 1.097 seconds (JVM running for 2.105)
#expansion.property#

Related

JavaFX runtime components are missing error in Java Gradle Project

I started a Gradle project with java and I wanted to add JavaFx Library but when I run my application I'd got "JavaFX runtime components are missing, and are required to run this application".
This is my Gradle file:
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
group 'org.personal'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
javafx {
version = "15.0.1"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
test {
useJUnitPlatform()
}
And this is the main class:
package client;
import javafx.application.Application;
import javafx.stage.Stage;
public class ClientApp extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
}
}
And this is my "Edit Configuration": Edit Configuration here
you need to add the JavaFX JDK libs to the path. Otherwise, you need to use JDK 8 (JDK1.8) which integrates the JavaFX SDK

Why am I getting 404 status on running a simple spring boot gradle project?

I am new to spring-boot. I have created a simple spring-boot version 2.2.6 project with gradle build. I have created a welcome jsp just to print a header. I get 404 status when i run it on my server with http://localhost:8081/welcome.html Following is my build.gradle, application class, controller class and application.properties file:
//build.gradle
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.banuprojects'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
implementation 'javax.servlet:jstl'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
//application class
package com.banuprojects.lmsdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class LmsdemoApplication {
public static void main(String[] args) {
SpringApplication.run(LmsdemoApplication.class, args);
}
}
//controller class
package com.banuprojects.lmsdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TestController {
#RequestMapping("/welcome.html")
public ModelAndView firstPage() {
return new ModelAndView("welcome");
}
}
//application.properties
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
I am not sure where I have one wrong with the implementation. Any help will be appreciated.
Thank you
As discussed over the comments. After seeing the application properties file, found that wrong port was passed in the URL.
No other technical error found.

A component required a bean named 'org.springframework.boot.context.internalConfigurationPropertiesBinder' that could not be found

I have a spring boot multi-module project with Gradle. While running the application i am getting the below error:-
Description:
A component required a bean named 'org.springframework.boot.context.internalConfigurationPropertiesBinder' that could not be found.
Action:
Consider defining a bean named 'org.springframework.boot.context.internalConfigurationPropertiesBinder' in your configuration.
The project has a root project which has a subproject 'GetAssociationService'
rootproject - build.gradle
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'com.github.johnrengelman.shadow' version '5.2.0'
}
bootJar { enabled = false }
jar { enabled = true }
subprojects {
group = 'org.qmetech'
version = '1.0.0'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
}
repositories { mavenCentral ( ) }
ext { set ( 'springCloudVersion' , "Hoxton.SR3" ) }
dependencies {
testImplementation ( 'org.springframework.boot:spring-boot-starter-test' ) {
exclude group: 'org.junit.vintage' , module: 'junit-vintage-engine'
}
}
ext.libraries = [
commonlibraries: [ 'org.springframework.boot:spring-boot-starter:2.2.5.RELEASE' ,
'org.springframework.cloud:spring-cloud-function-context' ,
'org.springframework.cloud:spring-cloud-function-adapter-aws:2.0.1.RELEASE' ,
'com.amazonaws:aws-lambda-java-log4j:1.0.0' ,
'org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE' ,
'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE' ,
'org.springframework.boot.experimental:spring-boot-thin-layout:1.0.11.RELEASE' ,
'org.springframework.boot:spring-boot-starter-data-mongodb:2.2.5.RELEASE',
'org.springframework.cloud:spring-cloud-function-compiler:2.0.0.RELEASE'] ,
]
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test { useJUnitPlatform ( ) }
ChildProject - build.gradle
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
springBoot {
mainClassName = 'org.qmetech.GetAssociationService'
}
dependencies {
dependencies {
compile libraries.commonlibraries
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}
}
UserRepository.java
package org.qmetech.repository;
import org.qmetech.domain.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface UserRepository extends MongoRepository<User, Integer> { }
AssociationService.java
package org.qmetech.service;
import org.qmetech.domain.User;
import org.qmetech.repository.UserRepository;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.function.Function;
#Component
public class AssociationService implements Function<String, List<User>> {
private final UserRepository userRepository;
public AssociationService(UserRepository userRepository) {
this.userRepository = userRepository;
}
#Override
public List<User> apply(String uppercaseRequest) {
List<User> users = userRepository.findAll();
return users;
}
}
The Complete Code can be found here - https://github.com/iftekharkhan09/Services
Can Anyone please tell me what am I doing wrong?
Thanks!
I also had this problem and in my case, there was the problem in 'org.springframework.boot' version. I changed my version to 2.1.6 Release. Then the problem resolved.
Please verify your pom.xml and make sure you are using the matching version of the Spring boot framework for Spring Cloud.
e.g. For Spring Cloud Greenwich you will need Spring boot 2.1.x
Here is Spring cloud documentation : https://spring.io/projects/spring-cloud
Revisit section : Release Trains

cannot access org.springframework.web.WebApplicationInitializer

I am currently working through the example on scheduling tasks using Spring 4, Java 1.8, and Gradle (https://spring.io/guides/gs/scheduling-tasks/).
However, when running this example, I receive the following error:
Error:(11, 8) java: cannot access org.springframework.web.WebApplicationInitializer
class file for org.springframework.web.WebApplicationInitializer not found
The source code to my Application.java is as follows:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
#SpringBootApplication
#EnableScheduling
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
My gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
jar {
baseName = 'gs-scheduling-tasks'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter")
testCompile("org.springframework.boot:spring-boot-starter-test")
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}
Why is org.springframework.web.WebApplicationInitializer causing this error when it is not even mentioned in the guide?
Thanks.
Add this dependency to fix this problem. I perfectly worked for me.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The guide does not extend SpringBootServletInitializer, remove it or add spring-boot-starter-web as a dependency if you want to start a Tomcat webserver inside you application.
You are extending SpringBootServletInitializer which implements WebApplicationInitializer
check http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.boot/spring-boot/1.0.2.RELEASE/org/springframework/boot/context/web/SpringBootServletInitializer.java
Use this Application class instead as the guide is showing
#SpringBootApplication
#EnableScheduling
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}

Spring MVC won't load static content

I am new to Spring and i want to load some css and js into a view.
The static content is now in src/main/resources/static/... but when i navigate to localhost:8080/css/test.css it gives me a 404 error..
I don't use any xml configuration files.
My project structure:
Update: Application class
package com.exstodigital.photofactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Update: build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
group 'PTS4'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
testCompile("junit:junit")
}
Some controller (just testing stuff):
package com.exstodigital.photofactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
#Controller
//#Controller
public class MainController {
//#ResponseBody
#RequestMapping("/greeting/{name}/{hoi}")
public String greeting(#PathVariable("name") String name, #PathVariable("hoi") String hoi) {
//model.addAttribute("message", "BERICHT");
return name;
}
#RequestMapping("/test")
public String test() {
return "test";
}
}
Your structure is correct. Don't forget to make gradle clean task before run bootRun.

Categories

Resources