Spring Boot JAR built with Maven using requiresUnpack not working - java

I think I have been running into the following issue Jersey doesn't always work with Spring Boot fat jars. The workaround should be to set the Jersey dependencies in the POM to requiresUnpack.
My POM looks like this:
<?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>net.hagstrom</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.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-jersey</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>
<configuration>
<requiresUnpack>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</dependency>
</requiresUnpack>
</configuration>
<version>1.4.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
But I still get the following error when I try to run the JAR that I built with mvn package:
2017-01-13 10:44:28.229 ERROR 9289 --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jerseyConfig' defined in URL [jar:file:/home/mikael/Dev/Java/Java%20Programs/springBootDemo/target/demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/net/hagstrom/JerseyConfig.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.hagstrom.JerseyConfig]: Constructor threw exception; nested exception is org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /home/mikael/Dev/Java/Java Programs/springBootDemo/target/demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory)
Running the JAR that I built in IDE with Artifacts works just fine.
Is there something wrong in my POM or the way I build the JAR with Maven?

This might have been fixed already. I have published a couple of blog posts related to Creating APIs using Spring Boot, Jersey 2 and Docker and documenting them using Swagger available at: http://tech.asimio.net/2016/04/05/Microservices-using-Spring-Boot-Jersey-Swagger-and-Docker.html and http://tech.asimio.net/2016/05/07/Documenting-multiple-REST-API-versions-using-Spring-Boot-Jersey-and-Swagger.html, both with accompanying source code and I didn't need to unpack and repackage Jersey 2 dependencies.
On the other hand, when I was working on the accompanying source code for another blog about Services Registration and Discovery using Spring Cloud, Eureka, Ribbon and Feign, I was integrating Spring Boot and Jersey 1 (Jersey 1 doesn't have a Spring Boot starter I do recall to work-around unpackaging Jersey 1 dependencies a needed to create a multi-module Maven project for that specific API service.

The problem is that Jersey cannot scan classes in the new "fat boot jar". This occurs when you try to use the packages("some.package.to.scan") method of the ResourceConfig class.
However, you can achive the same effect using Spring classpath scanning facilities. This way you can scan a package similarily to config.packages():
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Provider.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(Path.class));
config.registerClasses(scanner.findCandidateComponents("your.package.to.scan").stream()
.map(beanDefinition -> ClassUtils.resolveClassName(beanDefinition.getBeanClassName(), config.getClassLoader()))
.collect(Collectors.toSet()));
Note: please have a look at the source of org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener. This is the stock solution and you can see that it does the same: it scans for classes annotated with #Path or #Provider (but doesn't manage to find anything because of the broken scanning mechanism).
(Using the older version of the boot plugin worked for me too, but I tried to avoid it.)

I found the issue.
Spring Boot 1.4 changed the internal Jar Structure to facilitate the
Spring Boot bootstrap process.
https://github.com/spring-projects/spring-boot/issues/1468#issuecomment-267357809
You can leave this version 1.4.3:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
But spring-boot-jersey fat-jar to be executable without errors it`s need to downgrade plugin version to 1.3.8 like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.8.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

Related

Custom dependencies are not identified in Spring-Boot

I have two spring-boot project
greeter-library
greeter-spring-boot-autoconfigure
I have created jar file for greeter-library and installed that in my local m2(maven) repository.
Now I am using that jar as a maven dependency in greeter-spring-boot-autoconfigure.But it is stating
Class not found on Greeter.java.
pom.xml for greeter-library
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>greeter-library</artifactId>
<groupId>com.xyz.greeter</groupId>
<version>0.0.1-SNAPSHOT</version>
<name>greeter-library</name>
<dependencies>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Now pom.xml for greeter-spring-boot-autoconfigure is as follows
<artifactId>greeter-spring-boot-autoconfigure</artifactId>
<name>greeter-spring-boot-autoconfigure</name>
<groupId>com.xyz</groupId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<artifactId>greeter-library</artifactId>
<groupId>com.xyz.greeter</groupId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
But during maven build time for greeter-spring-boot-autoconfigure, it is stating that Greeter.java not found which is part of greeter-library project.
Can anyone has any solution to this?
Since you've added spring-boot-maven-plugin in "greeter-library" module, it will be packaged as a spring boot application. Open it with WinRar/WinZip and you'll see. This is a little bit confusing, but in general spring boot application, although is packed as a JAR is not actually a jar in the sense that JVM can't load it, can't find its classes automatically, etc. For example, it has its dependencies in BOOT-INF/lib folder - this is not the way regular jars work, in fact, spring boot has a bootstrapping code that uses custom class loaders to read the classes from such a structure.
As a consequence of all this, Maven and IDE won't be able to recognize the classes from the greeter-library hence the error. Bootom line, you can't really declare a dependency on a spring boot application from your greater-spring-boot-autoconfigure module.
Now as a solution - why do you need a greeter-library to be a spring boot artifact? Maybe if you just remove the spring-boot-maven-plugin and turn it to the regular jar (with regular dependencies on spring boot infrastructure perhaps) it will work?
If this doesn't help, feel free to share more details in the question to get a more precise solution to the problem...
Some ideas to explore
the first jar "greeter-library" doesnt need to be packed for spring-boot, it can be a normal jar file.
Create a spring.factories file under src/resources/META-INF path. Expose the java classes from this JAR that can be used by "greeter-spring-boot-autoconfigure"

Error: Could not find or load main class Maven Spring Boot Application - Executable JAR

I am trying to run an executable jar but got the following message :
Could not find or load main class
Here is the content of MANIFEST.MF file:
Manifest-Version: 1.0
Created-By: Apache Maven Built-By: Administrator
Build-Jdk: 11.0.3
Class-Path: spring-boot-starter-batch-2.2.0.RELEASE.jar spring-boot-starter-1.5.6.RELEASE.jar spring-boot-1.5.6.RELEASE.jar spring-boot-autocon
figure-1.5.6.RELEASE.jar .......
Main-Class: EAB.ActiveTeam.Exporter.App
I am really stuck as I searched the web, tried many solutions to make my executable JAR run!
Please help, I sent over 5 hours trying to fix with no luck!
here is my POM.xml file:
<?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>EAB.ActiveTeam.Exporter</groupId>
<artifactId>EAB.ActiveTeam.Exporter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EAB.ActiveTeam.Exporter</name>
<description>EAB.ActiveTeam.Exporter</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>EAB.ActiveTeam.Exporter.App</mainClass>
<layout>jar</layout>
<outputDirectory>../libs</outputDirectory>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When it comes to spring boot application you can't create a regular jar file, because spring boot application jar is not a jar at all. It doesn't have an internal directory layout of jar (all dependencies are in BOOT-INF/lib for example) and there are some other subtle differences.
So I believe the issue is with maven here.
Instead of trying to create jar "by youself" and add all the dependencies there you should use spring boot maven plugin:
It "knows" how to create a spring boot jar properly.
By default it will also resolve your "main" file (the one with method main and #SpringBootApplication annotation) however you can specify it in plugin configuration to be on the safe side.
Then you'll be able to the project with :
java -jar <Name_of_your_jar>

The type org.springframework.data.repository.Repository cannot be resolved. It is indirectly referenced from required .class files

I am new for spring Boot, I got this problem when I follow the Spring Guide-Accessing data with MySQL
I got an Error in my STS IDE, I don't know what's the hell happening.
In the src/main/java/UserRepository.java
package hello;
import org.springframework.data.repository.CrudRepository;
import hello.User;
public interface UserRepository extends CrudRepository<User, Long> {
}
STS does warn me about this mistake, I ignore it until I run Cmd: mvn spring-boot
so I am back to check the warning, it is:
The type org.springframework.data.repository.Repository cannot be
resolved. It is indirectly referenced from required .class files
Thanks very much for helping me!
I bulid the project using Maven as recommended,the Pom.xml is:
<?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>org.springframework</groupId>
<artifactId>gs-mysql-data</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Apply some spring boot standards to the code
Keep your boot application on top of the package, in your case your BootApplication should be in hello package. Like shown in below
#SpringBootApplication
public class BootApplication extends SpringBootServletInitializer {
public static void main(String args[]) {
SpringApplication.run(BootApplication.class, args);
}
}
Now keep your repository inside the sub-package of hello. In your case it should be in hello.repository
Now try to run with BootApplication.
Buddy,It doesn't work anyway.This Exception is caused by CrudRepository.The eclipse can't resolve this class.My project is very simple,I get this project from spring website.its guide--Accessing data with MySQL
And now my project is completely same as [gs-accessing-data-mysql-complete]
The Spring official Website think it is successful project,and let the trainer to check [gs-accessing-data-mysql-initial] against [gs-accessing-data-mysql-complete]
.Howerver,even the later can't be build successfully.My project structure:
src
+main
+java
+hello
--Application.java
--MainController.java
--User.java
--UserRepository.java
and the log info ,I had better show you about the digest:
1.[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:run (default-cli) on project gs-mysql-data: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'mainController' defined in file [D:\workbunch\Spring Workbunch\gs-accessing-data-mysql-initial\target\classes\hello\MainController.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [hello.MainController] from ClassLoader [java.net.URLClassLoader#6424f2f4]: org/springframework/data/repository/CrudRepository: org.springframework.data.repository.CrudRepository -> [Help 1]

Spring boot - Why is the exclude required when manually configuring Hibernate?

I have a following Spring boot project which uses Spring data JPA. My rest controller is annotated with the the following annotations-:
#SpringBootApplication
#EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
#ComponentScan({ "com.foo.bar"})
public class RESTService {
My question is why is the exclude parameter required in the #EnableAutoConfiguration ? If I start the application without the exclude I get the following exceptions-:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
Now I am manually configuring Hibernate from within my project.
My reasoning is that since Spring Boot sees spring data on the class path it tries to autoconfigure JDBC and Hibernate JPA. But then why isn't it trying to autoconfigure Mongo or any other data base solution ?
Can someone please help me to understand what is going on here ?
My POM file is-:
<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.foo.bar</groupId>
<artifactId>Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>REST Service</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.1.0</version>
</dependency>
<!--
<dependency>
<groupId>org.fosstrak.epcis</groupId>
<artifactId>epcis-repository</artifactId>
<version>0.5.0</version>
</dependency>
-->
<dependency>
<groupId>org.fosstrak.epcis</groupId>
<artifactId>epcis-repository</artifactId>
<version>0.5.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/epcis-commons-0.5.0.jar</systemPath>
</dependency>
</dependencies>
<properties>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.foo.bar.RESTService</mainClass>
<addResources>true</addResources>
<layout>JAR</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Spring only autoconfigures Mongo if you have the respective starter dependency in your classpath.
For example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
Regarding the Exception you postet, it says that you havn't configured your Databasedriver. You need someting along those lines in your properties:
spring.datasource.driver-class-name: oracle.jdbc.pool.OracleDataSource
spring.datasource.url: jdbc:oracle:thin:#<host>:1521:<schema>
spring.datasource.username: <user>
spring.datasource.password: <password>
Depending on what Database you use of course.
Based on your pom.xml I assume you are using Postgres. Make sure that in the application.properties file you configured your data source. Exception may suggest it is misconfigured.
spring.datasource.url=jdbc:postgresql://localhost/testdb
spring.datasource.username=postgres
spring.datasource.password=123

Can I Add SpringBoot Framework to Existing Maven Project

I have a Maven Project in Eclipse and now I need to add database connectivity. My textbook did all json tutorials in Maven. Now in this chapter on JDBC they are using SpringBoot.
Can I convert the project to SpringBoot? Or start a SpringBoot and import my previous Maven classes.
Here is described how to use maven for a SpringBoot Project.
You will need to modify your existing pom.xml to add something like this to make it a SpringBoot Project:
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Convert Maven Project to SpringBoot
Ist option :Spring Boot Using parent POM
Add following dependencies in pom.xml file**:-
1.) Inheriting the starter parent(spring-boot-starter-parent): spring-boot-starter-parent is a special starter that provides useful Maven defaults.
2.) spring-boot-starter-web :- Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
3.) spring-boot-maven-plugin:- Spring Boot includes a Maven plugin that can package the project as an executable jar.
here is pom.xml :-
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2nd option:Using Spring Boot without the Parent POM
Not everyone likes inheriting from the spring-boot-starter-parent POM. You may have your own corporate standard parent that you need to use or you may prefer to explicitly declare all your Maven configuration.
If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the dependency management (but not the plugin management) by using a scope=import dependency, as follows:
< dependency> < !-- Import dependency management from Spring Boot -->
< groupId>org.springframework.boot< /groupId>
< artifactId>spring-boot-dependencies< /artifactId>
< version>2.0.0.BUILD-SNAPSHOT< /version> < type>pom< /type>
< scope>import< /scope> < /dependency>
Fore more details find here in Spring Boot Docs:- https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#getting-started-maven-installation
1) Add Spring boot starter Parent and Web in Pom.xml file
2) Add #SpringBootApplication in the main class
3) Add SpringApplication.run(App.class, args); in main method.
Yes you can use Springboot with Maven as dependency management.
You will have to add spring boot dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Also add #SpringBootApplication and #EnableAutoConfiguration in the main class
I will face this question in two more interviews so it can help full your self also.
Simply right-click on your project select "Configure" and choose on "convert to Maven Project" then it will create or application as maven and .

Categories