Create a Java Project using Maven Archetypes

In this article, we will learn How to create a simple java project, java web project, and how to convert simple java project into spring boot applications.

Java Projects

A Java project also maintains a model of its contents. This model includes information about the type hierarchy, references, and declarations of Java elements. This information is constantly updated as the user changes the Java source code. The updating of the internal Java project model is independent of the Java builder; in particular, when performing code modifications, if auto-build is turned off, the model will still reflect the present project contents.

Maven Archetypes

An archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The name fits as we are trying to provide a system that provides a consistent means of generating Maven projects.

It can be generated by using the following command on windows/Linux etc

mvn archetype: generate

But before executing this command we need Java and Maven in our machine.

1) Creating a simple java project

  • Open eclipse in
  • In File ->New->Other.
  • Select the Maven project.
create maven project

  • Select Next and Next
  • Then we Have page select an Archetype


In filter put value quickstart

  • in search, result scroll down and select on Archetype having
  • groupId = org.pache.maven.archetypes
  • artifact Id = maven-archetype-quickstart

  • Click next

we have Screen New Maven Project Enter the details

  • Group Id - com.javacodestuff a unique ID for an organization, or a project (an open-source project, for instance
  • Artifact Id- my-java-app Name of the project you want to create
  • Version - 1.0 version of the project or when you create a jar or war file you can distinguish files using version package - com.javacodestuff top-level package Name of the project.
  • Click Finish.

our project looks like 


The project contains a pom.xml file where we add maven dependencies as required in project development the eclipse will creating a maven project of given details.

Right-click on project Run As- Java Application

we get the output as 

    
Hello World!  

2) Creating a maven web project

Open eclipse in

  • file ->New->Other.
  • Select the Maven project.
  • Select Next and Next

Then we Have page select an Archetype

in filter put value web

  • In search, result scroll down and select on Archetype having
  • groupId = org.apache.maven.archetypes
  • artifact Id = maven-archetype-webapp
  • Click next


we have Screen New Maven Project

Enter the details

  • Group Id - com.javacodestuff a unique ID for an organization, or a project (an open-source project, for instance
  • Artifact Id- my-web-app Name of the project you want to create
  • Version - 1.0 version of the project or when you create jar or war file you can distinguish files using version
  • package - com.javacodestuff top-level package name of the project.

Click Finish



3) Converting Maven Project to Spring boot project

There may be situation comes, we need to migrate our web application to spring boot application. 

Here are the steps to convert a simple java project to Spring boot web project.

Open pom.xml of our project add dependencies like

    
 
   <parent>
		<groupid>org.springframework.boot</groupid>
		<artifactid>spring-boot-starter-parent</artifactid>
		<version>2.3.0.RELEASE</version>
		<relativepath> <!--lookup parent from repository-->
	</relativepath></parent>
    
  <dependency>
			<groupid>org.springframework.boot</groupid>
			<artifactid>spring-boot-starter-web</artifactid>
		</dependency>

  
 

At The bottom of pom.xml remove all between the tags enter spring-boot plugin like

 


<build>
		<plugins>
			<plugin>
				<groupid>org.springframework.boot</groupid>
				<artifactid>spring-boot-maven-plugin</artifactid>
			</plugin>
		</plugins>
	</build>

 

In our main class .i.e in App.java remove all details and put code like:

 
package com.javacodestuff;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties
public class App {

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(App.class);
		app.run(args);

	}
}

Create package com.javacodestuffs.controller add Class HelloController.java In that put code like

 
package com.javacodestuff.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@GetMapping("/hello")
	public String sayHello()
	{
		return "Welcome !!! You Successfully created spring boot web Application.";
	}	
}

Our simple java project is converted to a spring boot project

Create a folder inside src/main as resources create a new file inside it application.properties put content inside it as

   
  server.port=9090
  
  

It will overrides spring boot applications default port 8080 to 9090 and our spring boot Application will run on port 9090

Right Click on Project-Run As -> Java Application or

open terminal or command prompt on the location of the project

run command as

mvn spring-boot:run

when you get output on the console like

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

2020-06-07 21:49:40.348  INFO 25563 --- [           main] com.javacodestuff.App                    : Starting App on devuser-Inspiron-3542 with PID 25563 (/media/devuser/my-java-app/target/classes started by devuser in /media/devuser/E0BA6AFDBA6AD018/javacodestuffs/my-java-app)
2020-06-07 21:49:40.354  INFO 25563 --- [           main] com.javacodestuff.App                    : No active profile set, falling back to default profiles: default
2020-06-07 21:49:44.910  INFO 25563 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2020-06-07 21:49:45.025  INFO 25563 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-06-07 21:49:45.026  INFO 25563 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-06-07 21:49:47.047  INFO 25563 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-06-07 21:49:47.048  INFO 25563 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 6543 ms
2020-06-07 21:49:48.147  INFO 25563 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-07 21:49:49.350  INFO 25563 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2020-06-07 21:49:49.407  INFO 25563 --- [           main] com.javacodestuff.App                    : Started App in 10.22 seconds (JVM running for 11.585)
2020-06-07 21:54:28.311  INFO 25563 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-07 21:54:28.312  INFO 25563 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-06-07 21:54:28.401  INFO 25563 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 89 ms
 

You Spring boot Web Application is Running on port 9090

Go to browser enter the URL

   
http://localhost:9090/hello 
 

You will see our message, we configured in the controller


We learned how to create a maven project using different archetypes. Also, we learned how to convert a simple java project to a spring boot web project. All source code in the article can be found in the GitHub repository