您的位置:首页 > 编程语言 > Java开发

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

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

1.创建

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





图1

在弹出的NewMavenProject对话框中,使用默认的选项。不要选择Createasimpleproject。然后,点击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上右击,再点击弹出的快捷菜单选择Runas,就能看到常见的Maven命令。





图6

选择想要执行的maven命令技能执行相应的构建,同时也能在eclipse的console中看到构建输出。例如:运行mvntest

[code][INFO]Scanningforprojects...
[INFO]
[INFO]------------------------------------------------------------------------
[INFO]Buildinghello-world0.0.1-SNAPSHOT
[INFO]------------------------------------------------------------------------
[INFO]
[INFO]---maven-resources-plugin:2.4.3:resources(default-resources)@hello-world---
[INFO]Using'UTF-8'encodingtocopyfilteredresources.
[INFO]skipnonexistingresourceDirectoryD:\WorkSpace\JavaWorkSpace\hello-world\src\main\resources
[INFO]
[INFO]---maven-compiler-plugin:2.3.2:compile(default-compile)@hello-world---
[INFO]Nothingtocompile-allclassesareuptodate
[INFO]
[INFO]---maven-resources-plugin:2.4.3:testResources(default-testResources)@hello-world---
[INFO]Using'UTF-8'encodingtocopyfilteredresources.
[INFO]skipnonexistingresourceDirectoryD:\WorkSpace\JavaWorkSpace\hello-world\src\test\resources
[INFO]
[INFO]---maven-compiler-plugin:2.3.2:testCompile(default-testCompile)@hello-world---
[INFO]Nothingtocompile-allclassesareuptodate
[INFO]
[INFO]---maven-surefire-plugin:2.7.1:test(default-test)@hello-world---
[INFO]Surefirereportdirectory:D:\WorkSpace\JavaWorkSpace\hello-world\target\surefire-reports
-------------------------------------------------------
TESTS
-------------------------------------------------------
Runningcom.qunar.model.AppTest
Testsrun:1,Failures:0,Errors:0,Skipped:0,Timeelapsed:0.035sec
Results:
Testsrun:1,Failures:0,Errors:0,Skipped:0
[INFO]------------------------------------------------------------------------
[INFO]BUILDSUCCESS
[INFO]------------------------------------------------------------------------
[INFO]Totaltime:4.439s
[INFO]Finishedat:SatJan2314:11:18CST2016
[INFO]FinalMemory:6M/15M
[INFO]------------------------------------------------------------------------


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





图7

当你运行时,你可能会遇到下面问题:

[code][INFO]Scanningforprojects...
[INFO]
[INFO]------------------------------------------------------------------------
[INFO]Buildinghello-world0.0.1-SNAPSHOT
[INFO]------------------------------------------------------------------------
[INFO]
[INFO]---maven-clean-plugin:2.4.1:clean(default-clean)@hello-world---
[INFO]DeletingD:\WorkSpace\JavaWorkSpace\hello-world\target
[INFO]
[INFO]---maven-resources-plugin:2.4.3:resources(default-resources)@hello-world---
[INFO]Using'UTF-8'encodingtocopyfilteredresources.
[INFO]skipnonexistingresourceDirectoryD:\WorkSpace\JavaWorkSpace\hello-world\src\main\resources
[INFO]
[INFO]---maven-compiler-plugin:2.3.2:compile(default-compile)@hello-world---
[INFO]Compiling1sourcefiletoD:\WorkSpace\JavaWorkSpace\hello-world\target\classes
[INFO]-------------------------------------------------------------
[ERROR]COMPILATIONERROR:
[INFO]-------------------------------------------------------------
[ERROR]UnabletolocatetheJavacCompilerin:
D:\ProgramFiles(x86)\Java\jre7\..\lib\tools.jar
PleaseensureyouareusingJDK1.4oraboveand
notaJRE(thecom.sun.tools.javac.Mainclassisrequired).
InmostcasesyoucanchangethelocationofyourJava
installationbysettingtheJ***A_HOMEenvironmentvariable.
[INFO]1error
[INFO]-------------------------------------------------------------
[INFO]------------------------------------------------------------------------
[INFO]BUILDFAILURE
[INFO]------------------------------------------------------------------------
[INFO]Totaltime:1.044s
[INFO]Finishedat:SatJan2314:54:26CST2016
[INFO]FinalMemory:5M/15M
[INFO]------------------------------------------------------------------------
[ERROR]Failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile(default-compile)onprojecthello-world:Compilationfailure
[ERROR]UnabletolocatetheJavacCompilerin:
[ERROR]D:\ProgramFiles(x86)\Java\jre7\..\lib\tools.jar
[ERROR]PleaseensureyouareusingJDK1.4oraboveand
[ERROR]notaJRE(thecom.sun.tools.javac.Mainclassisrequired).
[ERROR]InmostcasesyoucanchangethelocationofyourJava
[ERROR]installationbysettingtheJ***A_HOMEenvironmentvariable.
[ERROR]->[Help1]
[ERROR]
[ERROR]Toseethefullstacktraceoftheerrors,re-runMavenwiththe-eswitch.
[ERROR]Re-runMavenusingthe-Xswitchtoenablefulldebuglogging.
[ERROR]
[ERROR]Formoreinformationabouttheerrorsandpossiblesolutions,pleasereadthefollowingarticles:
[ERROR][Help1]'target='_blank'>http://cwiki.apache.org/confluence/display/M***EN/MojoFailureException[/code]
解决方案:这是因为eclipse默认是使用jre作为运行环境,而maven编译需要jdk作为运行环境。

就是在eclipse里设置一个jdk的运行环境,然后将当前项目的运行环境设为jdk运行环境即可。

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

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









[code][INFO]Scanningforprojects...
[INFO]
[INFO]------------------------------------------------------------------------
[INFO]Buildinghello-world0.0.1-SNAPSHOT
[INFO]------------------------------------------------------------------------
[INFO]
[INFO]---maven-clean-plugin:2.4.1:clean(default-clean)@hello-world---
[INFO]DeletingD:\WorkSpace\JavaWorkSpace\hello-world\target
[INFO]
[INFO]---maven-resources-plugin:2.4.3:resources(default-resources)@hello-world---
[INFO]Using'UTF-8'encodingtocopyfilteredresources.
[INFO]skipnonexistingresourceDirectoryD:\WorkSpace\JavaWorkSpace\hello-world\src\main\resources
[INFO]
[INFO]---maven-compiler-plugin:2.3.2:compile(default-compile)@hello-world---
[INFO]Compiling1sourcefiletoD:\WorkSpace\JavaWorkSpace\hello-world\target\classes
[INFO]
[INFO]---maven-resources-plugin:2.4.3:testResources(default-testResources)@hello-world---
[INFO]Using'UTF-8'encodingtocopyfilteredresources.
[INFO]skipnonexistingresourceDirectoryD:\WorkSpace\JavaWorkSpace\hello-world\src\test\resources
[INFO]
[INFO]---maven-compiler-plugin:2.3.2:testCompile(default-testCompile)@hello-world---
[INFO]Compiling1sourcefiletoD:\WorkSpace\JavaWorkSpace\hello-world\target\test-classes
[INFO]
[INFO]---maven-surefire-plugin:2.7.1:test(default-test)@hello-world---
[INFO]Surefirereportdirectory:D:\WorkSpace\JavaWorkSpace\hello-world\target\surefire-reports
-------------------------------------------------------
TESTS
-------------------------------------------------------
Runningcom.qunar.model.AppTest
Testsrun:1,Failures:0,Errors:0,Skipped:0,Timeelapsed:0.014sec
Results:
Testsrun:1,Failures:0,Errors:0,Skipped:0
[INFO]------------------------------------------------------------------------
[INFO]BUILDSUCCESS
[INFO]------------------------------------------------------------------------
[INFO]Totaltime:3.246s
[INFO]Finishedat:SatJan2314:58:05CST2016
[INFO]FinalMemory:11M/27M
[INFO]------------------------------------------------------------------------


来源于:《Maven实战》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: