[aspectj-users] Agent Embedder Maven Plugin and AspectJ load-time weaving

2024-03-24 Thread Alexander Kriegisch via aspectj-users
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 Kriegischhttps://scrum-master.de
___
aspectj-users mailing list
aspectj-users@eclipse.org
To unsubscribe from this list, visit 
https://www.eclipse.org/mailman/listinfo/aspectj-users


[aspectj-users] AspectJ 1.9.22 release (Java 22)

2024-03-24 Thread Alexander Kriegisch via aspectj-users
Dear AspectJ users,
we are pleased to announce AspectJ release. AspectJ 1.9.22 supports Java 22, its final and preview features, such as:



JEP 456: Unnamed Variables & Patterns
JEP 459: String Templates (Second Preview)
JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview)
JEP 447: Statements before super(...) (Preview)

The following Java 22 features are API or JVM only, therefore irrelevant for the compiler and should just work out of the box:

JEP 423: Region Pinning for G1
JEP 454: Foreign Function & Memory API
JEP 458: Launch Multi-File Source-Code Programs
JEP 462: Structured Concurrency (Second Preview)
JEP 464: Scoped Values (Second Preview)
JEP 457: Class-File API (Preview)
JEP 461: Stream Gatherers (Preview)
JEP 460: Vector API (Seventh Incubator)

Usage hints



Since 1.9.21, the AspectJ compiler AJC (contained in the aspectjtools library) no longer works on JDKs 11 to 16. The minimum compile-time requirement is now JDK 17 due to upstream changes in the Eclipse Java Compiler (subset of JDT Core), which AspectJ is a fork of. You can still compile to legacy target versions as low as Java 1.3 when compiling plain Java code or using plain Java ITD constructs which do not require the AspectJ runtime aspectjrt, but the compiler itself needs JDK 17+. Just like in previous AspectJ versions, both the runtime aspectjrt and the load-time weaver aspectjweaver still only require JRE 8+. History: Since 1.9.7, the AspectJ compiler AJC needed JDK 11+, before then JDK 8+.
Since AspectJ 1.9.21.1, using --add-opens is no longer necessary for load-time weaving on JDK 16+! The additional JVM command-line option was necessary in all AspectJ versions up to 1.9.21.

Resources

For more detailed release information, please read the release notes.
The current artifacts (runtime, weaver, compiler, matcher) are available on Maven Central.
The AspectJ installer can be found on GitHub.
There is also an AJDT update site for Eclipse 4.31 (2024-03). Unfortunately, updates sites for previous Eclipse versions are no longer compatible with AspectJ 1.9.22, because the latter depends on upstream Eclipse changes. So if you want to use ADJT builds with Eclipse IDE, you need to upgrade to 2024-03. Otherwise, you can only use AspectJ 1.9.22 via Maven build, not via direct IDE.
Documentation for AspectJ can be found here on the Eclipse project website.
There also is a short guide describing options for setting up a development environment.
See here for more information about how to upgrade to the latest AspectJ version when using dev.aspectj:aspectj-maven-plugin:1.14.

Enjoy AspectJ!
The AspectJ team

___
aspectj-users mailing list
aspectj-users@eclipse.org
To unsubscribe from this list, visit 
https://www.eclipse.org/mailman/listinfo/aspectj-users