Filter a SourceSet based on file contents?

Hi,

I have a multi module project having two submodules :common and :common-gwt. With :common-gwt having a compile time dependency to project(":common").

Now when I build the jar for :common-gwt I want to include certain source files from :common. First I have done

jar {
  from project.sourceSets.main.allSource
  from project.sourceSets.main.output
  from project(':common').sourceSets.main.allJava
}

which works great, however I need to filter the files from project :common based on an annotation. So I have changed it to

jar {
  from project.sourceSets.main.allSource
  from project.sourceSets.main.output
  from project(':common').sourceSets.main.allJava.filter { File file ->
    file.text.contains "@GwtCompatible"
  }
}

Now only files matching the filter condition are copied into the jar however they are now copied to the root of the jar file and not to their original java package locations. I assume thats because SourceSet.filter() returns a FileCollection and then jar.from has less information to do the right thing? Without filtering files are at the correct location inside the jar file.

How can I fix that? Is there a different way to filter a source set based on file content?

Thanks in advance

Does anyone have an idea?

Ok I am using a different approach now

jar {
  from(project(':common').sourceSets.main.allJava) {
    exclude { details ->
      return !details.isDirectory() &&
          (!details.file.text.contains("@GwtCompatible")
              || details.file.text.contains("emulated = true"))
    }
  }
}