I have a spring-boot project where I want to enable loadTimeWeaving for entityManagerFactory.
I know that I can create a bean of InstrumentationLoadTimeWeaver
like below.
@Bean
public LoadTimeWeaver loadTimeWeaver() {
return new InstrumentationLoadTimeWeaver();
}
and it works well when I add --javaagent:<path-to-spring-instrument.jar>
.
Command
java --javaagent:~/.m2/repository/org/springframework/spring-instrument/<version>/spring-instrument-<version>.jar -jar myjar.jar
There are two problems with the above approach-
- I have to hardcode the version managed by spring-boot-starter-parent.
- Adding javaagent at multiple deploy scripts as well as IDE config.
I came across agent-embedder-maven-plugin where I can embed javaagent as below.
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>agent-embedder-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>embed-agent</id>
<goals>
<goal>embed</goal>
</goals>
<configuration>
<javaAgents>
<agent>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
</agent>
</javaAgents>
</configuration>
</execution>
</executions>
</plugin>
But it doesn’t work while running the tests and I have to manage two configs
- Explicitly use javaagent for test
- Use plugin for executing jar for deployment
How can I use agent-embedder-maven-plugin for running tests as well?
Currently, I am running test by adding configuration to surefire plugin like below –
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<argLine>
--add-opens java.base/java.lang=ALL-UNNAMED
-javaagent:${user.home}/.m2/repository/org/springframework/spring-instrument/${spring-instrument.version}/spring-instrument-${spring-instrument.version}.jar
</argLine>
</configuration>
</plugin>
1
I think, for regular tests your Surefire configuration with -javaagent
is fine. By the way, --add-opens
should no longer be necessary with the more recent AspectJ versions.
If you want to test with the packaged artifact, this is to be considered an integration test. Use Failsafe for that, not Surefire. It runs in the verify
phase, i.e., after package
, and you can use the packaged artifact in your tests.