Tuesday 10 July 2018

Execute spring project using maven

In this post, you are going to learn
a.   How to create simple maven java project
b.   Add dependencies in pom.xml
c.   Write Hello World spring boot application.
d.   Run the spring boot application using maven

Step 1: How to create simple maven java project?
Maven provides number of archetypes to create project skeltons.

For example,
maven-archetype-quickstart : Generate simple maven project.
maven-archetype-j2ee-simple : Generate simple j2ee application.

Open command prompt and execute the command "mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart"

When you execute above command, maven asks groupId, artifactId, version, and package information and create a simple project.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[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 Interactive mode
Define value for property 'groupId': org.selflearningjava
Define value for property 'artifactId': HelloWorld
Define value for property 'version' 1.0-SNAPSHOT: : 1
Define value for property 'package' org.selflearningjava: : com.sample.app
Confirm properties configuration:
groupId: org.selflearningjava
artifactId: HelloWorld
version: 1
package: com.sample.app
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\krishna\Documents\Study\Spring Boot\examples\projects
[INFO] Parameter: package, Value: com.sample.app
[INFO] Parameter: groupId, Value: org.selflearningjava
[INFO] Parameter: artifactId, Value: HelloWorld
[INFO] Parameter: packageName, Value: com.sample.app
[INFO] Parameter: version, Value: 1
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\krishna\Documents\Study\Spring Boot\examples\projects\HelloWorld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:51 min
[INFO] Finished at: 2018-04-20T14:44:35+05:30
[INFO] ------------------------------------------------------------------------

Project structure looks like below.




package com.sample.app;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

Step 2:  Add dependencies in pom.xml
Pom.xml file will be located under the project directory. Add below maven dependencies to pom.xml.

         <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>2.0.1.RELEASE</version>
         </parent>

         <dependencies>

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

         </dependencies>

Final pom file looks like below.

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>org.selflearningjava</groupId>
  <artifactId>HelloWorld</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>HelloWorld</name>
  <url>http://maven.apache.org</url>
  
  <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.0.1.RELEASE</version>
  </parent>
 
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
  </dependencies>
  
</project>

Step 3: Write Hello World spring boot application

Update App.java like below.


App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class App 
{
 @RequestMapping("/")
 @ResponseBody
 String home() {
  return "Hello World!";
 }

 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}

Step 4: Run the spring boot application using maven
Go to the directory, where pom.xml file is located and execute the command ‘mvn spring-boot:run’.


You can able to see that, maven start the tomcat server and deploy the application.

[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) @ HelloWorld ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)
 ..........
 ..........
 ..........
2018-04-20 14:55:54.251  INFO 11412 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-20 14:55:54.262  INFO 11412 --- [           main] com.sample.app.App                       : Started App in 3.772 seconds (JVM running for 9.045)

Once the application started, open the url ‘http://localhost:8080/’ in browser, you can able to see ‘Hello World’ message


Previous                                                 Next                                                 Home

No comments:

Post a Comment