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).
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
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
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-
Make the changes under libraries, select the Alternate JRE or Workspace default JRE. Here we will go with third option.
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
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 |
<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
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.
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.
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 |
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“
Once you click on Run, if everything goes fine, build would be success.
9: Run the project as Java Application. It will generate the Excel file along side to your project folder.