How to Convert Java project to Maven project in Eclipse

There are times when we need to convert Java project into a Maven project, this will be a huge task when you have more number of projects. There is a plugin called “m2e” plugin which lets us perform this task with ease. Just follow the below step to convert Java Project to Maven project.

Install Maven support for Eclipse

In order to add m2e plugin to eclipse, Go to help ->Install New Software

Convert Java project to Maven project

Convert Java project to Maven project

Once the plugin is installed then our task is so simple Select the project which needs to be converted into a maven project and right click on it and click on Configure and you will see “Convert to Maven Project” click on it.

Convert to Maven Project

Now you will get a new popup window, which will have the Group Id and Artifact Id pre-populated. Enter the Name and Description but it is not mandatory.

Create New POM

Now click Finish, your project will be converted to a maven project now and you will have POM.xml created.

<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>Test</groupId>
  <artifactId>Test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
   <sourceDirectory>src</sourceDirectory>
    <plugins>
     <plugin>
       <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
         <source>1.8</source>
         <target>1.8</target>
       </configuration>
     </plugin>
    <plugin>
     <artifactId>maven-war-plugin</artifactId>
     <version>2.6</version>
     <configuration>
      <warSourceDirectory>WebContent</warSourceDirectory>
      <failOnMissingWebXml>false</failOnMissingWebXml>
     </configuration>
    </plugin>
  </plugins>
  </build>
</project>

How to add dependency in Maven using Eclipse

In order to add the dependent jar for our project using Maven, we can add it through <dependencies></dependencies> tag in our POM.xml like below.

<dependencies>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.6.1</version>
 </dependency>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.6.1</version>
 </dependency>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.6.1</version>
 </dependency>
</dependencies>

 

Leave a Reply

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