How to convert Maven based Java Project to support Eclipse IDE

In last tutorial, you created a Java project with Maven, but that project is not able to import into Eclipse IDE, because it is not Eclipse style project.

Here’s a guide to show you how to convert the Maven generated Java project to Eclipse supported style project.

1. mvn eclipse:eclipse

It’s really easy to do it. Navigate to your Java project folder, where pom.xml file is placed. And issue this command :


mvn eclipse:eclipse

See a full example :


D:\workspace-new\maven-test\mkyong-core>mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building mkyong-core
[INFO]    task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse {execution: default-cli}]
[INFO] Using Eclipse Workspace: D:\workspace-new
[INFO] no substring wtp server match.
[INFO] Using as WTP server : Apache Tomcat v6.0
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "mkyong-core" to D:\workspace-new\maven-test\mkyong-core.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Thu Apr 14 22:47:38 SGT 2011
[INFO] Final Memory: 7M/14M
[INFO] ------------------------------------------------------------------------
Note
If this is your first time to run this command, it may take some time to download all required dependency to convert your project to Eclipse style project.

2. Verify Java Project

After that, you will notice two new files are created – “.classpath” and “.project“. Both files are created for Eclipse IDE.

File : .classpath


<classpath>
  <classpathentry kind="src" path="src/test/java" 
           output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" 
	   sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>
Wait, M2_REPO !?
Did you notice a “M2_REPO” classpath variable is generated? For first time, you have to add this M2_REPO classpath variable to your Eclipse IDE. Otherwise, Eclipse will raise error about your imported project later.

File : .project


<projectDescription>
  <name>mkyong-core</name>
  <comment/>
  <projects/>
  <buildSpec>
    <buildCommand>
      <name>org.eclipse.jdt.core.javabuilder</name>
    </buildCommand>
  </buildSpec>
  <natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
  </natures>
</projectDescription>

3. Import into Eclipse IDE

Now, import it into Eclipse IDE, follow below steps :

In Eclipse IDE, menu bar , File -> Import… -> General -> Existing Projects into Workspace -> select root directory (select your project folder) -> Done.

Import Maven project to Eclipse IDE
Import Maven project to Eclipse IDE

Your Maven generated Java project is imported into Eclipse IDE, you can start your development now.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
21 Comment threads
9 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
24 Comment authors
JhonMonte ChanRawoofRaghuSathish Recent comment authors
newest oldest most voted
sdsd
Guest
sdsd

sdd

sdsd
Guest
sdsd

sorry actually i just tested the validation

Jhon
Guest
Jhon

mvn eclipse:eclipse is deprecated, is there any alternative?

Monte Chan
Guest
Monte Chan

Hi Mkyong,

I am getting the following error after doing mvn eclipse:eclipse.

[INFO] ————————————————————————
[INFO] BUILD FAILURE
[INFO] ————————————————————————
[INFO] Total time: 01:05 min
[INFO] Finished at: 2015-01-06T10:21:53-06:00
[INFO] Final Memory: 44M/326M
[INFO] ————————————————————————

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-eclipse-plugin:2.9
:eclipse (default-cli) on project aimsweb-browser: Execution default-cli of goal
org.apache.maven.plugins:maven-eclipse-plugin:2.9:eclipse failed. NullPointerException -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:

Can anybody help me please?

Rawoof
Guest
Rawoof

hi, when i am trying to convert mvn project to eclipse this error is comming: [INFO] Scanning for projects… [INFO] Reactor build order: [INFO] Biometric Integration API [INFO] Authentication XSD Model [INFO] UID Protobuf Model [INFO] Authentication Client Library [INFO] Aadhaar Authentication – Sample GUI [INFO] Aadhaar Authentication Reference Sample Application [INFO] Searching repository for plugin with prefix: ‘eclipse’. [INFO] ———————————————————————— [INFO] Building Biometric Integration API [INFO] task-segment: [eclipse:eclipse] [INFO] ———————————————————————— [INFO] Preparing eclipse:eclipse [INFO] No goals needed for project – skipping [INFO] [eclipse:eclipse {execution: default-cli}] [INFO] Using Eclipse Workspace: /home/madhu/Desktop [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER [INFO] Wrote settings… Read more »

raghu
Guest
raghu

Hi MkYong,

Am getting “Using Eclipse Workspace: null” while executing mvn eclipse:eclipse. kindly tell me how can i overcome this. waiting for your reply

Vikash
Guest
Vikash

Hello mkyong, I am not able to create EAR and WAR module using maven. I am using command prompt to do so. I have created a mvn project with the below structure: D:. ????????SampleProject-ear ????????SampleProject-war ????????src ?? ????????main ?? ?? ????????java ?? ?? ?? ????????com ?? ?? ?? ?? ????????aexp ?? ?? ?? ?? ????????sampleProject ?? ?? ?? ?? ????????web ?? ?? ?? ?? ????????controller ?? ?? ?? ????????META-INF ?? ?? ????????resources ?? ?? ????????webapp ?? ?? ????????META-INF ?? ?? ????????resources ?? ?? ?? ????????images ?? ?? ?? ????????styles ?? ?? ????????WEB-INF ?? ?? ????????config ?? ?? ????????views ?? ????????test… Read more »

jay
Guest
jay

Hi MKyong:
Do you have any tutorial using artifactory/maven. and m2e plugin. We are trying to convert the current project using maven, and artifacotry is a repo. So far you are the best simple example and easy to follow. Many thanks

jaggsmca325
Guest
jaggsmca325

Hi MKY, thanks for your all tuts. I did it same way as explained, i created a maven project, building, packaging and running, everything works fine in command line, but then I tried to import into STS (v2.9.2) it failed with below error. Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 from/to central (http://repo1.maven.org/maven2): The operation was cancelled. Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.4.3 or one of its dependencies could not… Read more »

bdo
Guest
bdo

Im having an error when executing mvn eclipse:eclipse

project id: com.dyuproject.protostuff:protostuff-maven-plugin reason: POM 'com.dyuproject.protostuff:protostuff-maven-plugin' not found in repository: unable to download the artifact from any repository

com.dyuproject.protostuff:protostuff-maven-plugin:pom:1.0.2-SNAPSHOT

from the specified remote repositories:
central (http://repo1.maven.org/maven2)

for project com.dyuproject.protostuff:protostuff-maven-plugin
Muthu Patil
Guest
Muthu Patil

i have war,jar,ear all , in maven structured , i want to convrt this project into normal java project. ur command mvn eclipse:eclipse is not working for this scenario..

thanks any help..

Sathish
Guest
Sathish

I think you can have only one project in maven. It is either a Java project (JAR) or a Enterprise Web project (EAR) or a Web project (WAR). Please change the POM accordingly to reflect the type of project you want in . Since I didn’t find any reply for this I am replying the thread for anybody who can find some use.

Sathish
Guest
Sathish

On the other hand, if you are talking about modules, like JAR to be added to WAR lib path then you have to create a POM on the parent directory and establish the modules. You can check for more info on that in the maven website on adding modules.

Gurunath Solanki
Guest
Gurunath Solanki

Hi Mkyong,

When I do mvn eclipse:eclipse, It shows build successful, but it shows eclipse workspace:null. When I try to import the project into eclipse , it does not show .classpath and .project files in IDE, and project shows error.

Could you please help me with this issue.

Thanks in advance.

Rajesh
Guest
Rajesh

Great stuff…. i was able to solve dependency issue between between maven and eclipse based project.

Anirban Chatterjee
Guest
Anirban Chatterjee

Hello mkyong,

I have added dependent project in my project via pom.xml entry. But those are downloaded as jar where I can find .class file only.

Please let me know what changes I need to do(in pom.xml or anywhere) to see the source code(.java) as well.

Thanks in advance.

Regards,
Anirban

Vito
Guest
Vito

when i enter mvn eclipse:eclipse in shell, it tell me:BUILD FAILURE this is run-info: shuumatoMacBook:mvn-test Vito$ mvn eclipse:eclipse [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.511s [INFO] Finished at: Mon Aug 27 15:39:39 CST 2012 [INFO] Final Memory: 6M/23M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-eclipse-plugin:2.9:eclipse (default-cli): Goal requires a project to execute but there is no POM in this directory (/Users/Vito/mvn-test). Please verify you invoked Maven from the correct directory. -> [Help 1] [ERROR] [ERROR] To see the… Read more »

Raghu
Guest
Raghu

you should get inside the folder where pom.xml is there, then you have to run mvn eclipse:eclipse. it will work

julius
Guest
julius

Great stuff and thanks
Then import the maven project in eclipse I can not see the files, the classpath and the project

DZ
Guest
DZ

Great tutorial,thanks.Solved my problem

Nawaz
Guest
Nawaz

Hi I followed the same mkyoung tutorial and facing the following build issue. My expectation was that it will download all the necessary libs and i would not be setting any config for this.

Can some one help me to resolved the following issue.

Description Resource Path Location Type
Unbound classpath variable: ‘M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar’ in project ‘mkyong-core’ mkyong-core Build path Build Path Problem
The project cannot be built until build path errors are resolved mkyong-core Unknown Java Problem

Gagandeep Singh
Guest
Gagandeep Singh

[solved] Your MAVEN folder doens’t include the junit folder and thus the junit jar file. Here’s how it goes… After importing the project in eclipse IDE. 1. create a classpath variable: windows->preference->java->buildPath->classpath Variables -> click New button and define “M2_REPO” for value “C:/Program Files/Apache Software Foundation/apache-maven-3.0.4” or your path to maven repo 2. within your maven Repo : (right where your bin folder is defined) create (junit/junit/junit-4.10.jar) a. create folder junit b. again create folder junit inside “junit” c. copy the junit-4.10.jar file to that path. 3. reconfigure the build path to target the junit directory 4. change the POM.xml… Read more »

Faisal
Guest
Faisal

Thanks Mkyong for this nice tutorial. I am new in Maven use. And therefore having some qurstions. Will be glad if u answer: In this tutorial, It is nice that I got Java Project now. Previously everytime while I was even pressing F3 upon keeping cursor on any function call, was getting: “The resource is not on the build path of a Java project”. Now can u plz tell me, after writing all the code, how can i run the project in my local server (here websphere) (I mean, how can i create an EAR file) and how can i… Read more »

trackback
How to convert Maven based web application to support Eclipse IDE

[…] convert a Maven based Java project to support IDE, you use this command : mvn […]

john smith
Guest
john smith

or just install IDEA and open the pom.xml directly….

pzelnip
Guest
pzelnip

Or install m2eclipse