gradle学习(21)-在eclipse中构建java项目
最后更新于:2022-04-01 06:52:06
## 1.下载gradle for eclipse插件
根据[前人文章](http://blog.csdn.net/caolaosanahnu/article/details/17022321)找到的链接下载的,就不多说了。下载完成后,重启eclipse。
## 2.创建gradle项目
创建新项目的时候会发现多了一个gradle选项:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e46691c45c.jpg)
选择Gradle Project后会等待一段时间,等创建完成后会生一个gradle项目,看一下目录结构:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e4669369c0.jpg)
这些项目的目录结构其实都在之前的学习中多少有些涉及,这里就不再过多的废话了。直接看build.gralde脚本里都写了啥吧。
~~~
//java插件
apply plugin: 'java'
//eclipse插件
apply plugin: 'eclipse'
//指定JDK版本,改成你系统中版本
sourceCompatibility = 1.5
//版本号
version = '1.0'
//jar包配置文件的一些属性设置
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
//jar来源定义为Maven的中央库
repositories {
mavenCentral()
}
//依赖
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
~~~
## 3.test任务
这个时候我们写一个任务来执行单元测试,这个时候有可能报JDK错误,[解决方法。](http://forums.gradle.org/gradle/topics/java_quickstart_problems)
由于eclipse执行gradle的方式让醉了,所以我还是选择命令行吧:
~~~
D:\eclipse\workspare\gradle_hello>gradle test
:compileJava
:processResources
:classes
:compileTestJava
:processTestResources
:testClasses
:test
BUILD SUCCESSFUL
Total time: 5.07 secs
~~~
然后我们看一下项目目录下的结构:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466946d0b.jpg)
生成了build目录,里面有classes、dependency-cache、resources、test-results、reports。主要来看一下我们生成的测试报告:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466a58b87.jpg)
## 4.加入JaCoco
想测试一下单元测试的代码覆盖率,在代码中添加如下内容:
~~~
//添加JaCoCo
apply plugin: 'jacoco'
jacoco{
toolVersion = "0.7.1.201405082137"
reportsDir = file("$buildDir/customJacocoReportDir")
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}
~~~
然后执行jacoco任务:
~~~
D:\eclipse\workspare\gradle_hello>gradle jacoco
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jacocoTestReport
Download https://repo1.maven.org/maven2/org/jacoco/org.jacoco.ant/0.7.1.20140508
2137/org.jacoco.ant-0.7.1.201405082137.pom
Download https://repo1.maven.org/maven2/org/jacoco/org.jacoco.core/0.7.1.2014050
82137/org.jacoco.core-0.7.1.201405082137.pom
Download https://repo1.maven.org/maven2/org/jacoco/org.jacoco.report/0.7.1.20140
5082137/org.jacoco.report-0.7.1.201405082137.pom
Download https://repo1.maven.org/maven2/org/ow2/asm/asm-debug-all/5.0.1/asm-debu
g-all-5.0.1.pom
Download https://repo1.maven.org/maven2/org/ow2/asm/asm-parent/5.0.1/asm-parent-
5.0.1.pom
Download https://repo1.maven.org/maven2/org/ow2/ow2/1.3/ow2-1.3.pom
Download https://repo1.maven.org/maven2/org/jacoco/org.jacoco.ant/0.7.1.20140508
2137/org.jacoco.ant-0.7.1.201405082137.jar
Download https://repo1.maven.org/maven2/org/jacoco/org.jacoco.core/0.7.1.2014050
82137/org.jacoco.core-0.7.1.201405082137.jar
Download https://repo1.maven.org/maven2/org/jacoco/org.jacoco.report/0.7.1.20140
5082137/org.jacoco.report-0.7.1.201405082137.jar
Download https://repo1.maven.org/maven2/org/ow2/asm/asm-debug-all/5.0.1/asm-debu
g-all-5.0.1.jar
BUILD SUCCESSFUL
Total time: 2 mins 5.018 secs
~~~
在build目录中会生成jacoco的目录报告:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466a69be6.jpg)
打开html文件显示如下:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466a79d3e.jpg)
这样我们就包含了jacoco的插件啦。
[](http://blog.csdn.net/itfootball/article/details/42710653#)[](http://blog.csdn.net/itfootball/article/details/42710653# "分享到QQ空间")[](http://blog.csdn.net/itfootball/article/details/42710653# "分享到新浪微博")[](http://blog.csdn.net/itfootball/article/details/42710653# "分享到腾讯微博")[](http://blog.csdn.net/itfootball/article/details/42710653# "分享到人人网")[](http://blog.csdn.net/itfootball/article/details/42710653# "分享到微信")