Attaching Java Agent to Tests Using Custom Plugin

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Attaching Java Agent to Tests Using Custom Plugin

James Owen
Hi,

I am currently trying to write a custom plugin that does two things

 1. Attach a java agent to the JVM when the tests are running, causing
    some instrumentation to be logged
 2. Analyze the logged instrumentation and report back to the user

I have the second part working, but I'm completely stumped by the first
part. My understanding is that applying JVM arguments during tests is
the responsibility of surefire, using the argLine configuration
parameters - so I am trying to write a mojo that will add those
parameters on the fly if they are not already there.

As it stands, I have written a mojo (InjectAgentMojo) that does the
following:

  * Get the project from a parameter
  * Look up the surefire plugin using
    project.getPlugin(org.apache.maven.plugins:maven-surefire-plugin)
  * Create a configuration with the required elements, and attach it
    using setConfiguration

However, this does not seem to have any effect, and the configuration is
not being used. I am calling the mojo in the INITIALIZE phase of my test
project, but it seems that the configuration has already been set
elsewhere by then.

Does anyone have any ideas that would make this possible? Specifically:

  * Is there a way for maven plugins to set the configuration of other
    plugins at runtime?
  * Is there another way this could be done - writing a plugin for
    surefire or something?

After a couple of days of experimenting/googling, I appear to have hit a
brick wall.

Thanks in advance for any help.

James Owen

Reply | Threaded
Open this post in threaded view
|

Re: Attaching Java Agent to Tests Using Custom Plugin

Thomas Broyer-2
Have a look at how JaCoCo does it, as AFAICT it has exactly the same needs:
https://www.eclemma.org/jacoco/trunk/doc/maven.html (source code:
https://github.com/jacoco/jacoco/tree/master/jacoco-maven-plugin/src/org/jacoco/maven
)

TL;DR: prepare-agent goal sets a property that you can then use to
configure Surefire.

On Tue, Oct 16, 2018 at 6:23 PM James Owen <[hidden email]> wrote:

> Hi,
>
> I am currently trying to write a custom plugin that does two things
>
>  1. Attach a java agent to the JVM when the tests are running, causing
>     some instrumentation to be logged
>  2. Analyze the logged instrumentation and report back to the user
>
> I have the second part working, but I'm completely stumped by the first
> part. My understanding is that applying JVM arguments during tests is
> the responsibility of surefire, using the argLine configuration
> parameters - so I am trying to write a mojo that will add those
> parameters on the fly if they are not already there.
>
> As it stands, I have written a mojo (InjectAgentMojo) that does the
> following:
>
>   * Get the project from a parameter
>   * Look up the surefire plugin using
>     project.getPlugin(org.apache.maven.plugins:maven-surefire-plugin)
>   * Create a configuration with the required elements, and attach it
>     using setConfiguration
>
> However, this does not seem to have any effect, and the configuration is
> not being used. I am calling the mojo in the INITIALIZE phase of my test
> project, but it seems that the configuration has already been set
> elsewhere by then.
>
> Does anyone have any ideas that would make this possible? Specifically:
>
>   * Is there a way for maven plugins to set the configuration of other
>     plugins at runtime?
>   * Is there another way this could be done - writing a plugin for
>     surefire or something?
>
> After a couple of days of experimenting/googling, I appear to have hit a
> brick wall.
>
> Thanks in advance for any help.
>
> James Owen
>
>