4

I use application plugin in gradle (v1.10) to package and to run my apps.

So, now I need to use aspects (aspectj), and I dont want to use aspectj-compiler (ajc).

Is it possible to tweak gradle application run scripts, so my app could be run with load-time-weaving? Something like providing jvm options:

-javaagent:_path_to_aspectj_weaver.jar

head_thrash
  • 1,623
  • 1
  • 21
  • 26

1 Answers1

5

Yep, that's done like this:

project(':whatever') {
    apply plugin: 'application'

    mainClassName = 'some.Main'
    repositories { mavenCentral() } 

    dependencies {
        // substitute needed version of aspectj
        runtime "org.aspectj:aspectjweaver:$aspectj" 
    }

    applicationDefaultJvmArgs = [
        "-javaagent:\$APP_HOME/lib/aspectjweaver-${aspectj}.jar"
    ]

    // $ symbol gets escaped in script anyway:( so we need to replace it.
    startScripts {
        doLast {
            unixScript.text = unixScript.text.replace('\\$APP_HOME', '\$APP_HOME') 
            // do something like this for Windows scripts also
        }
    }
}
head_thrash
  • 1,623
  • 1
  • 21
  • 26
  • This works for the distTar task, but somehow is also executing in the run task which causes the \$APP_HOME to fail resolving and prevents from running ./gradlew run – Christian Vielma Oct 28 '16 at 09:55
  • There's a gradle plugin to resolve exactly that problem: https://plugins.gradle.org/plugin/com.zoltu.application-agent – Matt F May 27 '20 at 17:50