Maven test and -Javaagent argument
Asked Answered
T

1

6

I have a simple java project with on Junit test case that has the current architecture:

pom.xml
src/main/java/com/Example.Java
src/test/java/com/ExampleTest.java

The contents of pom.xml are as follow:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com</groupId>
     <artifactId>SampleExample</artifactId>
     <packaging>jar</packaging>
  <version>1.0</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
  </dependencies>
</project>

To execute the tests I simply call mvn test from bash. This as expected will run the test. Now to my question:

specifying javagent outside of maven is simply done by specificity the -javaagent option. How do I do this within the maven framework such that when I execute mvn test the agent I specify is loaded? (i.e how do I add custom arguments that maven will pass to the 'java' command when the tests are executed)

Trull answered 19/9, 2017 at 14:7 Comment(0)
A
8

Define the Surefire plugin in your POM and pass the JVM arg via Surefire's configuration.

For example:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
    <configuration>
        <argLine>-javaagent:/path/to/javaagent</argLine>
    </configuration>
</plugin>

For background: the test goal is bound to surefire:test by default (more details on default bindings here) so you have been already been running the Surefire plugin perhaps without realising it. The only change now is that you need to change the configuration of the Surefire plugin and this requires you to include it explicitly in your POM as per the example I showed above. You cannot run the test goal without using the Surefire plugin and you cannot tell Surefire to use a JVM arg without configuring the Surefire plugin to do so.

Atp answered 19/9, 2017 at 14:14 Comment(2)
what is the surefire plugin exactly? whats its relation to mvn test? can this be done without this plugin? Thanks!Trull
@Trull I have updated the answer to include explain that your have been using Surefire all along :)Atp

© 2022 - 2024 — McMap. All rights reserved.