Maven 测试和 -Javaagent 参数
Maven test and -Javaagent argument
我有一个简单的 java 项目,其中包含具有当前架构的 Junit 测试用例:
pom.xml
src/main/java/com/Example.Java
src/test/java/com/ExampleTest.java
pom.xml内容如下:
<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>
为了执行测试,我只需从 bash 调用 mvn test。这正如预期的那样将 运行 测试。现在回答我的问题:
在 maven 之外指定 javagent 只需通过特定 -javaagent 选项即可.我如何在 maven 框架内执行此操作,以便在执行 mvn test 时加载我指定的代理? (即如何添加 Maven 将在执行测试时传递给 'java' 命令的自定义参数)
在您的 POM 中定义 Surefire 插件并通过 Surefire 的配置传递 JVM arg。
例如:
<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>
对于背景:test
目标默认绑定到 surefire:test
(有关默认绑定的更多详细信息 here),因此您已经 运行 Surefire 插件也许没有意识到。现在唯一的变化是您需要更改 Surefire 插件的配置,这需要您按照我上面显示的示例将其 明确地 包含在您的 POM 中。如果不使用 Surefire 插件,您就无法 运行 test
目标,并且如果不配置 Surefire 插件,您就无法告诉 Surefire 使用 JVM arg。
我有一个简单的 java 项目,其中包含具有当前架构的 Junit 测试用例:
pom.xml
src/main/java/com/Example.Java
src/test/java/com/ExampleTest.java
pom.xml内容如下:
<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>
为了执行测试,我只需从 bash 调用 mvn test。这正如预期的那样将 运行 测试。现在回答我的问题:
在 maven 之外指定 javagent 只需通过特定 -javaagent 选项即可.我如何在 maven 框架内执行此操作,以便在执行 mvn test 时加载我指定的代理? (即如何添加 Maven 将在执行测试时传递给 'java' 命令的自定义参数)
在您的 POM 中定义 Surefire 插件并通过 Surefire 的配置传递 JVM arg。
例如:
<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>
对于背景:test
目标默认绑定到 surefire:test
(有关默认绑定的更多详细信息 here),因此您已经 运行 Surefire 插件也许没有意识到。现在唯一的变化是您需要更改 Surefire 插件的配置,这需要您按照我上面显示的示例将其 明确地 包含在您的 POM 中。如果不使用 Surefire 插件,您就无法 运行 test
目标,并且如果不配置 Surefire 插件,您就无法告诉 Surefire 使用 JVM arg。