How to exclude module-info.java in Gradle 7?

I created a project like this:

plugins {
    id("java")
}

group = "org.glavo"
version = "1.0-SNAPSHOT"

tasks.compileJava {
    exclude("module-info.java")

    logger.quiet("Sources: " + source.asPath)
}

In Gradle 6, exclude("module-info.java") can avoid compiling module-info.java, but this is not working in Gradle 7.

By printing the log, I can see that module-info.java does not exist in the source path of compileJava task. However, it still always compiles in Gradle 7 (I tried 7.0~7.4.2 with the same result).

Am I doing something wrong? Why is there such a difference between Gradle 6 and Gradle 7?

After investigation, I found out that this is the result brought by modularity.inferModulePath.

It defaults to true and works in a very unusual way, even excluding module-info.java from the SourceSet, it still finds and forces module-info.java to be compiled.

Is this expected behavior?