This tutorial talks about Converting an existing Java Project to Maven in Eclipse?
I have a Non-Maven Spring Project which I want to convert into a maven project.
Before converting an existing Java Project to Maven in Eclipse:
Steps to convert an existing Java Project to Maven in Eclipse:
Step 1: Right-click on your java project and go to Configure and you should see the “Convert to Maven Project” option.
Step 2: Provide group id and artifact id, I have kept the default.
Let me explain to you more about group id and artifact id.
Group id : It is a unique identifier for your project among all. So it may be something like com.companyname
Artifact id : It is the name of jar or war without a version. it may be something like a project, so here we have put artifact id as SpringApplicationContextDemo.
Click on the finish button.
Step 3:
Just remember to put all dependencies in 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 |
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringApplicationContextDemo</groupId> <artifactId>SpringApplicationContextDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>4.2.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <resources> <resource> <directory>src</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> |
Step 4: Now right on converted maven project then go to maven ->Update Project
Step 5. Select Your Project & Force Update of Snapshots/Releases. Click on OK
Finally, the Maven Project would look like this:
Note: Still this conversion process has a problem like you will not find standards project directory structures src/main/java and src/test/java, You can solve this issue manually(Still you can build and run the project successfully).
Complete source code of the above Project:
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 |
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringApplicationContextDemo</groupId> <artifactId>SpringApplicationContextDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>4.2.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <resources> <resource> <directory>src</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> |
Employee.java
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 |
package com.infotech.model; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Employee implements InitializingBean,DisposableBean{ private Integer employeeId; private String employeeName; public Integer getEmployeeId() { return employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } @Override public void afterPropertiesSet() throws Exception { System.out.println("Bean is going through init process..."); } @Override public void destroy() throws Exception { System.out.println("Bean is going to destroy..."); } } |
ClientTest.java
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 |
package com.infotech.client; import org.springframework.beans.BeansException; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.infotech.model.Employee; public class ClientTest { public static void main(String[] args) { AbstractApplicationContext context = null; try { context = new ClassPathXmlApplicationContext("applicationContext.xml"); Employee employee = context.getBean("employee", Employee.class); System.out.println(employee.getEmployeeId()+"\t"+employee.getEmployeeName()); context.registerShutdownHook(); } catch (BeansException e) { if(context != null) context.close(); } } } |
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="employee" class="com.infotech.model.Employee"> <property name="employeeId" value="19901"></property> <property name="employeeName" value="Sean Murphy"></property> </bean> </beans> |
If You right-click on ClientTest.java and Run AS -> Java Application. I will give below output
That’s all about Convert an existing Java Project to Maven in Eclipse?
You May Also Like:
What is Maven?
How to install and setup Maven Environment on windows?
Create a maven project using the command line
How to import the maven project in Eclipse?
How to Create a New Maven Project in Eclipse
How to create a maven web application project in Eclipse
How to create a multi-module project using maven?
How to Fix missing src/main/java & src/test/java folders in the Eclipse Maven web Project?
How to create user-defined properties in Maven?
What is the maven local repository location & how to change it?
How to add or update the maven settings.xml file in eclipse?
Importing maven remote Archetype Catalogs in eclipse ?
What is POM in the maven Project?
What is Super POM in Maven Project?
Understanding the purpose of Maven Plugin
Compiling the Maven project with different JDK versions?
Understanding of settings.xml in maven
How to enable a proxy setting in Maven?
Maven Dependency Scopes
How to generate javadoc in the maven Project
How to create a runnable JAR file with Maven?
How to add local jar files to a Maven project?
How to exclude dependency in maven?
Managing External Dependencies in maven
Understanding of Maven Repositories
What is an archetype in Maven?
Snapshot Vs Version in maven
Apache Maven – Profiles
If you have any feedback or suggestion please feel free to drop in below comment box.