Hey guys,
If I add these dependencies to my project
dependencies{
compile project(':my-lib')
compile 'com.example:another-lib:xxx'
}
Is it possible to get their .class files during build time? Actually i want to manipulate some .class files from dependencies.
One approach is using jar handler to open jar files, manipulate them and replace with old ones, but i don’t know it’s the best way or not.
Thanks in advance.
configurations {
tweakMe { transitive = false }
}
dependencies {
tweakMe project(':my-lib')
tweakMe 'com.example:another-lib:xxx'
}
task tweakClasses {
inputs.files configurations.tweakMe
doLast {
FileTree classFiles = configurations.tweakMe.matching {
include '**/*.class'
}
FileTree jarFiles = configurations.tweakMe.matching {
include '**/*.jar'
}
jarFiles.each { jarFile ->
classFiles += zipTree(jarFile).matching {
include '**/*.class'
}
}
doStuff(classFiles)
}
}
1 Like
Thanks, but i want the running app takes effect of the manipulated classes for example my-lib
has a class like this
public class Foo{
public void bar(){}
}
the tweakClasses
task will transform it to
public class Foo{
public void bar(){
System.out.println("hello world");
}
}
So how can i replace new classes in their jar files? Can i do something like this?
def doStuff (FileTree classFiles){
...
classFiles.files.each{
writeFile(it).in(it.absoloutePath)
}
}
No, you should consider the class files as read only. They have been built / published prior to tweakClasses
. With my approach I was suggesting to create a new jar with a different artifact/group containing the tweaked classes which could be used downstream instead of the original jar(s).
If you want to prevent the original class files from being built into jars (and published), you will need to do this in ':my-lib'
and 'com.example:another-lib:xxx'
projects prior to building / publishing the jars