Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Agent Embedder Maven Plugin and AspectJ load-time weaving

Hi AspectJ community!

Over the years, it has been a FAQ, both on Stack Overflow and by people contacting me directly:

I have an executable JAR (e.g. Spring Boot fat JAR), which I am starting with

java -javaagent:/path/to/aspectjweaver.jar -jar my_app.jar

Is there any way to add the AspectJ weaver and automatically start it when executing the JAR, so I do not have to use the -javaagent parameter on the command line?

The good news first: Yes, since JRE 9 Java instrumentation provides the Launcher-Agent-Class mechanism, which you can leverage to embed any Java agent into an executable JAR and automatically launch the agent before the application in the JAR is started.

The bad news are: The JRE feature is not so easy to use, because

  • the java agent JAR cannot just be embedded into the containing executable JAR as a nested JAR but needs to be unpacked before embedding it,
  • the executable JAR's manifest needs to be adjusted to make it aware of the embedded agent,
  • the JRE only supports launching a single embedded Java agent in contrast to the JVM command line supporting multiple ones,
  • the started agent cannot receive any option string in contrast to JVM command line agents via -javaagent:path/to/agent.jar=option1=one,option2=two,
  • build plugins like the Spring Boot Maven Plugin do not support embedding agents.

Agent Embedder Maven Plugin to the rescue! The plugin embeds one or multiple(!) java agents, takes care of unpacking them and modifying the manifest. You can even specify an option string per agent, i.e. the functionality is on par with the JVM command line. The multi-agent feature is achieved by a tiny launcher agent embedded into the executable JAR along with the actualy user agents. The launcher launches all your agents and passes to them the options you specified in your build plugin configuration.

How does that help AspectJ LTW and Spring (Boot) users? Well, aspectjweaver.jar is one such aava agent you can embed into your executable (Spring Boot) JAR to be launched automatically. If you make sure to use a version 1.9.21.1 or newer - I recommend the latest 1.9.22 released earlier today - you do not even need to specify the infamous --add-opens parameter when running your application on JDK 16+ anymore. Simply launch your application with e.g. java -jar my-spring-boot.jar and enjoy native AspectJ weaving. You do not even need to configure anything else in Spring to activate it, because Spring detects the active weaving agent automatically.

Before you ask:

  • No, it does not work on JRE 8 or older, because the JVM only supports Launcher-Agent-Class since JRE 9. But you can run the Maven build on JRE 8 and also compile to target 8, if you like, as long as you run your executable JAR on JRE 9+.
  • Yes, in theory a similar build plugin could also be implemented for Gradle or other build tools. But being a Maven person, I only did it for Maven. I hope that other developers will step up to fill in the gaps.
  • No, I did not test the plugin with signed JARs.

If you experience any problems or have usage questions, please use GitHub discussions. If you think you found a bug or have a feature request, GitHub issues.

Enjoy

--
Alexander Kriegisch
https://scrum-master.de


Back to the top