Prepare agent remotely

46 views
Skip to first unread message

kry...@gmail.com

unread,
Apr 1, 2020, 2:50:34 AM4/1/20
to JaCoCo and EclEmma Users
How does the prepare agent goal work in Maven when you want to connect to it remotely?

This is what I had in mind. I will build my application with Maven and use jacoco-maven-plugin as a dependency. But when this jacoco agent will be on the server side (remotely), I need to connect to it. Can this be done with my local Maven (not connecting but enabling those properties) via the prepare-agent, e.g. in the pom file:

<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>jacoco-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>test</phase>
<configuration>
<address>0.0.0.0</address>
<port>5000</port>
<output>tcpserver</output>
<append>false</append>
<dumpOnExit>false</dumpOnExit>
</configuration>
</execution>

or do I need sort of create a script similar to "java -javaagent:[yourpath/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]?" that will listen in my docker container?

And then, I just dump the coverage file by using the jacococli.jar application by connecting to the agent.

So the question is: is it possible to achieve to have an agent via dependency and some configuration in my local Maven to have this agent listening on some address and port without copying the agent directly into the application with preconfigured parameters?

Marc Hoffmann

unread,
Apr 1, 2020, 4:00:08 AM4/1/20
to jac...@googlegroups.com
Hi,

as said before:

1) The agent needs to be present and configured on the machine where your JVM under test is running
2) Our jacoco-maven-plugin does not support remote deployments

If you deploy with docker a option is that in your Dockerfile you download the agent and start your JVM with the correct agent parameters.


Regards,
-marc


--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/aa77fbe3-9232-4367-b711-93761e848ec1%40googlegroups.com.

kry...@gmail.com

unread,
Apr 1, 2020, 4:36:05 AM4/1/20
to JaCoCo and EclEmma Users
Well, I quite understood that, but my question was, why do you have a maven documentation about the prepare-agent if it has no purpose? I thought, that when you package maven with mvn package, he will download the agent with predefined parameters as can be seen in my configuration from the pom file. Then all that would be needed is to copy over the agent in e.g. the dockerfile.

As you can see, I quite don't understand what the prepare-agent is supposed to do? As you mention, I will have to add manually the configuration in e.g. a script, something like java -javaagent:path/to/dependency/jacocoagent.jar=options-comes-here.
To unsubscribe from this group and stop receiving emails from it, send an email to jac...@googlegroups.com.

Marc Hoffmann

unread,
Apr 1, 2020, 4:40:21 AM4/1/20
to jac...@googlegroups.com
It has the purpose to provide a *local* copy of the agent to use in a *local* jvm.

Sure you can parse the local path from generated parameter and use this as a copy source if you think this is easier than using the direct link on the target system.


To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/3ec0c2bf-ff04-49e4-a4e2-5b6bf628e033%40googlegroups.com.

kry...@gmail.com

unread,
Apr 1, 2020, 4:53:16 AM4/1/20
to JaCoCo and EclEmma Users
Sir, thank you for your quick replies.

I'm not quite following. Does this mean, as I explained (and forgive me being unclear), that all I need to do will be to copy the org.jacoco.agent-0.8.5-runtime.jar (or all of the deps. so I will get all necessary files) into my remote server. And if the script that I will copy will look something like java -javaagent:path/to/dependency/jacocoagent.jar [my java app main class] I do not need to provide any more parameters, because that is already preconfigured thanks to the prepare-agent in the pom file?

Thus, I do not need to create my own configuration with the jacocoagent.jar, e.g. java -javaagent:org.jacoco.agent-0.8.5-runtime.jar=options-here... and pass this one to my remote server, because when I packaged the maven project, it is already preconfigured?

One more quick question, when using your agent, do you always need to provide the main class name of the application: java -javaagent:path/to/dependency/jacocoagent.jar [my java app main class]?

Marc Hoffmann

unread,
Apr 1, 2020, 5:02:19 AM4/1/20
to jac...@googlegroups.com
Hi,

1) the JaCoCo agent does not require additional dependencies. So it is the only file you need on your remote machine
2) The prepare agent goal downloads a copy of the agent to the local machine and create a reads-to-use JVM parameter (-javaagent:<local temp path>=<options>). It is a convenience tool for the typical case where the VM under test is launched directly from the Maven build on the same machine.
3) The agent is simply a additional vm parameter. If you have a working vm startup you can simply add the parameter.

Regards,
-marc

To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/39f18399-8de2-410c-abbf-f1fc111cfd7a%40googlegroups.com.

kry...@gmail.com

unread,
Apr 3, 2020, 6:18:14 AM4/3/20
to JaCoCo and EclEmma Users
Please forgive me my stupidity. Your last comment, nr. 3, can you start the agent without using a main class? E.g. a normal scenario as I've interpreted the agent would be, java- javaagent:/jacocoagent.jar=[options] com.example.myapp.HelloWorld, but is it possible to start the agent through Dockerfile with a CMD ["sh", "-c", "./script.bash [similar to the above for the jacocoagent]"] without adding any main class to the jvm? With other words, if you have a docker container running a java application, can you trigger the agent with the CMD above without adding any main class or must you always provide a main class to be added to the jacocoagent so it can do the coverage?

Evgeny Mandrikov

unread,
Apr 3, 2020, 11:55:13 AM4/3/20
to JaCoCo and EclEmma Users


On Friday, April 3, 2020 at 12:18:14 PM UTC+2, kry...@gmail.com wrote:
Please forgive me my stupidity. Your last comment, nr. 3, can you start the agent without using a main class? E.g. a normal scenario as I've interpreted the agent would be, java- javaagent:/jacocoagent.jar=[options] com.example.myapp.HelloWorld, but is it possible to start the agent through Dockerfile with a CMD ["sh", "-c", "./script.bash [similar to the above for the jacocoagent]"] without adding any main class to the jvm? With other words, if you have a docker container running a java application, can you trigger the agent with the CMD above without adding any main class or must you always provide a main class to be added to the jacocoagent so it can do the coverage?

Let me quote some page from many on the internet - https://www.journaldev.com/12552/public-static-void-main-string-args-java-main-method
Java main method is the entry point of any java program.

Back to your example this means that your application already has main class.
And somewhere in your "script.bash" there is already command "java".
Whether or not and how you can pass "-javaagent" from cmd that calls "script.bash" down to this "java" command is not a question for JaCoCo developers, but for developers of your "script.bash" - please talk with them.

kry...@gmail.com

unread,
Apr 4, 2020, 7:05:53 AM4/4/20
to JaCoCo and EclEmma Users
(Ehum, cough, cough, ohh boy, I just hope no one pays attention to my incompetence.)

Sirs, thank you for your patience. Let's do some easy examples, and forget about everything else. I fully understand that the main class needs to have a main method to start the application. I have also read your manuals for the agent and the cli, so I quite know how to use them, but am a bit, maybe, confused, how the agent works.

I have a simple java application, HelloWorld.class which consists of a class and a main method that waits for an input:

System.out.println("Say something:");
System.console().readLine();

Normal scenario to invoke your agent would be java -javaagent:agent.jar=address=localhost,port=500,output=tcpserver HelloWorld
This will make a successful code coverage due to the application is constantly running until input.

Now, would the same scenario be possible without a java class? java -javaagent:agent.jar=[options]

Next example, to understand better with simple examples provided (and this does not mean that you have misunderstood me), if the above java application is already running without the agent, would it be possible to attach this agent to this application without providing any class? Or, when I start any java application, I also need to start the agent simultaneously with the application so he can listen on the ip and port, otherwise, he will not have any connection whatsoever to that simple application?

Please forgive me if I am just confusing everything, because personally I believe the agent simultaneously needs to be provided a class as an argument to the jvm (java -javaagent:agent.jar=[options] class).

Evgeny Mandrikov

unread,
Apr 4, 2020, 7:13:21 AM4/4/20
to JaCoCo and EclEmma Users

On Saturday, April 4, 2020 at 1:05:53 PM UTC+2, kry...@gmail.com wrote:
java application is already running without the agent, would it be possible to attach this agent to this application without providing any class?

NO - you can not attach JaCoCo agent to already running Java application
 
start the agent simultaneously with the application

YES

kry...@gmail.com

unread,
Apr 4, 2020, 7:32:18 AM4/4/20
to JaCoCo and EclEmma Users
Thank you.

帅哥

unread,
Apr 4, 2020, 7:41:55 AM4/4/20
to JaCoCo and EclEmma Users
excuse me  
the code 
        InputStream is = this.memoryClassLoader.getResourceAsStream(resource);
        return is;
at CoverageCollector
always report is is nullpointer .I want to ask what's going on 
thanks


------------------ 原始邮件 ------------------
发件人: "kryman0"<kry...@gmail.com>;
发送时间: 2020年4月4日(星期六) 晚上7:32
收件人: "JaCoCo and EclEmma Users"<jac...@googlegroups.com>;
主题: Re: [java code coverage] Prepare agent remotely
--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/295a5970-5a54-4435-8b24-81cd2869126c%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages