Gradle: How compile/build a specific class and package?

For a big project with multimodules based on Gradle, lets say Spring Security.

I am able to build a specific module (and skipping some tasks to gain time) through:

./gradlew :spring-security-config:build
./gradlew :spring-security-config:build -x test
./gradlew :spring-security-config:build -x javadoc -x test

Assuming the following scenario:

module-x
   src/main/java
      com.something.abc
         A1.java
         A2.java
         ...
         AN.java
      com.something.xyz
         B1.java
         B2.java
         ...
         BN.java
 

Assuming that only one java file was edited. Lets say A7.java. How I can compile/build only for that file?, because with:

./gradlew :module-x:build -x test

“Seems” that all the module is compiled again, it because it always takes the same time, I thought that Gradle has an internal cache to know what was changed and what classes weren’t changed, therefore it should be faster. Even if that module has for example 100 classes and only 4 were changed (same or different packages) I though the build should be faster than the first time where there are no compiled classes, or clean task was executed.

Now, about testing I can execute a specific Test through:

./gradlew :spring-security-xxx:test --tests org.springframework.security.something.XTests

From above, I know is possible apply for a specific package too.

Therefore if is possible, how build/compile a:

  • specific class
  • specific package

You cannot compile only a specific class or package, but the incremental Java compilation should automatically only compile what is necessary. It is a bit more complex than just recompiling changed files, as the Java compiler for example inlines constant values into the classes, so more might be needed to be compiled than you think.

1 Like

Iirc running with -i or -d should give you more insight, for example when incremental compilation was not possible.

1 Like

Thanks for the reply.

So seems I must trust in what Gradle does do behind the scenes.

Iirc running with -i or -d should give you more insight

Could you share a simple example of how use that parameters and where I can find documentation of them? Perhaps would be available other parameters too.

Thank you

./gradlew -i :module-x:compileJava
./gradlew -d :module-x:compileJava

./gradlew --help
and / or
https://docs.gradle.org/current/userguide/userguide_single.html#sec:command_line_logging

Thanks again for the quick and polite support.