compileOnly seem to add transitive dependencies to compileClasspath?

I am really confused about compileOnly and the fact that it seems to load transitive dependencies to the compileClasspath. I thought that a compileOnly dependency doesn’t needs its transitive dependencies.

I really would like to know more about this.

My problem comes from a decision that I have no control over. Instead of lifting third party versions to to a java 17 compatible versions, the old jars was modified to handle java 17. When I now use the dependency on compileOnly i get transitive dependencies that are not compatible with java 17.

My knowledge about gradle isn’t good enough to understand what is going on.

I thought that a compileOnly dependency doesn’t needs its transitive dependencies.

No, compileOnly means that you have that dependency and its transitive api-dependencies at compile time of your code, otherwise you for example might miss classes in its API like superclasses or parameter types and your compilation might fail.

compileOnly just means, that you do not have the dependency available at runtime (unless added by something else of course).

If you don’t want the transitive dependencies, set that dependencies transitive flag to false, but that is seldomly what you want.

Instead of lifting third party versions to to a java 17 compatible versions, the old jars was modified to handle java 17.

Usually in Java-land things are almost always backwards compatible.
So usually if a library works for Java 11, it should work just the same for Java 17.
And having dependencies for older Java versions on a compile classpath should likewise not make any problems.

Thank you. That explains my confusion.

I really don’t want the transitive dependencies because they are actually really bad. I didn’t say that am running on an appserver that uses EE10 and is moving from javax to jakarta and can’t have dependencies that bring back javax.