从GitHub将Maven项目导入Eclipse4.2

简介

GitHub is a web-based hosting service for projects that user the Git revision control system. It is a social networking where you can share your code.

GitHub中的Maven项目一般没有本地配置文件(主要是为了去除依赖,使项目整体结构清晰)。

但是当导入Github的Maven项目,并与本地的Eclipse直接结成,总有些困难,直到Eclipse 4.2(Juno). 本文介绍如何导入github项目,并直接与eclipse集成。

引文

本文是在Windows 7下进行,下面的软件将被安装,并使之一起工作 :
1. Eclipse: Eclipse IDE(本文使用eclipse-4.2)
2. Java: Java编程语言(本文使用java-1.7.0_05)
3. Maven: Java项目管理工具(本文使用mava-3.0.4)
4. Druid: JDBC Connection Pool(本文使用Druid项目为例)

条件准备
  1. GitHub流程, Set up git, Create a repo, Fork a repo, Be social. 参考GitHub Help.
  2. 生成ssh keys. 参考Generating SSH Keys.
  3. 有项目的push权限
  4. Java, Eclipse, Maven安装正确

配置

Maven配置
Eclipse-4.2使用的Maven版本默认为maven-3.0.4, 但是还是建议设置一下:
版本设置: Window > Preferences > Maven > Installations
用户设置: Window > Preferences > Maven > User Settings

SSH配置
SSH2设置: Window > Preferences > General > Network Connections > SSH2
注意设置SSH2 home及private keys.

导入项目

1.右键 > Import > Project from Git

从GitHub将Maven项目导入Eclipse4.2

2.选择URI

从GitHub将Maven项目导入Eclipse4.2

3.输入Remote Git Repo的配置信息

从GitHub将Maven项目导入Eclipse4.2

注意:Protocol使用ssh, User使用git, Password为账户在github的密码

4.查找远程分支信息

从GitHub将Maven项目导入Eclipse4.2

5.选择分支

从GitHub将Maven项目导入Eclipse4.2

6.选择本地目标位置

从GitHub将Maven项目导入Eclipse4.2

7.从版本仓库中进行Clone

从GitHub将Maven项目导入Eclipse4.2

8.接受文件中

从GitHub将Maven项目导入Eclipse4.2

9.选择导入项目类型

从GitHub将Maven项目导入Eclipse4.2

注意: 选择Import as general project

10.确认项目名称

从GitHub将Maven项目导入Eclipse4.2

11.项目如下

从GitHub将Maven项目导入Eclipse4.2

注意: 此时项目为General Project不是Maven project, 需要手工修改配置文件。但是由[druid master]可以看出,已经是一个带版本控制的项目了。

修改项目配置文件

由于导入的是普通项目,需要转化成Maven Project。Eclipse中项目的主要配置文件是.classpath和.project,还有.settings文件夹。
原项目为General project, 只有.project文件,其.project配置文件内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <projectDescription>
  3. <name>druid</name>
  4. <comment></comment>
  5. <projects></projects>
  6. <buildSpec></buildSpec>
  7. <natures></natures>
  8. </projectDescription>


需要修改.project,并添加.classpath文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <projectDescription>
  3. <name>druid</name>
  4. <comment></comment>
  5. <projects>
  6. </projects>
  7. <buildSpec>
  8. <buildCommand>
  9. <name>org.eclipse.jdt.core.javabuilder</name>
  10. <arguments>
  11. </arguments>
  12. </buildCommand>
  13. <buildCommand>
  14. <name>org.eclipse.m2e.core.maven2Builder</name>
  15. <arguments>
  16. </arguments>
  17. </buildCommand>
  18. </buildSpec>
  19. <natures>
  20. <nature>org.eclipse.jdt.core.javanature</nature>
  21. <nature>org.eclipse.m2e.core.maven2Nature</nature>
  22. </natures>
  23. </projectDescription>

还有.classpath文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <classpath>
  3. <classpathentry kind="src" output="target/classes" path="src/main/java">
  4. <attributes>
  5. <attribute name="optional" value="true"/>
  6. <attribute name="maven.pomderived" value="true"/>
  7. </attributes>
  8. </classpathentry>
  9. <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
  10. <attributes>
  11. <attribute name="maven.pomderived" value="true"/>
  12. </attributes>
  13. </classpathentry>
  14. <classpathentry kind="src" output="target/test-classes" path="src/test/java">
  15. <attributes>
  16. <attribute name="optional" value="true"/>
  17. <attribute name="maven.pomderived" value="true"/>
  18. </attributes>
  19. </classpathentry>
  20. <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
  21. <attributes>
  22. <attribute name="maven.pomderived" value="true"/>
  23. </attributes>
  24. </classpathentry>
  25. <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
  26. <attributes>
  27. <attribute name="maven.pomderived" value="true"/>
  28. </attributes>
  29. </classpathentry>
  30. <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
  31. <attributes>
  32. <attribute name="maven.pomderived" value="true"/>
  33. </attributes>
  34. </classpathentry>
  35. <classpathentry kind="output" path="target/classes"/>
  36. </classpath>

刷新项目

在项目上右键 > Refresh.
如果还有错误,可以在项目上右键 > Maven > Update project.. > OK, Eclipse 会自动重新建立.settings文件夹。

从GitHub将Maven项目导入Eclipse4.2

此时项目完成,可以直接提交到GitHub了。

转载于:https://my.oschina.net/u/698243/blog/84792