There is currently an issue open to share Gradle cache between containers:
I think the best solution at the moment is to bake as many dependencies in to the Docker image as possible, and then use a volume to share between sequential builds, limiting it to one concurrent build per host. You can mount the cache on ...
If you want just to deploy faster to server(propably not only Glassfish) you can just write code in gradle to do that.
Example taken from here
* or in Netbeans, right click project, Properties, Manage Build in Tasks, Run
* Add line to Arguments: -Dorg.gradle.project....
Installing it as a tool allows Jenkins to manage the binary and ensure it's available for your build while also providing supporting pipeline syntax withEnv(). Having an agent with the binary allows you to shell out and run your commands using sh without withEnv(). It also allows you to decide where these binaries come from depending on your agent type (...
Build tools like maven or gradle could benefit you greatly here. It would do exactly what you have in mind as long as you have child project definition (with a build.gradle in gradle or pom.xml in maven).
I would not recommend the script as it would mean you have to maintain and tweak it in the future. Instead, investing in build tools can give you great ...