转换基于Maven的Java项目支持Eclipse IDE

在过去的教程中,使用 Maven 创建了一个Java项目,但是这个项目不能导入到Eclipse IDE中,因为它不是 Eclipse 风格的项目。这里有一个指南,向您演示如何转换 Maven 生成 Java 项目为 Eclipse 支持格式的项目。1. mvn eclipse:eclipse这真的很容易做到这一点。浏览到 Java 项目文件夹,其中 pom.xml 文件被重写。并发出以下命令:C:worksp>mvn archetype:generate -DgroupId=com.yiibai -DartifactId=Yiibaicor [INFO] ------------------------------------------------------------------------- --- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1 [INFO] ------------------------------------------------------------------------- --- [INFO] Parameter: basedir, Value: C:worksp [INFO] Parameter: package, Value: com.yiibai [INFO] Parameter: groupId, Value: com.yiibai [INFO] Parameter: artifactId, Value: Yiibaicor [INFO] Parameter: packageName, Value: com.yiibai [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: C:workspYiibaicor [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 03:37 min [INFO] Finished at: 2015-10-27T20:37:00+08:00 [INFO] Final Memory: 16M/185M [INFO] ------------------------------------------------------------------------看一个完整的例子:C:worksp>cd Yiibaicor C:workspYiibaicor>mvn eclipse:eclipse [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Yiibaicor 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ Yiibaicor >>> [INFO] [INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ Yiibaicor <<< [INFO] [INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ Yiibaicor --- [INFO] Using Eclipse Workspace: null [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAI ER [INFO] Not writing settings - defaults suffice [INFO] Wrote Eclipse project for "Yiibaicor" to C:workspYiibaicor. [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.737 s [INFO] Finished at: 2015-10-27T20:39:15+08:00 [INFO] Final Memory: 13M/114M [INFO] ------------------------------------------------------------------------注意,如果你第一次运行这个命令,它可能需要一些时间来下载所有必需的依赖到你的项目,才能转换为 Eclipse 风格的项目。2. 验证Java项目在此之后,你会发现创建了两个新文件 - “.classpath”和“.project”。这两个文件都为 Eclipse IDE 所创建。File : .classpath <?xml version="1.0" encoding="UTF-8"?> <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="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/> </classpath>注意
有没有注意到,一个“M2_REPO” classpath变量已经生成?对于第一次,要将这个 M2_REPO classpath 变量添加到您的Eclipse IDE中。 否则,Eclipse会在你的导入项目之后引发错误。File : .project  <?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>Yiibaicor</name> <comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment> <projects/> <buildSpec> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> </buildCommand> </buildSpec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>3. 导入到Eclipse IDE现在,将它导入到 Eclipse IDE中,请按以下步骤操作:在Eclipse IDE,菜单栏 , File -> Import… -> General -> Existing Projects into Workspace -> 选择根目录(选择项目文件夹中) - >完成。 你的Maven生成的Java项目导入到Eclipse IDE中,你可以现在就开始你的开发。

相关内容推荐