Assumes that your Java, Maven, and eclipse are set up as per: Setting up Java, Maven, and eclipse
Step 1: Create a simple maven project using the maven “archetype” plugin.
1 2 | mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simple-spring-boot -DinteractiveMode=false |
The above command creates a folder “simple-spring-boot”
Step 2: import it into eclipse by choosing File –> Import –> Maven –> Existing Maven projects and select the folder “simple-spring-boot” you had just created with Maven.
Check “Maven Dependencies“:
Step 3: Add Spring boot dependency “spring-boot-starter-actuator“. The actuators enable production-ready features like health, metrics, info, dump, env etc to a Spring Boot application – without having to actually implement these things yourself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <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.mytutorial</groupId> <artifactId>simple-spring-boot</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>simple-spring-boot</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project> |
Note: The “parent” tag and the “dependency” tags having spring boot references.
What if you want to extend a different company level parent project?
You can use the Spring IO BOM (Bill Of Material) config
Note: dependency “spring-boot-dependencies” and the plugin “spring-boot-maven-plugin” with “repackage” goal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <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.mytutorial</groupId> <artifactId>simple-spring-boot</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>simple-spring-boot</name> <url>http://maven.apache.org</url> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <!-- Build an executable jar --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.mytutorial.App</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.4.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
Step 4:Run the “mvn package” command.
1 2 3 | c:/projects/simple-spring-boot> mvn package |
Check “Maven Dependencies“: “spring-boot-starter-actuator” has added
May have to click “F5” within eclipse to refersh the projects.
Step 5: Modify the “App.java” as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.mytutorial; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main( String[] args ) { System.out.println( "Hello Spring Boot!" ); } } |
Step 6: To run the jar file, you need to create a “META-INF/MANIFEST.MF” file. Using the maven jar plugin to generate the “MANIFEST.MF” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <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.mytutorial</groupId> <artifactId>simple-spring-boot</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>simple-spring-boot</name> <url>http://maven.apache.org</url> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <!-- Build an executable JAR --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.mytutorial.App</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
Note in pom.xml: “<mainClass>com.mytutorial.App</mainClass>”
1 | <mainClass>com.mytutorial.App</mainClass> |
Step 7: Ready to build the “jar” now.
1 2 3 | c:/projects/simple-spring-boot> mvn package |
The “target” directory will have the jar file “simple-spring-boot-1.0-SNAPSHOT.jar”
Step 8: Deploy the very simple Spring boot application & run as shown below
right-click on the jar, and then select “properties” to get the full file path of the jar file.
“c:/projects/simple-spring-boot/target/simple-spring-boot-1.0-SNAPSHOT.jar”
1 2 3 | c:/projects/simple-spring-boot>java -jar c:/projects/simple-spring-boot/target/simple-spring-boot-1.0-SNAPSHOT.jar |
Output:
Hello Spring Boot!
Conclusion:
If you look at the packaged “simple-spring-boot-1.0-SNAPSHOT.jar” with
1 2 | jar -tvf target/simple-spring-boot-1.0-SNAPSHOT.jar |
you will get
Java does not provide any standard way to load nested jar files, so we use “uber” jars. An uber jar simply packages all classes, from all jars, into a single archive. The following plugin creates an uber jar.
1 2 3 4 5 6 7 | <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.4.RELEASE</version> </plugin> |
In the next tutorial I will expand this to a simple “RESTful” web service. Simple Spring Boot Restful Web Service Tutorial