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

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

移调项目中所有关联的Libraries

将Selenium项目转换成Maven项目

增加Maven依赖库到项目中

创建Maven的测试文件夹架构

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

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

448d850e55a1fc4fbaf2b024d204c6c4.png

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

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

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

60e422521158d838b1558453a9450584.png

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

0bf71c5e8715967fd69447a46d5dc96a.png             5f2ece9413828f70a3dd984940313ac5.png

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

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

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

2) 点击Selenium Java

4c0f219ca80245802d959d502e61a86c.png

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

e67d53be1754811bb5391eb2858c6ea4.png

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

d48161d62a31b6f6850cbe7726e4dce6.png

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

ed4a8e5325a41185d30e39ce4f9759db.pngad42664d3c3a22bbdf276da3cd97338b.png

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

6ddbbffa04d86cc07394e867e6407358.png

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

0c4c78b36c330d1d40ec1c59939b5db6.png

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

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

输出结果为:

[INFO] Scanning forprojects...

[INFO]

[INFO]------------------------------------------------------------------------[INFO] Building MavenConvertDemon10.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] Totaltime: 2.999s

[INFO] Finished at:2015-02-06T16:22:09+08:00[INFO] Final Memory: 6M/15M

[INFO]------------------------------------------------------------------------

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

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

src/

src/test

成功后的输出结果:

[INFO] Scanning forprojects...

[INFO]

[INFO]------------------------------------------------------------------------[INFO] Building MavenConvertDemon10.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 hasnot been set, using platform encoding Cp1252, i.e. build is platform dependent![INFO] Compiling1 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

Feb6, 2015 4:32:55PM 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.452sec

Results :

Tests run:1, Failures: 0, Errors: 0, Skipped: 0[INFO]------------------------------------------------------------------------[INFO] BUILD SUCCESS

[INFO]------------------------------------------------------------------------[INFO] Totaltime: 38.029s

[INFO] Finished at:2015-02-06T16:32:57+08:00[INFO] Final Memory: 11M/26M

[INFO]------------------------------------------------------------------------

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

98e60cd028b0c13f7e56dae70061bbca.gif

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

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.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值