SpringBoot: Defining the main class in pom.xml file - spring

I've generated a Spring Boot web application using Spring Initializer, embedded Tomcat, Thymeleaf template engine.Technologies used: Spring Boot 1.4.2.RELEASE, Spring 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, Tomcat Embed 8.5.6, Maven 3, Java 8.
I have this pom.xml file,
and I generate the war using this command
mvn clean package -DskipTests -Dspring.profiles.active=pebloc,war -DAPPKEY=pebloc
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bookcloud</groupId>
<artifactId>bookcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>bookcloud</name>
<description>Book Cloud </description>
<profiles>
<profile>
<id>jar</id>
<properties>
<spring.boot.mainclass>com.bookcloud.iot.BookCloudApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>war</id>
<properties>
<spring.boot.mainclass>com.bookcloud.iot.BookCloudApplicationWar</spring.boot.mainclass>
</properties>
</profile>
</profiles>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>spring-boot-email-core</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>spring-boot-thymeleaf-email</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.icegreen/greenmail -->
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.5.3</version>
<optional>true</optional>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>${spring.boot.mainclass}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
but I still got this error:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project bookcloud: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.bookcloud.iot.BookCloudApplication, com.bookcloud.iot.BookCloudApplicationWar] -> [Help 1]
BookCloudApplication.java
#Profile("!war")
#SpringBootApplication
#Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class})
public class BookCloudApplication {
public static void main(String[] args) {
SpringApplication.run(BookCloudApplication.class, args);
}
}
BookCloudApplicationWar.java
#Profile("war")
#Import({SecurityConfig.class ,PersistenceConfig.class})
#SpringBootApplication
public class BookCloudApplicationWar extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(BookCloudApplicationWar.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(BookCloudApplicationWar.class, args);
}
}

Maven profiles has no relation to Spring profiles.
You have two Maven profiles: jar, war
You either use:
mvn clean package -Pjar
or
mvn clean package -Pwar
The error you are getting is while building the artifact because pom needs:
<start-class>your.app.entry.point.Main</start-class>
inside the properties element.
I would suggest to have only one start class regardless of the Maven profile.
something like:
pom.xml:
...
<modelVersion>4.0.0</modelVersion>
<groupId>com.asimio.cloud</groupId>
<artifactId>zuul-server</artifactId>
<version>0-SNAPSHOT</version>
<packaging>${packaging.type}</packaging>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>your.app.entry.point.Main</start-class>
<packaging.type>jar</packaging.type>
...
<profiles>
<profile>
<id>war</id>
<properties>
<packaging.type>war</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
...
</profile>
</profiles>
...
Main.java:
package your.app.entry.point;
...
#SpringBootApplication
#Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class})
public class App extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
No need for Spring profiles. This artifact can now be built using either Maven profile and will run either using:
java -jar .....
when building this artifact as a jar
or
as a war file deployed to a servlet container when built using the -Pwar Maven profile.

Maven profiles and Spring profiles are two different concepts.
You should also run Maven using the war profile, in order for the spring.boot.mainclass property to be set with the correct matching value.
Add -Pwar to the command line in order to activate the war Maven profile.
mvn clean package -DskipTests -Pwar -Dspring.profiles.active=pebloc,war -DAPPKEY=pebloc

Related

spring boot #repository bean from Maven dependency not found

I'm working on multiple Spring Boot projects using spring-data-mongoDB (w/o JPA). One that contains the common (udc-common) components, repositories and services and others (eg udc-gateway) that use these components. the udc-common library is declared as a maven dependency in the other projects.
(you'll find enclosed the maven detailed configurations at the end of this post)
Environment structure
udc_common project
udc_common.models
udc_common.repositories
udc_common.services
udc_common.interfaces
udc_gateway project
udc_gateway.controllers
udc_gateway ....
gatewayApplication.java
gateway pom.xml
<dependency>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
in the udc-gateway project I have a rest controller thats refers to a udc-comm SampleService. When running it I face an error
Parameter 0 of constructor in org.open_si.udc_common.services.SampleService required a bean of type 'org.open_si.udc_common.repositories.SampleRepository' that could not be found.
the coding organization is :
the controller (SamplesController) belongs to the gateway project
the service (SampleService) and its repository (SampleRepository) belong to the common project being declared as maven dependency in the gateway project
Obviously the SampleService injection does work but its SampleRepository dependency doesn't. As the common package is an external one I've set the #componentscan accordingly in the gateway app main class
tryed
#ComponentScan("org.open_si.udc_common")
and
#ComponentScan({
"org.open_si.udc_common.repositories",
"org.open_si.udc_common.services"
})
the SamplesController (gateway project) code excerpt related for the SampleService is
#RestController
#RequestMapping("/samples")
public class SamplesController {
#Autowired
private SampleService service;
......
the SampleService (common project) is
#Service
#EnableMongoRepositories
public class SampleService {
#Autowired
private SampleRepository sr;
void insert(BaseSampleEntity sample) {
this.sr.insert(sample);
}
public void delete(BaseSampleEntity sample) {
this.sr.delete(sample);
}
.....
or also tryed
#Service
#EnableMongoRepositories
public class SampleService {
private final SampleRepository sr;
public SampleService(SampleRepository repository) {
sr = repository;
}
void insert(BaseSampleEntity sample) {
this.sr.insert(sample);
}
public void delete(BaseSampleEntity sample) {
this.sr.delete(sample);
}
and the SampleRepository (common project) is
// #Repository (JPA only)
public interface SampleRepository extends MongoRepository<BaseSampleEntity, String> {
List<BaseSampleEntity> findByNodeUuid(String Uuid, Sort sort, Pageable pageable);
List<BaseSampleEntity> findByFieldUuid(String Uuid, Sort sort, Pageable pageable);
so the raised exception
Parameter 0 of constructor in org.open_si.udc_common.services.SampleService required a bean of type 'org.open_si.udc_common.repositories.SampleRepository' that could not be found.
leads me thinking that something is wrong in the spring boot IOC process. Any idea ?
thanks in advance for your help.
udc-common pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>UDC common</name>
<packaging>jar</packaging>
<description>Common resources for Universal Data Collector</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
udc-gateway pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.open_si</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Gateway</name>
<description>Gateway for UDC project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-oauth2-client</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Try adding below annotation in the spring boot main application class (ie. class annotated with SpringBootApplication - GateWayApplication.java)
#EnableMongoRepositories(basePackages = "org.open_si.udc_common")
#ComponentScan("org.open_si.udc_common.services")
#EntityScan(basePackages ="org.open_si.udc_common.models")
Also, as you mentioned you don't need to annotate repository class with #Repository as this is already managed by spring data
UPDATE:
#SpringBootApplication itself has ComponentScan annoation embedded to it, so it automatically scans all the classes under all the subdirectories.
Hope you have the directory structure somewhat similar to
com.example
--controllers
-- SamplesController
--service
-- SampleService
SampleApplication (this is the class that contains #SpringBootApplication & #EnableMongoRepositories)
Mb you need to add the Spring Data dependency to Maven
As suggested by Kumar V adding
#ComponentScan("org.open_si.udc_common.services")
#EnableMongoRepositories(basePackages = {"org.open_si.udc_common.repositories"})
In the gateway.application.java did the trick.
Unfortunately due to the org.open_si.udc_common.services import, I faced a new issue :
when sending a request against the SamplesController I get an HTTP 404 error.
When removing the ComponentScan of udc-common-services (and the reference to the SampleService in the controller) everythink is ok.
Springs does make some stuff under the wood that I don't understand.

SpringBootBackendApplication.java:3: error: package org.springframework.boot does not exist

When trying to run SpringBoot application, I get the following errors:
SpringBootBackendApplication.java:3: error: package org.springframework.boot does not exist
import org.springframework.boot.SpringApplication;
^
SpringBootBackendApplication.java:4: error: package org.springframework.boot.autoconfigure does not exist
import org.springframework.boot.autoconfigure.SpringBootApplication;
^
SpringBootBackendApplication.java:6: error: cannot find symbol
#SpringBootApplication
^
symbol: class SpringBootApplication
This also happened with another project of mine. Does the project have to be run with Maven? If not, what could be causing an error such as this? Is the code even supposed to be executable in its current state?
My Pom File:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ourlibrary</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-backend</name>
<description>Our Library </description>
<properties>
<java.version>11</java.version>
</properties>
<properties>
<start-class>com.ourlibrary.springboot.Application</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My Main Method:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootBackendApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootBackendApplication.class, args);
}
}

Spring Web-mvc not working with Spring Boot[Spring cloud]

I am new to microservices.I have created a microservice application using Spring.I am using "spring-cloud-starter-eureka-server".I have total 3 modules:
microservicecldm
account-microservice
webclient-microservice-server
Find below their main classes accordingly:
#EnableEurekaServer
#SpringBootApplication
public class MicroservicecldmApplication {
public static void main(String[] args) {
SpringApplication.run(MicroservicecldmApplication.class, args);
}
}
#EnableDiscoveryClient
#SpringBootApplication
#ComponentScan(basePackages="com.wmsdm.cloud")
public class WebclientMicroserviceServerApplication{
public static void main(String[] args) {
SpringApplication.run(WebclientMicroserviceServerApplication.class, args);
}
}
In application.properties I have following entries:
spring.application.name=WebclientMicroserviceServerApplication
eureka.client.serviceUrl.defaultZone:http://localhost:9091/eureka/
server.port=9993
eureka.instance.lease-renewal-interval-in-seconds=80
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
spring.mvc.static-path-pattern=/resources/**
I have a AccountController class as follows:
#Controller
public class AccountController {
#RequestMapping(value = "/account", method = RequestMethod.GET)
public String viewAccount() {
return "accountSummary";
}
}
I have the accountSummary.jsp page in my /WEB-INF/view path.Also in maven's pom.xml I have the following dependencies along with other dependencies:
<groupId>com.wmsdm.cloud</groupId>
<artifactId>webclient-microservice-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>webclient-microservice-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Inspite of these when I hit the URL:
http://localhost:9993/WebclientMicroserviceServerApplication/account
I get the error No mapping found for the requested uri
I suggest using spring-boot-starter-web instead of tomcat-embed-jasper. You can use multiple starters in your pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
See spring.io for more info.
Make sure the context component scan base package is correct. If not specified specify it using regex.

Camel-Spring Boot: Route doesn't stop after execution

I am using Spring Boot + Camel with below config.
Main Class
#SpringBootApplication
public class Application extends RouteBuilder {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public void configure() throws Exception {
from("sql:{{list.sql}}?dataSource=#dataSource")
.log("process row ${body}")
.to("stream:out");
}}
application.properties
camel.springboot.name=ListJob
camel.springboot.main-run-controller=true
list.sql=<SELECT QUERY>
spring.datasource.driver=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://IP:3306/test
spring.datasource.username=un
spring.datasource.password=pwd
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc</groupId>
<artifactId>wl-event-notification-batch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>notification</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Camel -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-sql</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My Spring Boot runs the route in infinite loop executing SELECT query because of "camel.springboot.main-run-controller" property until I hit CNTRL+C. If I remove this property, application just starts and stops the route immediately without completing the execution.
Can someone please help me, to get my route start, execute the steps and stop when I run Spring Boot application?
You can use the spring-boot properties:
# handle only one 1 message and then stop the route
camel.springboot.duration-max-messages=1
# run for 180 seconds and then gracefully shutdown
camel.springboot.duration-max-seconds=180
# a polling consumer will cancel the graceful shutdown so set the shutdownTimeout to a minimum
camel.springboot.shutdownTimeout=1
add .stop():
public void configure() throws Exception {
from("sql:{{list.sql}}?dataSource=#dataSource")
.log("process row ${body}")
.stop();
}

Spring Boot on Embedded Tomcat with JSTL still throws java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config

I have an application which leverages PrimeFaces, elastic search,Spring boot.
Following is it's pom.xml,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.acn.hps.aops.demo</groupId>
<artifactId>PSCommandCenter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CommandCentre</name>
<description>Command Center Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.16</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<!-- Primefaces -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>2.3.4</version>
</dependency>
<!-- JSF dependencies -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.8</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Elastic Search dependencies -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>primefaces</id>
<url>http://repository.primefaces.org</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
Following is its spring configuration,
#SpringBootApplication
public class CommandCentreApplication extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(CommandCentreApplication.class, args);
}
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/home.jsf");
}
#Bean
ServletRegistrationBean facesServletRegistration() {
return new ServletRegistrationBean() {
#Override
public void onStartup(ServletContext servletContext)
throws ServletException {
FacesInitializer facesInitializer = new FacesInitializer();
facesInitializer.onStartup(Collections.singleton(CommandCentreApplication.class),
servletContext);
servletContext.setInitParameter("primefaces.THEME", "redmond");
servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "0");
servletContext.setInitParameter("facelets.SKIP_COMMENTS", "true");
}
};
}
#Bean
ServletRegistrationBean pushServletRegistration(){
ServletRegistrationBean pushServlet = new ServletRegistrationBean(new PushServlet(), "/primepush/*");
pushServlet.addInitParameter("org.atmosphere.annotation.packages", "org.primefaces.push");
pushServlet.addInitParameter("org.atmosphere.cpr.packages", "com.acn.hps.aops.demo.map.service.channel");
pushServlet.setAsyncSupported(true);
pushServlet.setLoadOnStartup(0);
pushServlet.setOrder(Ordered.HIGHEST_PRECEDENCE);
return pushServlet;
}
}
and my elastic search's configuration,
#Bean
public Client client(){
TransportClient client= new TransportClient();
TransportAddress address = new InetSocketTransportAddress(PropertiesLoader.esIP, PropertiesLoader.esPort);
client.addTransportAddress(address);
return client;
}
This same application runs as expected when I execute it from my IDE (Eclipse) but when I execute the application from its jar then I am getting the following exception ,
java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_45].
I searched for this exception and I found that jstl's jar is added to avoid this error. But I already have jstl's jar in tomcat's jar. And also if this is the problem then I should be getting the same exception while executing from my IDE.
You can refer this github code which i used as outline for my application. I am facing the same issue in this also.
When using spring-boot-starter as a parent, running
mvn clean install
should produce a fat jar (that means all dependencies are copied into that jar).
More informations can be found here:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html
Maybe this part in the spring-boot-starter plugin within the pom.xml
<configuration>
<executable>true</executable>
</configuration>
is messing with this. Try to remove that part and build the jar again.

Resources