How to customize the predefined jar task

I have a sub-project lombok-anno to extends lombok lib, with a user-gen dir $buildDir/lombok, which is used for compile lombok-anno, and added to mainsourceset.output, but not needed (neather run or compile) for another sub-project main-proj who uses lombok-anno.

the default behavior of JavaPlugin’s jar task will add all main.output to xxx.jar ( by call from(output) ).

so i need to customize the jar task, to remove $buildDir/lombok in Jar.source

  1. because the exclude uses path in des_path in jar, not source path in from, so it failes. The file in jar is mixed, hard to exclude.
     exclude "$buildDir/lombok"
  1. not work, the source added by JavaPlugin’s jar not removed.
tasks.replace("jar", Jar).configure{
      println source.getFiles()

How to customize the jar task?

Are you wanting to exclude resources or class files?

If resources you’ll want to configure the “processResources” task which is of type “Copy”. Eg

apply plugin: 'java'
processResources {
   exclude 'lombok/**' 

If classes I think you’d actually want to delete the class files in a doLast {...} closure in the “compileJava” task. Otherwise your runtimeClasspath will be inconsistent with your jar


apply plugin: 'java'
compileJava {
   doLast {
      delete "$destinationDir/lombok"