[Maven实战](4)eclipse创建Maven项目

简介:
介绍前面Hello World项目的时候,并没有涉及IDE,这样一个简单的项目,使用最简单的编辑器也能完成。但是对一个稍微大一些的项目来说,还是推荐使用IDE。这一篇文章我们在m2eclipse帮助下使用eclipse创建Maven项目。

1. 创建

使用IDE创建一个Maven项目非常简单,选择菜单项File->New->Other,在弹出的对话框中选择Maven下的Maven Project,然后点击Next按钮。

图1

在弹出的New Maven Project对话框中,使用默认的选项。不要选择Create a simple project。然后,点击Next按钮。

图2 

此时m2eclipse会提示我们选择一个Archetype。这里选择maven-archetype-quickstart,再点击Next按钮。m2eclipse实际上是使用maven-archetype-plugin插件创建项目。

图3

此时输入groupId,artifactId,version,package。

图4

单击Finshi按钮,Maven项目就创建完成了。项目目录结构如下:

图5

2. 运行mvn命令

在Maven项目或者pom.xml上右击,再点击弹出的快捷菜单选择Run as,就能看到常见的Maven命令。

图6

选择想要执行的maven命令技能执行相应的构建,同时也能在eclipse的console中看到构建输出。例如:运行mvn test
 
  
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\JavaWorkSpace\hello-world\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-world ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\JavaWorkSpace\hello-world\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello-world ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ hello-world ---
[INFO] Surefire report directory: D:\WorkSpace\JavaWorkSpace\hello-world\target\surefire-reports
 
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.qunar.model.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec
 
Results :
 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.439s
[INFO] Finished at: Sat Jan 23 14:11:18 CST 2016
[INFO] Final Memory: 6M/15M
[INFO] ------------------------------------------------------------------------

如果默认选项张没有我们想要执行的Maven命令怎么办?比如,默认带有mvn test,但是没有mvn clean test。我们可以选择Maven build来自定义Maven 运行命令,在弹出的对话框的Goals一项中输入我们想要执行的命令,如clean test,设置一下Name,点击Run即可。

图7
当你运行时,你可能会遇到下面问题:
 
  
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-world ---
[INFO] Deleting D:\WorkSpace\JavaWorkSpace\hello-world\target
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\JavaWorkSpace\hello-world\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-world ---
[INFO] Compiling 1 source file to D:\WorkSpace\JavaWorkSpace\hello-world\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Unable to locate the Javac Compiler in:
D:\Program Files (x86)\Java\jre7\..\lib\tools.jar
Please ensure you are using JDK 1.4 or above and
not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java
installation by setting the JAVA_HOME environment variable.
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.044s
[INFO] Finished at: Sat Jan 23 14:54:26 CST 2016
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project hello-world: Compilation failure
[ERROR] Unable to locate the Javac Compiler in:
[ERROR] D:\Program Files (x86)\Java\jre7\..\lib\tools.jar
[ERROR] Please ensure you are using JDK 1.4 or above and
[ERROR] not a JRE (the com.sun.tools.javac.Main class is required).
[ERROR] In most cases you can change the location of your Java
[ERROR] installation by setting the JAVA_HOME environment variable.
[ERROR] -> [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:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

解决方案:这是因为eclipse默认是使用jre作为运行环境,而maven编译需要jdk作为运行环境。
就是在eclipse里设置一个jdk的运行环境,然后将当前项目的运行环境设为jdk运行环境即可。 

(1)window -> preferences -> java -> installed jres 修改你的jre,将其location指向你的jdk目录。 

(2)点击Add按钮,选择Standard VM,jre home选择你的jdk目录。点击finish,这时发现多了一个JRE,将其勾上,以后新的项目,就默认使用这个JRE了。
 


 
  
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-world ---
[INFO] Deleting D:\WorkSpace\JavaWorkSpace\hello-world\target
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\JavaWorkSpace\hello-world\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-world ---
[INFO] Compiling 1 source file to D:\WorkSpace\JavaWorkSpace\hello-world\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\JavaWorkSpace\hello-world\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hello-world ---
[INFO] Compiling 1 source file to D:\WorkSpace\JavaWorkSpace\hello-world\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ hello-world ---
[INFO] Surefire report directory: D:\WorkSpace\JavaWorkSpace\hello-world\target\surefire-reports
 
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.qunar.model.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.014 sec
 
Results :
 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.246s
[INFO] Finished at: Sat Jan 23 14:58:05 CST 2016
[INFO] Final Memory: 11M/27M
[INFO] ------------------------------------------------------------------------






来源于:《Maven实战》




目录
相关文章
|
2月前
|
XML Java Shell
【深入浅出Maven开发实战】「入门教程系列」带你零基础学习和开发使用Maven开发工具实战指南(实战技术总结)(一)
【深入浅出Maven开发实战】「入门教程系列」带你零基础学习和开发使用Maven开发工具实战指南(实战技术总结)
82 1
|
2月前
|
XML Java Maven
【Maven技术专题】「实战开发系列」盘点Maven项目中打包需要注意到的那点事儿
【Maven技术专题】「实战开发系列」盘点Maven项目中打包需要注意到的那点事儿
41 0
|
2月前
|
XML Java Maven
【深入浅出Maven开发实战】「入门教程系列」带你零基础学习和开发使用Maven开发工具实战指南(实战技术总结)(二)
【深入浅出Maven开发实战】「入门教程系列」带你零基础学习和开发使用Maven开发工具实战指南(实战技术总结)
51 0
|
8天前
|
Java Maven
idea中maven项目pom文件Could not acquire lock(s)
idea中maven项目pom文件Could not acquire lock(s)
|
27天前
|
Java Maven Spring
【操作宝典】IntelliJ IDEA新建maven项目详细教程
【操作宝典】IntelliJ IDEA新建maven项目详细教程
36 1
|
2月前
|
Java Maven 开发工具
maven导入项目出现Unable to import maven project: See logs for details
maven导入项目出现Unable to import maven project: See logs for details
11 0
maven导入项目出现Unable to import maven project: See logs for details
|
1天前
|
Java 项目管理 Maven
【揭秘】Maven聚合与继承:如何轻松实现项目依赖管理?
Maven的聚合和继承是Java开发中重要的概念。聚合允许将多个项目组合成一个构建单元,简化多模块项目的构建过程,提高构建效率。继承则让子项目重用父项目的配置和属性,避免了重复定义,增强了项目的一致性和可维护性。通过聚合和继承,Maven为多模块项目的构建和管理提供了高效且灵活的支持,减少了配置冗余,提升了开发效率。
【揭秘】Maven聚合与继承:如何轻松实现项目依赖管理?
|
2天前
|
Java Maven
Maven 项目文档
学习Maven项目文档创作,借助Doxia引擎将内容转化为通用模型。支持Apt(纯文本)、Xdoc(Maven 1.x格式)、FML(FAQ)和XHTML。
|
3天前
|
Java Apache Maven
Maven 项目文档Maven 项目文档
Maven使用Doxia引擎将多种格式(如Apt、Xdoc、FML和XHTML)转换为通用文档模型。在创建Maven项目文档时,例如在C:/MVN下创建consumerBanking项目,需运行指定的mvn archetype:generate命令。接着,更新pom.xml,确保包含maven-site-plugin和maven-project-info-reports-plugin的最新版本(至少3.3和2.7),以避免NoClassDefFoundError。执行`mvn site`命令生成文档。
|
3天前
|
Java Apache Maven
Maven 项目文档
本教程介绍如何创建 Maven 项目文档。在 C:/MVN 目录下,使用命令 `mvn archetype:generate` 创建 `consumerBanking` 项目。为解决运行 `mvn site` 时的 NoClassDefFoundError,需在 `pom.xml` 中添加并更新 `maven-site-plugin` 至版本 3.3。执行 `mvn site` 后,Maven 生成包括"About", "Issue Tracking"等报告的文档。成功后,文档位于 `target/site/index.html`。

热门文章

最新文章

推荐镜像

更多