15

I would like to configure the maven sure fire plugin to start the unit testing jvm with the argument for a java agent. The agent jar file is being obtained from maven central so I want maven to automatically figure out the path to the agent's jar file.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
    <configuration>
        <argLine>-javaagent: How to reference an agent jar that is a depedency </argLine>
    </configuration>
</plugin>

How can I refer to the path to the agent which is a dependency of the project using maven co-ordinates?

3 Answers 3

11

You can copy one of your needed jars to a target destination. Then refer to that jar on your command line.

Here is an example (using log4j which is NOT a valid agent jar but just to show an example):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
        <execution>
            <id>copy-agent</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                        <version>1.2.14</version>
                        <outputDirectory>${project.build.directory}/agents</outputDirectory>
                        <destFileName>my-special-agent.jar</destFileName>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
    <configuration>
        <argLine>-javaagent:${project.build.directory}/agents/my-special-agent.jar</argLine>
    </configuration>
</plugin>
Sign up to request clarification or add additional context in comments.

Comments

11

@wozza-xing gives a far superior solution to copying the jar around. Complete XML snippet:

 <plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>properties</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <argLine>-javaagent:${net.bytebuddy:byte-buddy-agent:jar}</argLine>
    </configuration>
  </plugin>

5 Comments

To be highlighted: Without the maven-dependency-plugin, the replacement into variables does not work.
@kap how is this supposed to work, since the goal only creates properties for the project dependencies, not the plugin dependencies. Now I must make bytebuddy a dependency on my project?
Well, if you want to use byte-buddy then you must make it a dependency. But I must admit I don't get why you want to add plugin dependencies as agents. Doesn't make sense for me.
This is basically the same as mentioned in Mockitos documentation. However, when I do that and do a simple mvn clean install then the forked JVM crashes as it can't find the agent dependency, which furthermore doesn't seem to get resolved in first place as the argline string printed to the console still says .. -javaagent:${org.mockito:mockito-core:jar} ... instead of the replaced value which the /bin/sh -c commands probably can't resolve. Replacing the ${...} with the absolut path would work though
oh, I noticed that I have specified both the maven-dependency-plugin as well as the maven-surefire-plugin as part of the pluginManagement section. When doing so then the maven-dependency-plugin needs to be referenced via <plugin><groupId>org.apavche.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactid></plugin> setting in the plugins portion of course as well.
6

Use

The properties mojo of the dependency plugin.

<argLine>-javaagent:${org.springframework:spring-instrument:jar}</argLine>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.