Maven Project Code2java

Maven project in Eclipse

In this blog we would be discussing about one of the ways of creating a Maven project in Eclipse.

Tools Used

The Eclipse I am using in this article is – Eclipse IDE for Enterprise Java and Web Developers (includes Incubating components) Version: 2021-03 (4.19.0) Build id: 20210312-0638.

MAVEN version used 3.5.1

Whenever you open the Eclipse workspace for first time, or when there is no project in your workspace, you will see the shortcut to create multiple types or project (although it may vary depending on the type of eclipse you have).

Create Maven Project Shortcut

Else, you can right click on Project Explorer and create a new Maven Project, provided you already have Maven plugin installed in your eclipse.

Step wise guide:

A. Project Setup

1: Create the new Maven Project in eclipse (make sure you have checked the first check box to create simple project)

2: Enter the artifact details to create the project. However, these can be modified later in your pom.xml file as well

Project Details

3: Project is created with default JRE System Library which we will change in next step 4, as you can see in below image it is J2SE-1.5

Maven Project Created

4: Modify the JRE to the one installed in your system. To do so, go to right click on project, go to Build Path and configure-

Configure Build Path

Make the changes under libraries, select the Alternate JRE or Workspace default JRE. Here we will go with third option.

Configure JRE

B. Code modifications

5: After this, open POM.xml to add some dependencies (similar to adding jar files in build path). You can copy and paste below pom.xml.

Here we have added few sample properties, dependencies and build plugin details

<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>
	<groupId>com</groupId>
	<artifactId>code2java</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Code2JavaMaven</name>
	<description>My first maven project</description>

	<!-- Add one time properties -->
	<properties>
		<!--  This version can be used in dependency, describe once use multiple times -->
		<spring.version>5.2.0.RELEASE</spring.version>
	</properties>
	
	<!-- Add dependencies to be used for your project -->
	<dependencies>
		<!-- Spring MVC Dependency -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- Spring ORM -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>5.0.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>5.0.0</version>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src/main/java</sourceDirectory>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<plugins>
			<!-- Maven Compiler Plugin -->
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<!-- Example for adding Apache Tomcat plugin in order to support web applications -->
			<plugin>
				<groupId>org.apache.tomcat</groupId>
				<artifactId>tomcat</artifactId>
				<version>9.0.5</version>
				<configuration>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

6: Add new class “MyFirstMaven.java” in src/main/java package

MyFirstMaven class

7: In case there are some errors in the project, try to Update the project. Right Click on project -> Maven -> Update Project -> Check Force Update -> OK.

Maven Update project

8: Let us add some good example in our Java Class. Here we are trying to create an Excel file with site data using Apache POI library. The pom dependency of apache POI has already been added in our pom.xml.

package code2java;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class MyFirstMaven {

	public static void main(String[] args) {
		//Create a Blank workbook
		XSSFWorkbook blackWB = new XSSFWorkbook(); 

		//Create a blank sheet
		XSSFSheet wbSheet = blackWB.createSheet("Code2Java");

		//Create the dummy data to be inserted in sheet
		Map<String, String[]> siteData = new TreeMap<String, String[]>();
		siteData.put("1", new String[] {"Sr.No", "Site Name", "Owner"});
		siteData.put("2", new String[] {"1", "Code2Java", "admin"});
		siteData.put("3", new String[] {"2", "Gmail", "Google"});
		siteData.put("4", new String[] {"3", "Facebook", "Facebook"});
		siteData.put("5", new String[] {"4", "Instagram", "Facebook"});

		//Iterate over data and write to sheet
		Set<String> keyset = siteData.keySet();
		int rownum = 0;
		for (String key : keyset)
		{
			Row row = wbSheet.createRow(rownum++);
			String [] strArray = siteData.get(key);
			int cellnum = 0;
			for (String obj : strArray)
			{
				Cell cell = row.createCell(cellnum++);
				cell.setCellValue(obj);
			}
		}
		try
		{
			//Write the workbook in file system
			FileOutputStream out = new FileOutputStream(new File("Code2Java_MavenProject.xlsx"));
			blackWB.write(out);
			out.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}finally
		{
			try {
				blackWB.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}

C. Build and Run

9: Now the project is ready to be build. To create the Run Configuration with goals as “clean install

Run Configuration to build the maven project

Once you click on Run, if everything goes fine, build would be success.

MAVEN PROJECT BUILD SUCCESS

9: Run the project as Java Application. It will generate the Excel file along side to your project folder.

Output

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.