码迷,mamicode.com
首页 > 编程语言 > 详细

Maven 学习笔记——将普通的Java项目转换成Maven项目(3)

时间:2015-02-06 18:18:32      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:

         将一个普通的java项目转换成Maven项目并不是一个很大的任务,仅仅只需要下面的几步就能将转换成功。下面我是用一个简单的Selenium测试小demon作为例子来说的。

  1. 移调项目中所有关联的Libraries
  2. 将Selenium项目转换成Maven项目
  3. 增加Maven依赖库到项目中
  4. 创建Maven的测试文件夹架构

Step1:移调项目中所有关联的Libraries

      选中你需要转换的Project:Build Path > Configure Build Path

技术分享

 

Step2: 将Selenium项目转换成Maven项目

     1) 右键需转换的项目,Configure > Convert Maven Project

     2) 要求填入Group ID和Artifact ID,这个不需要改变,用默认项目提供的

技术分享

 

   3) 转换成功后,该项目在Eclipse中的结构变成下面这样

 

      技术分享             技术分享

                    没转换前的结构                                                                               转换后的结构

 

Step3:增加Maven依赖库到项目中

        现在我们就需要将项目中所需要的依赖包加到本地中心源中,我就针对这个项目来说,我们知道我们现在缺少Selenium jar包,junit jar包等等。

1) 到 http://www.mvnrepository.com/ 上面查找Selenium

2) 点击Selenium Java

技术分享

3) 选择你所需要使用的Selenium的版本

技术分享

4) 关于Selenium在Maven中信息就会显示出来,然后将这些信息加到pom.xml中

技术分享

5) 将Selenium的dependency的信息加到pom.xml,可以在Dependencies以流程的方式添加进去,保存,这个时候会下载相关的依赖包到本地源中心中。(需要一点时间,有任何问题,可以参考之前Maven(1)中提到,解决问题)

技术分享技术分享

6)直接在pom.xml中将查询到的信息拷贝到xml文件中,保存。

技术分享

7)等待依赖包被下载完后,所有的错误就会解决

技术分享

 

Step4 创建Maven的测试文件夹架构

接下来就是检验真理的时候,看这个转换的Selenium项目能否跑起来,选择pom.xml > Run As > Maven Test 结果-----------测试用例没有被执行,这是为什么呢?仔细看看log,发现Maven默认是在\src\main\resource这个路径下找的源代码资源,测试源代码是在\src\test\resources 。

输出结果为:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenConvertDemon1 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenConvertDemon1 ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Junit\MavenConvertDemon1\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenConvertDemon1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenConvertDemon1 ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Junit\MavenConvertDemon1\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenConvertDemon1 ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenConvertDemon1 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.999 s
[INFO] Finished at: 2015-02-06T16:22:09+08:00
[INFO] Final Memory: 6M/15M
[INFO] ------------------------------------------------------------------------

那么现在的目的就是怎么自定义Maven的目录结构?

下面这个代码是把源代码设置成src目录下,把测试的源代码放在src/test下面,这样修改后就解决刚才的问题了,测试就跑起来了。这样一个Selenium项目就转换成Maven项目了。

<build>
    <sourceDirectory>src/</sourceDirectory>
 
    <testSourceDirectory>src/test</testSourceDirectory>
</build>

成功后的输出结果:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenConvertDemon1 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenConvertDemon1 ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Junit\MavenConvertDemon1\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenConvertDemon1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenConvertDemon1 ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Junit\MavenConvertDemon1\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenConvertDemon1 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Junit\MavenConvertDemon1\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenConvertDemon1 ---
[INFO] Surefire report directory: D:\Junit\MavenConvertDemon1\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.TestSeleniumDemon1
Feb 6, 2015 4:32:55 PM org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
INFO: Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@9ffe3f
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 31.452 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 38.029 s
[INFO] Finished at: 2015-02-06T16:32:57+08:00
[INFO] Final Memory: 11M/26M
[INFO] ------------------------------------------------------------------------

 

PS;附一张Maven的标准目录结构:

                                                             技术分享

 

 

————————————————————————————————

WHOOOOSHHHHHHHHHHHH…………

Blimey what was that?

That was your life mate

Oh, I was not quite ready. Can I have another go?

Sorry mate, only one per person.

Maven 学习笔记——将普通的Java项目转换成Maven项目(3)

标签:

原文地址:http://www.cnblogs.com/taoSir/p/4277510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!