Setup Spring Boot 2 project in Eclipse using Maven

In this particular article we will see How to get started with Spring Boot 2 in Eclipse and how to setup a initial Spring Boot 2 project.

Lets take a look at required things before we try to setup a Spring Boot 2 project in Eclipse IDE, following are minimal requirements.

1) Java 8+
2) Maven
3) Eclipse IDE

Setup Spring Boot 2 project in Eclipse using Maven

1) Requirement Testing


1.1) Java: Before we start configuring lets test our system for required tools, make sure you have Java installed in your class path:

$ java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)


1.2) Maven: Maven Now lets test for maven installation, type following command to test if maven is already installed:

$ mvn -version
Apache Maven 3.5.2
Maven home: /usr/share/maven
Java version: 1.8.0_201, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-45-generic", arch: "amd64", family: "unix"


1.3) Eclipse: Make sure you have a working Eclipse IDE with you.

2) Create a simple Java project with maven

Type following command in command line as shown below:

$ mvn archetype:generate -DgroupId=com.tb.java -DartifactId=SpringBoot2Maven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Now you should see a BUILD SUCCESS massage something like this:


[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] 
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/techie/eclipse-workspace
[INFO] Parameter: package, Value: com.tb.java
[INFO] Parameter: groupId, Value: com.tb.java
[INFO] Parameter: artifactId, Value: SpringBoot2Maven
[INFO] Parameter: packageName, Value: com.tb.java
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/techie/eclipse-workspace/SpringBoot2Maven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.848 s
[INFO] Finished at: 2019-02-28T15:56:56+05:30
[INFO] Final Memory: 18M/238M
[INFO] ------------------------------------------------------------------------

3) Make maven project compatible to eclipse

By default maven projects are not compatible to eclipse, just go inside the project directory and supply following command to make it compilable to Ecllipse IDE.

$ cd SpringBoot2Maven
$ mvn eclipse:eclipse
If everything goes right, you should see something like below on your console:


[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building SpringBoot2Maven 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ SpringBoot2Maven >>>
[INFO] 
[INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ SpringBoot2Maven <<<
[INFO] 
[INFO] 
[INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ SpringBoot2Maven ---
[INFO] Using Eclipse Workspace: /home/techie/eclipse-workspace
[INFO] no substring wtp server match.
[INFO] Using as WTP server : Apache Tomcat v8.0
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "SpringBoot2Maven" to /home/techie/eclipse-workspace/SpringBoot2Maven.
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.835 s
[INFO] Finished at: 2019-02-28T15:58:30+05:30
[INFO] Final Memory: 13M/303M
[INFO] ------------------------------------------------------------------------

Here we are done with creating a simple java project in maven.

4) Import project to eclipse

Now lets import this project to Eclipse IDE, open your ide and import the project, using import > existing project in workspace.

5) Spring Boot 2 maven dependencies

Now we have simple Java project imported in eclipse, lets make it a Spring Boot 2 project, add following dependencies to pom.xml.
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.tb.java</groupId>
	<artifactId>SpringBoot2Maven</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>SpringBoot2Maven</name>
	<url>http://maven.apache.org</url>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>repository.spring.release</id>
			<name>Spring GA Repository</name>
			<url>http://repo.spring.io/release</url>
		</repository>
	</repositories>
</project>


6) Download required dependencies

Now we need to download required dependecies mentioned in pom.xml and make them available in eclipse, run below mwntioned command in project directory:

$ mvn eclipse:eclipse


6) Create Spring boot application launch class

/src/main/java/com/tb/java/App.java
package com.tb.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext ctx = SpringApplication.run(App.class, args);
    	System.out.println("Hello world !");
    }
}
Here, @SpringBootApplication does all the magic, a class having main() and annotated with @SpringBootApplication is called Spring boot application launch class.

This annotation is a shortcut of applying 3 annotations in one statement,

1) @SpringBootConfiguration : It simply means that annotated class is a configuration class and shall be scanned for further configurations and bean definitions.

2) @EnableAutoConfiguration : As the name suggest, this annotation is used to auto configure Spring Application Context, Auto-configuration is always applied after user-defined beans are registered. One can always manually exclude any auto-configuration using the spring.autoconfigure.exclude property in properties file as shown below:

@EnableAutoConfiguration(excludeName = {"multipartResolver","mbeanServer"})

3) @ComponentScan : Provides support parallel with Spring XML's element.

7) Run the launch application

Now we are all done with creating and importing a Spring Boot 2 project in Eclipse, lets run the application from application launch class.

If everything goes right, you will see something like this in Eclipse console:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-02-28 16:17:53.987  INFO 16757 --- [           main] com.tb.java.App                          : Starting App on techie-ThinkPad-13-2nd-Gen with PID 16757 (/home/techie/eclipse-workspace/SpringBoot2Maven/target/classes started by techie in /home/techie/eclipse-workspace/SpringBoot2Maven)
2019-02-28 16:17:53.990  INFO 16757 --- [           main] com.tb.java.App                          : No active profile set, falling back to default profiles: default
2019-02-28 16:17:55.379  INFO 16757 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-02-28 16:17:55.413  INFO 16757 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-02-28 16:17:55.413  INFO 16757 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-02-28 16:17:55.424  INFO 16757 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2019-02-28 16:17:55.505  INFO 16757 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-02-28 16:17:55.506  INFO 16757 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1450 ms
2019-02-28 16:17:55.734  INFO 16757 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-02-28 16:17:55.924  INFO 16757 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-02-28 16:17:55.926  INFO 16757 --- [           main] com.tb.java.App                          : Started App in 2.283 seconds (JVM running for 2.581)
Hello world !


We are all done with creating a Spring Boot 2 project using Maven and importing it in Eclipse, in coming articles we will see more about Spring Boot.

Download "Spring Boot 2 project" source code from: GitHub.