将 Maven生成的java项目转化为支持 Eclipse IDE的项目

转自: http://www.xuebuyuan.com/1297046.html

将 Maven生成的java项目转化为支持 Eclipse IDE的项目
 

在前一篇文章中,我们使用maven创建了一个java 项目, 但是这个项目不能导入 Eclipse IDE, 因为它不是 Eclipse style的项目。

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 :
 
  1. mvn eclipse:eclipse
完整的运行情况,可能像这样:
  1. C:\Users\admin\henrycublog>mvn eclipse:eclipse
  2. [INFO] Scanning for projects...
  3. Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-eclips
  4. e-plugin/maven-metadata.xml
  5. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-eclipse
  6. -plugin/maven-metadata.xml (680 B at 0.2 KB/sec)
  7. Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-eclips
  8. e-plugin/2.8/maven-eclipse-plugin-2.8.pom
  9. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-eclipse
  10. -plugin/2.8/maven-eclipse-plugin-2.8.pom (12 KB at 3.7 KB/sec)
  11. Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-plugin
  12. s/17/maven-plugins-17.pom
  13. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-plugins
  14. /17/maven-plugins-17.pom (13 KB at 8.6 KB/sec)
  15. Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-eclips
  16. e-plugin/2.8/maven-eclipse-plugin-2.8.jar
  17. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-eclipse
  18. -plugin/2.8/maven-eclipse-plugin-2.8.jar (202 KB at 5.4 KB/sec)
  19. [INFO]
  20. [INFO] ------------------------------------------------------------------------
  21. [INFO] Building henrycublog 1.0-SNAPSHOT
  22. [INFO] ------------------------------------------------------------------------
  23. [INFO]
  24. [INFO] >>> maven-eclipse-plugin:2.8:eclipse (default-cli) @ henrycublog >>>
  25. [INFO]
  26. [INFO] <<< maven-eclipse-plugin:2.8:eclipse (default-cli) @ henrycublog <<<
  27. [INFO]
  28. [INFO] --- maven-eclipse-plugin:2.8:eclipse (default-cli) @ henrycublog ---
  29. Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-interactiv
  30. ity-jline/1.0-alpha-5/plexus-interactivity-jline-1.0-alpha-5.pom
  31. Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-interactivi
  32. ty-jline/1.0-alpha-5/plexus-interactivity-jline-1.0-alpha-5.pom (772 B at 0.7 KB
  33. /sec)
  34. Downloading: http://repo1.maven.org/maven2/jline/jline/0.9.1/jline-0.9.1.pom
  35. Downloaded: http://repo1.maven.org/maven2/jline/jline/0.9.1/jline-0.9.1.pom (145
  36. B at 0.1 KB/sec)
  37. Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.2/m
  38. aven-archiver-2.2.pom
  39. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.2/ma
  40. ven-archiver-2.2.pom (2 KB at 1.3 KB/sec)
  41. Downloading: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-shared-
  42. components/3/maven-shared-components-3.pom
  43. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-shared-c
  44. omponents/3/maven-shared-components-3.pom (2 KB at 2.0 KB/sec)
  45. Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-resources/
  46. 1.0-alpha-7/plexus-resources-1.0-alpha-7.pom
  47. Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-resources/1
  48. .0-alpha-7/plexus-resources-1.0-alpha-7.pom (2 KB at 1.3 KB/sec)
  49. Downloading: http://repo1.maven.org/maven2/biz/aQute/bndlib/0.0.145/bndlib-0.0.1
  50. 45.pom
  51. Downloaded: http://repo1.maven.org/maven2/biz/aQute/bndlib/0.0.145/bndlib-0.0.14
  52. 5.pom (886 B at 0.7 KB/sec)
  53. Downloading: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-osgi/0.
  54. 2.0/maven-osgi-0.2.0.pom
  55. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-osgi/0.2
  56. .0/maven-osgi-0.2.0.pom (2 KB at 2.0 KB/sec)
  57. Downloading: http://repo1.maven.org/maven2/biz/aQute/bndlib/0.0.203/bndlib-0.0.2
  58. 03.pom
  59. Downloaded: http://repo1.maven.org/maven2/biz/aQute/bndlib/0.0.203/bndlib-0.0.20
  60. 3.pom (886 B at 0.7 KB/sec)
  61. Downloading: http://repo1.maven.org/maven2/org/eclipse/core/resources/3.3.0-v200
  62. 70604/resources-3.3.0-v20070604.pom
  63. Downloaded: http://repo1.maven.org/maven2/org/eclipse/core/resources/3.3.0-v2007
  64. 0604/resources-3.3.0-v20070604.pom (2 KB at 1.1 KB/sec)
  65. Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-interactiv
  66. ity-jline/1.0-alpha-5/plexus-interactivity-jline-1.0-alpha-5.jar
  67. Downloading: http://repo1.maven.org/maven2/jline/jline/0.9.1/jline-0.9.1.jar
  68. Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.2/m
  69. aven-archiver-2.2.jar
  70. Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-resources/
  71. 1.0-alpha-7/plexus-resources-1.0-alpha-7.jar
  72. Downloading: http://repo1.maven.org/maven2/biz/aQute/bndlib/0.0.145/bndlib-0.0.1
  73. 45.jar
  74. Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-interactivi
  75. ty-jline/1.0-alpha-5/plexus-interactivity-jline-1.0-alpha-5.jar (6 KB at 5.4 KB/
  76. sec)
  77. Downloading: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-osgi/0.
  78. 2.0/maven-osgi-0.2.0.jar
  79. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-osgi/0.2
  80. .0/maven-osgi-0.2.0.jar (13 KB at 6.8 KB/sec)
  81. Downloading: http://repo1.maven.org/maven2/org/eclipse/core/resources/3.3.0-v200
  82. 70604/resources-3.3.0-v20070604.jar
  83. Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-resources/1
  84. .0-alpha-7/plexus-resources-1.0-alpha-7.jar (23 KB at 5.1 KB/sec)
  85. Downloaded: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.2/ma
  86. ven-archiver-2.2.jar (10 KB at 1.5 KB/sec)
  87. Downloaded: http://repo1.maven.org/maven2/biz/aQute/bndlib/0.0.145/bndlib-0.0.14
  88. 5.jar (112 KB at 6.5 KB/sec)
  89. Downloaded: http://repo1.maven.org/maven2/jline/jline/0.9.1/jline-0.9.1.jar (46
  90. KB at 2.6 KB/sec)
  91. Downloaded: http://repo1.maven.org/maven2/org/eclipse/core/resources/3.3.0-v2007
  92. 0604/resources-3.3.0-v20070604.jar (663 KB at 6.5 KB/sec)
  93. [INFO] Using Eclipse Workspace: null
  94. [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAIN
  95. ER
  96. [INFO] Not writing settings - defaults suffice
  97. [INFO] Wrote Eclipse project for "henrycublog" to C:\Users\admin\henrycublog.
  98. [INFO]
  99. [INFO] ------------------------------------------------------------------------
  100. [INFO] BUILD SUCCESS
  101. [INFO] ------------------------------------------------------------------------
  102. [INFO] Total time: 2:48.142s
  103. [INFO] Finished at: Wed Nov 09 11:31:54 CST 2011
  104. [INFO] Final Memory: 8M/58M
  105. [INFO] ------------------------------------------------------------------------
  106. C:\Users\admin\henrycublog>

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

  1. <classpath>
  2. <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
  3. <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  4. <classpathentry kind="output" path="target/classes"/>
  5. <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
  6. <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  7. </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.

当然,如果你已经安装了maven eclipse插件,那就很简单了。window>Preferences>Maven>Installations>Add(添加你的maven安装位置)

如果你没有安装Maven插件,按以下方法添加M2_REPO:

Define and add M2_REPO classpath variable manually to Eclipse IDE. Follow below steps :

  1. Eclipse IDE, menu bar
  2. Select Window > Preferences
  3. Select Java > Build Path > Classpath Variables
  4. Click on the new button > defined a new M2_REPO variable and point it to your local Maven repository

Done.

File : .project

  1. <projectDescription>
  2. <name>henrycublog</name>
  3. <comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
  4. <projects/>
  5. <buildSpec>
  6. <buildCommand>
  7. <name>org.eclipse.jdt.core.javabuilder</name>
  8. </buildCommand>
  9. </buildSpec>
  10. <natures>
  11. <nature>org.eclipse.jdt.core.javanature</nature>
  12. </natures>
  13. </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.

上一篇:NFS - Network File System网络文件系统


下一篇:牛腩新闻发布系统--学习Web的小技巧汇总