Is there a way to modify the entire content of the file while assembling the Jar using Gradle.?

212 views Asked by At

There's an usecase in my project that, I have to modify the class file content while preparing the jar & use the custom classloader for loading that class.

To achieve the above scenario, I have used the following snippet of CopySpec in my build.gradle. Surprisingly It's working with small class files & not incase of large files. Is there something that I'm missing?

def contentSpec = copySpec {
    from {
        configurations.runtime.collect {it.isDirectory() ? it : zipTree(it)}
    }
    eachFile (new Action<FileCopyDetails>() {
        @Override
        void execute(FileCopyDetails fileCopyDetails) {
            if (fileCopyDetails.getPath().contains('abc')) {
                println fileCopyDetails.getPath()
                byte[] content = fileCopyDetails.file.bytes;
                byte[] newContent = encryptClass.encrypt(content)
                println ("${content.length} -> ${newContent.length}")
                fileCopyDetails.file.bytes = newContent
                println fileCopyDetails.file.bytes.length
                fileCopyDetails.setName(fileCopyDetails.getName().replace(".class",".enc"))
            }
        }
    })
    with jar
}

For example I have file of name AbcConfig.class & the println statements prints like below : It shows 33232 bytes written (after modification) com/abc/xyz/config/AbcConfig.class 33216 -> 33232 33232

But when i extract jar and do ls -lrt for checking out number of bytes of that particular on disk, It shows (33202 in the output)

-rwxrwxrwx 1 root root 33202 Aug 3 13:02 AbcConfig.enc

0

There are 0 answers