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

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

1. 创建

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

在弹出的New Maven Project对话框中,使用默认的选项。不要选择Create a simple project。然后,点击Next按钮。
[Maven实战](4)eclipse创建Maven项目
图2 

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

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

此时输入groupId,artifactId,version,package。
[Maven实战](4)eclipse创建Maven项目
图4

单击Finshi按钮,Maven项目就创建完成了。项目目录结构如下:
[Maven实战](4)eclipse创建Maven项目
图5

2. 运行mvn命令

在Maven项目或者pom.xml上右击,再点击弹出的快捷菜单选择Run as,就能看到常见的Maven命令。
[Maven实战](4)eclipse创建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即可。
[Maven实战](4)eclipse创建Maven项目
图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了。
[Maven实战](4)eclipse创建Maven项目 [Maven实战](4)eclipse创建Maven项目



[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实战》




上一篇:类型转换


下一篇:STM32+果云GA6-GPRS/GSM模块+MQTT+HTTP协议连接中移OneNet上传GPS数据定位