ITKeyword,专注技术干货聚合推荐

注册 | 登录

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

SunnyYoona 分享于 2016-01-23

推荐:Maven实战(三)Eclipse构建Maven项目

1. Eclipse安装m2eclipse插件,见eclipse maven plugin 插件 安装 和 配置 2 构建Maven项目 2.1)创建简单Maven项目 点击Eclipse菜单栏File->New->Ohter->Maven得

2019阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址https://promotion.aliyun.com/ntms/yunparter/invite.html

介绍前面Hello World项目的时候,并没有涉及IDE,这样一个简单的项目,使用最简单的编辑器也能完成。但是对一个稍微大一些的项目来说,还是推荐使用IDE。这一篇文章我们在m2eclipse帮助下使用eclipse创建Maven项目。
1. 创建
使用IDE创建一个Maven项目非常简单,选择菜单项File->New->Other,在弹出的对话框中选择Maven下的Maven Project,然后点击Next按钮。 1.png
图1
在弹出的New Maven Project对话框中,使用默认的选项。不要选择Create a simple project。然后,点击Next按钮。 2.png
图2 
此时m2eclipse会提示我们选择一个Archetype。这里选择maven-archetype-quickstart,再点击Next按钮。m2eclipse实际上是使用maven-archetype-plugin插件创建项目。 3.png
图3
此时输入groupId,artifactId,version,package。 4.png
图4
单击Finshi按钮,Maven项目就创建完成了。项目目录结构如下: 5.png
图5
2. 运行mvn命令
在Maven项目或者pom.xml上右击,再点击弹出的快捷菜单选择Run as,就能看到常见的Maven命令。 6.png
图6
选择想要执行的maven命令技能执行相应的构建,

推荐:Maven实战(一)--Why Maven

1.概述 Maven有如下优点: Maven是一个强大的构建工具,能够帮我们自动化构建过程,从清理、编译、测试到生成报告,再到打包和部署。 Java的一大优势是现成的类

同时也能在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.png
图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了。  
9.png
8.png
   
    
     [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实战》



推荐:[Maven实战](3)Maven创建项目

前两篇文章我们已经大概了解并安装好了Maven。现在我们开始使用Maven创建项目,在此我们创建一个最简单的Hllo World项目。让我们对Maven有一个初步的认识,对于

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

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。