Reputation: 71
I have a spring app using failsafe and to run IT's by launching the application jar through custom code, and there is no organizational appetite to have it run directly through maven.
To get test coverage, I need to add the flag
-javaagent:{mavenHome}/repository/org/jacoco/org.jacoco.agent/0.8.10/org.jacoco.agent-0.8.10-runtime.jar=destfile={targetLocation}/jacoco-it.exec
To build this string flag, I have to add
String mavenHome = System.getenv("MAVEN_HOME");
Also, this assumes that the path to the jacoco agent jar will always be the same. I also can't get the jacoco version number as it's located in.
My coworker told me to do a maven search in MAVEN_HOME/repository/org/jacoco/org.jacoco.agent
(which has all the versions number), and get the most recent version by finding the max version in that folder, but this seems so hacky. It's very "automagical" when you have maven launch the jar files, as it automatically adds this flag. But since our code is custom-launching jars through code, I need to create this flag myself. Are there any non-hacky ways to do this?
Upvotes: 0
Views: 29
Reputation: 71
I found the answer:
import java.lang.management.ManagementFactory;
import java.util.List;
// Get jacoco agent jvm argument.
List<String> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
String jacocoAgentArg = jvmArgs.stream()
.filter(arg -> arg.startsWith("-javaagent") && arg.contains("org.jacoco.agent"))
.toArray(String[]::new)[0];
Upvotes: 0