Gradle multi-module project: Exclude one or more modules for javadoc

Hello Gradle community.

I’m working in a multi-module project and currently we are able to generate all javadoc from all modules with a Javadoc taks.

task alljavadoc(type: Javadoc) {
...
}

Now I’m trying to exclude one module, let’s say Module2, but I haven’t been able to achieve that. There’s a tricky part, Module1 and Module2 both have classes with the same package and name, so if I use “exclude” property, it will get rid of classes from Module1 and Module2.
For example.
Module1:

  • com.example.ClassA
  • com.example.ClassB

Module2:

  • com.example.ClassB
  • com.example.ClassC

By using exclude 'com.example.ClassB' will remove ClassB in both modules. I want to remove all classes from Module2.

I’ve tried something like this

source subprojects.collect { project ->
    if (project.name != 'Module2') {
      project.sourceSets.main.allJava
    }
  }

But I get an error about returning Null or empty when the project is Module2.

What is the best approach to exclude a module from Javadoc?

Thanks!

Could someone please point me to the right direction?