How can I weave AspectJ in external jars with build.gradle?
I wanna replace a method in external jar(a library called j2mod) by AspectJ’s @Around.
I’m using Java 11, build.gradle(gradle-7.1.1).
So far I asked GPT-4 and got codes below. Building and execution of the project was successful(no error detected), but aspect is not weaved into j2mod.
Please tell me what is wrong in this code.
Thanks.
plugins {
id 'io.freefair.aspectj' version '6.2.0'
}
apply plugin: 'io.freefair.aspectj'
bootJar {
launchScript()
}
dependencies {
implementation 'org.aspectj:aspectjrt:1.9.7'
implementation 'com.ghgande:j2mod:3.1.1'
}
configurations {
aspectpath
inpath
}
def j2modJar = configurations.runtimeClasspath.files.find {
it.name.contains('j2mod')
}
dependencies {
aspectpath 'org.aspectj:aspectjtools:1.9.7'
inpath files(j2modJar)
}
def weavedClassesDir = "$buildDir/weaved-classes"
task weave(type: JavaExec) {
main = 'org.aspectj.tools.ajc.Main'
classpath = configurations.aspectpath + configurations.compileClasspath
args = [
'-inpath', configurations.inpath.asPath,
'-aspectpath', sourceSets.main.output.asPath,
'-d', weavedClassesDir,
'-source', '11',
'-target', '11',
'-showWeaveInfo'
]
doFirst {
new File(weavedClassesDir).mkdirs()
}
}
compileJava.finalizedBy(weave)
task buildWeavedJar(type: Jar, dependsOn: weave) {
from zipTree(weavedClassesDir)
archiveClassifier.set('weaved')
destinationDirectory.set(file("$buildDir/libs"))
}
assemble.dependsOn buildWeavedJar
and this is how the Java code using aspectj looks like.
@Aspect
public class ExampleAdvice {
@Around("execution(* com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersRequest.createResponse(..))")
public ModbusResponse exampleAdvice(ProceedingJoinPoint pjp) {
// make desirable return value
return response;
}
}
Welcome to SO. Please describe what you did yourself to solve the problem other than let ChatGPT hallucinate a faulty solution for you. How much do you know about AspectJ? Do you e.g. know how to solve the same problem in Maven and just want to port it to Gradle? Have you ever used AspectJ before? Did you study the Freefair documentation? What does your aspect source code look like? Can you provide an MCVE?
Sorry for asking a lot of questions, but I really want to know. In the past, many lazy developers simply used minimal research to find code snippets, then copied & pasted them into their projects. If they did not work, they immediately asked here without any research effort on their own part. Laziness 2.0 or copy & paste programming 2.0 seems to be not to even use Google to find information, but simply to let ChatGPT do the job and then ask humans to fix that in order to avoid lifting a finger. So please, with all due respect: Describe how you lifted your finger. Thank you.
> How much do you know about AspectJ? Do you e.g. know how to solve the same problem in Maven and just want to port it to Gradle? – I (think I) know what can I do using AspectJ but I don’t know how. So I neither know how to solve this in Maven. > Have you ever used AspectJ before? – I’ve used it indirectly by using Spring AOP. So I know what to write in Java code but I don’t know that in AspectJ code. If possible, I want to solve the problem by gradle settings because I don’t want to make build jar procedure complex.
> Did you study the Freefair documentation? – I’ve never. I’ve studied Eclipse AspectJ Documentation roughly and found no referrence about gradle, so I’m gonna study Freefair documentation. Thank you. > What does your aspect source code look like? – I added it in the question, but it’s java code. > Can you provide an MCVE? – I’m starting to learn Freefair documentation to know how I write build.gradle. It will take time to provide preferrable example.
In my understanding, I need io.freefair.aspectj.post-compile-weaving to weave aspectj into external jars (that’s obvious because there’s no source code to weave into), but I haven’t get what to write settings in build.gradle. I’ve found a kind of solution here and it worked. It’s necessary to use inpath(${project}) to set weave target. I’m gonna close my question when I get time.
Show 1 more comment