尝试通过 jacoco 使用离线仪器(不能使用即时仪器:由于 powermock 测试用例)为 maven 项目获取覆盖。将 jacocoagent.jar 添加到surefire插件的类路径中,如下所示。将“org.jacoco.agent-0.7.7.201606060606-runtime.jar”(来自本地 maven 存储库)重命名为“jacocoagent.jar”并将其保存在该 pom.xml 所在的同一文件夹中。我正在点击以下内容即使将其添加到类路径后也会出现异常。
pom.xml 的片段(surefire - 插件配置)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkmode>once</forkmode>
<additionalClasspathElements>
<additionalClasspathElement>jacocoagent.jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
控制台上看到的异常:
#############
Number of foreign imports: 1
import: Entry[import from realm ClassRealm[maven.api, parent: null]]
-----------------------------------------------------
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:166)
... 21 more
Caused by: java.lang.NoClassDefFoundError: org/jacoco/agent/rt/internal_6da5971/Offline
at com.cisco.ise.ups.modelframework.hibernate.OracleNamingStrategy.$jacocoInit(OracleNamingStrategy.java)
at com.cisco.ise.ups.modelframework.hibernate.OracleNamingStrategy.<clinit>(OracleNamingStrategy.java)
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:142)
at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1082)
at java.lang.reflect.Field.getFieldAccessor(Field.java:1063)
at java.lang.reflect.Field.get(Field.java:387)
at com.cisco.ise.ups.build.WorkflowRunnerMojo.namingStrategyInstance(WorkflowRunnerMojo.java:335)
at com.cisco.ise.ups.build.WorkflowRunnerMojo.setupWorkflowEnvironment(WorkflowRunnerMojo.java:514)
at com.cisco.ise.ups.build.WorkflowRunnerMojo.execute(WorkflowRunnerMojo.java:816)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
... 21 more
Caused by: java.lang.ClassNotFoundException: org.jacoco.agent.rt.internal_6da5971.Offline
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
... 33 more
[ERROR]
###############
后续步骤:
- “mvn 编译”。
- “mvn org.jacoco:jacoco-maven-plugin:instrument”
- “mvn test” - 在这一步引发异常。
请让我知道如何摆脱这个异常?那是添加类路径的正确位置吗?(在surefire插件中)或者应该在哪里指定?
谢谢你。