我正在努力让jacoco + gradle一起工作。看看这个论坛,似乎有些人已经成功地做到了这一点。但是当我尝试时,我遇到了一个奇怪的例外。
我做了什么:
1.Downloaded gradle 2.2.1和配置的env变量等
2.从http://www.eclemma.org/jacoco/
下载jacoco 0.7.13.added:
apply plugin: 'jacoco'
和
buildTypes {
debug
{
testCoverageEnabled true
...
build.gradle中的
4.run gradle build
5.我收到一条错误消息,说无法找到jacoco代理jar等。错误信息显示它试图搜索C:\ Program Files(x86)\ Android \ android-sdk \ extras \ android \ m2repository下的文件。 ..等等。
6.我手动解压缩jacoco jar文件并将它们放在提到错误消息的位置,错误消息消失。
7.然后我跑了gradle build。运行内置的instrumentDebug任务时出现以下新错误:
Caused by: : taskdef A class needed by class org.jacoco.ant.InstrumentTask cannot be found: org/jacoco/core/runtime/IExecutionDataAccessorGenerator using the classloader AntClassLoader[C:\Program Files (x86)\Android\android-sdk\extras\android\m2repository\org\jacoco\org.jacoco.ant\0.7.1.201405082137\org.jacoco.ant-0.7.1.201405082137.jar]
at org.apache.tools.ant.taskdefs.Definer.addDefinition(Definer.java:612)
at org.apache.tools.ant.taskdefs.Definer.execute(Definer.java:237)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.gradle.api.internal.project.ant.BasicAntBuilder.nodeCompleted(BasicAntBuilder.java:77)
at org.gradle.api.internal.project.ant.BasicAntBuilder.doInvokeMethod(BasicAntBuilder.java:92)
at com.android.build.gradle.internal.coverage.JacocoInstrumentTask.instrument(JacocoInstrumentask.groovy:51)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:218)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:211)
Caused by: java.lang.ClassNotFoundException:
at org.jacoco.core.runtime.IExecutionDataAccessorGenerator
at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1366)
at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1315)
at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1068)...
当我使用进程监视器(我的开发机器是Win 7)时,我看到无法访问org.jacoco.core-0.7.1.201405082137.jar,org.jacoco.ant.InstrumentTask类所在的位置。所以我认为gradle没有正确地将文件路径传递给org.apache.tools.ant.AntClassLoader。
我尝试过以下方法,但没有一种方法有效:
有谁知道如何解决这个问题?
提前致谢!
修改
主要目的是:
1。让jacoco构建一个检测的apk文件
2。手动测试apk文件(非自动测试)
第3。让jacoco生成覆盖率报告
更新 我刚刚在执行InstrumentDebug任务时发现,gradle使用以下命令启动一个新进程:
“C:\ Program Files \ Java \ jdk1.8.0_25 \ bin \ java.exe”-XX:MaxPermSize = 512m -Xmx2048m -Dfile.encoding = UTF-8 -Duser.country = CN -Duser.language = zh -Duser.variant -cp D:\ gradle-2.2.1 \ gradle-2.2.1 \ lib \ gradle-launcher-2.2.1.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 2.2.1 D:\ gradle-2.2.1 \ gradle-2.2.1 \ daemon 120000 744501ac-32c1-4930-82bd-59e0a9e2b92d -XX:MaxPermSize = 512m -Xmx2048m -Dfile.encoding = UTF-8 -Duser.country = CN -Duser.language = zh -Duser.variant
正如您所看到的,类路径是硬编码的,它会覆盖环境变量中定义的内容。因此无法找到所需的jacoco核心库,从而导致了这个问题。我现在正试图查看此进程的启动位置以及是否有更改-cp选项的方法。
UPDATE2: 我终于发现这个过程是由gradle-launcher-2.2.1.jar启动的。见下面的代码。我现在正在尝试查看是否可以更改DefaultModuleRegistry并使registry.getGradleHome()== null。 与此同时,任何成功使用过jacoco + gradle的人都可以告诉我你使用的是哪个版本的gradle?
public DaemonStartupInfo startDaemon()
{
DefaultModuleRegistry registry = new DefaultModuleRegistry();
Set<File> bootstrapClasspath = new LinkedHashSet();
bootstrapClasspath.addAll(registry.getModule("gradle-launcher").getImplementationClasspath().getAsFiles());
if (registry.getGradleHome() == null)
{
bootstrapClasspath.addAll(registry.getFullClasspath());
}
if (bootstrapClasspath.isEmpty()) {
throw new IllegalStateException("Unable to construct a bootstrap classpath when starting the daemon");
}
new JvmVersionValidator().validate(this.daemonParameters);
List<String> daemonArgs = new ArrayList();
daemonArgs.add(this.daemonParameters.getEffectiveJavaExecutable());
List<String> daemonOpts = this.daemonParameters.getEffectiveJvmArgs();
LOGGER.debug("Using daemon opts: {}", daemonOpts);
daemonArgs.addAll(daemonOpts);
daemonArgs.add("-cp");
daemonArgs.add(CollectionUtils.join(File.pathSeparator, bootstrapClasspath));
daemonArgs.add(GradleDaemon.class.getName());
daemonArgs.add(GradleVersion.current().getVersion());
daemonArgs.add(this.daemonDir.getBaseDir().getAbsolutePath());
daemonArgs.add(String.valueOf(this.daemonParameters.getIdleTimeout()));
daemonArgs.add(this.daemonParameters.getUid());
...
}
答案 0 :(得分:1)
看看这是否有帮助。 我没有使用Gradle 2.2.1,但这是我在Global Gradle文件中的内容(即$ GRADLE_HOME / init.d级别文件中的文件)。文件名可以是.gradle扩展名。
allprojects {
apply plugin: 'java'
apply plugin: 'pmd'
apply plugin: 'findbugs'
apply plugin: 'checkstyle'
apply plugin: 'jacoco'
//NOTE1: The following soureSet section is NOT required, if your folder structure follows what Gradle says where your main source should reside, where test (Unit tests) should reside, and where other like integrationTest (integration tests code reside). If your project structure doesn't follow the Gradle defined structure, then you can define that as my source code is not under src/main/java but is under src/java. The use of "sourceSet" section in this global file is only helping to use some conventional values in this global level file for ex: see integrationTest task and jacocoTestReport task below (you can't use those values if sourceSet is NOT defined in this file and if your project doesn't following the Gradle defined structure).
//NOTE2: Here in the global level Gradle file, I'm using values for sources for main, test, integrationTest etc as "dont_change_me" as I don't know what all projects (which will use this Gradle's global level file), will have what source code structure. The main / actual values of the sources for main, test and integrationTest task MUST be defined in the PROJECT's build.gradle file in sourceSets { main { java { srcDir 'src/java' } } } way.
sourceSets {
main {
java {
srcDir 'dont_change_me'
}
resources {
srcDir 'dont_change_me'
}
}
test {
java {
srcDir 'dont_change_me'
}
resources {
srcDir 'dont_change_me'
}
}
integrationTest {
java {
srcDir 'dont_change_me'
}
resources {
srcDir 'dont_change_me'
}
}
acceptanceTest {
java {
srcDir 'dont_change_me'
}
resources {
srcDir 'dont_change_me'
}
}
}
//...more code here
//...more code here
// The following is necessary to get code coverage info out. Compile with debug mode.
tasks.withType(Compile) {
options.debug = true
options.compilerArgs = ["-g"]
}
jacoco {
//toolVersion = "0.6.2.201302030002"
//toolVersion = "0.7.0.201403182114"
//toolVersion = "0.7.1.201404171759"
//This is latest than above, you may find later versions in online Maven repository.
toolVersion = "0.7.2.201409121644"
//OK I don't need the following folder to be created as I'll define my own.
// reportsDir = file("$buildDir/customJacocoReportDir")
}
//The following section is for UNIT tests (as build task in Gradle calls test task for free)
test {
maxParallelForks = 5
forkEvery = 50
ignoreFailures = true
// I want my reports (html) files to be created in a user defined folder UT(Unit test in build/reports/UT folder) and xml files (in user defined folder UT folder) under build/test-results/UT folder.
testReportDir = file("$buildDir/reports/tests/UT")
testResultsDir = file("$buildDir/test-results/UT")
//Following jacoco session will RUN in GRADLE's JVM session (during build / test time). This is different JVM than what many think of a runtime/Tomcat JVM session where we run a .war/.ear/etc file of an app to run that app and if you want to get code coverage of your main source code using non-unit tests from a Tomcat JVM, then see next task (integrationTest) as the following jacoco section in this "test" task is just for UNIT tests running in Gradle JVM session on a machine.
jacoco {
//NOTE: The following vars works ONLY with Gradle <= 1.6 version
// Create jacoco .exec file for Unit test in a user defined location
destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
//The following line is not that usesful acc. to my experience so commented it.
//classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
//NOTE: Following vars works only with versions >= 1.7 version of Gradle
//destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
// classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
}
}
task integrationTest( type: Test) {
//Always run tests
outputs.upToDateWhen { false }
//Ignore the failures if any during tests and don't mark the Gradle task as failed.
//You can comment this line if you want your gradle task to fail as soon as it finds any failing tests.
ignoreFailures = true
//This is telling Gradle that where it'll find class files from integration tests source code
testClassesDir = sourceSets.integrationTest.output.classesDir
//What path to use in classpath for integration tests
classpath = sourceSets.integrationTest.runtimeClasspath
//My custom location where I want my html reports files and xml result times of integration tests
testReportDir = file("$buildDir/reports/tests/IT")
testResultsDir = file("$buildDir/test-results/IT")
//Jacoco section in IT tests is NOT required here. Why as it'll never generage a coverage report this way as this way of using jacoco section in integrationTest task is telling Gradle to use jacoco in Gradle JVM and for getting code coverage you have to run jacoco/jacocoagent.jar in Target JVM (which is Tomcat or similar) by introducing jacocoagent.jar and other parameters for jacoco to Tomcat using one of Tomcat's -Dxxx option (see Jacoco help on how to do this). As the following is not required, I'm commenting the following jacoco code(otherwise if used, it'll always give you 0% coverage).
//jacoco {
//This works with 1.6
// destPath = file("$buildDir/jacoco/IT/jacocoIT.exec")
// classDumpPath = file("$buildDir/jacoco/IT/classpathdumps")
//Following works only with versions >= 1.7 version of Gradle
//destinationFile = file("$buildDir/jacoco/IT/jacocoIT.exec")
// classDumpFile = file("$buildDir/jacoco/IT/classpathdumps")
//}
}
jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
ignoreFailures = true
//Use any .exec file found before generating coverage report i.e. it'll give you combined coverage report if you have both jacocoUT.exec and jacocoIT.exec or other .exec files in build/jacoco/xx folders.
executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')
//executionData = files('build/jacoco/UT/jacocoUT.exec', 'build/jacoco/IT/jacocoIT.exec')
//executionData = files(['build/jacoco/UT/jacocoUT.exec', 'build/jacoco/IT/jacocoIT.exec'])
reports {
xml{
enabled true
//Following value is a file
destination "${buildDir}/reports/jacoco/xml/jacoco.xml"
}
csv.enabled false
html{
enabled true
//Following value is a folder
destination "${buildDir}/reports/jacoco/html"
}
}
//The following is an example of using Gradle conventional way of saying where is my main source code directory
//sourceDirectories = files(sourceSets.main.allJava.srcDirs)
sourceDirectories = files(['src/java', 'src/groovy'])
classDirectories = files('build/classes/main')
}
}
在Project的build.gradle中,如果项目结构与Gradle所说的不同,则定义sourceSets部分(带有实际值)。你定义了依赖关系(即对于编译你需要这个.jar,对于test或integrationTest,你需要这个.jar或.class来自main / test等)。
然后,如果你运行gradle clean build,你将以.exec文件的形式在build / jacoco / UT文件夹下获得UT(单元测试)的jacoco代码覆盖率数据。在build / reports / ... html文件夹下,你会找到jacoco main index.html,它会显示代码覆盖率报告。
如果您需要来自非单元测试(即集成测试等)的代码覆盖率报告,请将以下参数附加到目标JVM。我使用Tomcat,所以我附加了这个,其中testType是一个变量,我用它来查找/判断我是在运行IT(集成测试),AT(验收测试)还是ST(Selenium GUI测试)。
export PROJ_EXTRA_JVM_OPTS="-javaagent:tomcat/jacocoagent.jar=destfile=build/jacoco/${testType}/jacoco${testType}.exec,append=false"
在我使用的startTomcat.sh脚本中,您会注意到我正在使用上面的变量并将其传递给Tomcat的JVM,因为这是运行我的项目主.war / .ear文件的JVM使用Integration / Acceptance /其他非UNIT类型测试我希望我的代码covreage数据的源代码类文件:
## Tomcat command - JDK 1.6/Tomcat 6.0
TOMCAT_CMD="$JAVA_HOME/bin/java $TOMCAT_JVM_ARGS \
$OPTIT_JVM_ARGS \
$JPROF_JVM_ARGS \
$PROJ_EXTRA_JVM_OPTS \
org.apache.catalina.startup.Bootstrap $TOMCAT_CFG_FILE_ARGS start"
上面的变量需要在Tomcat启动脚本中存在,即当Tomcat启动时,它应该在其JVM会话中获得上述变量。完成此操作后,您必须运行“gradle integrationTest”然后停止Tomcat会话(只有它将代码覆盖率数据刷新到jacocoIT.exec文件),然后如果您运行“gradle jacocoTestReport”,它将会读取jacocoIT.exec文件和genreate jacoco代码覆盖率报告,包含您的集成/验收/ Selenium测试所涵盖的主要源代码。