0

I've seen these:

Gradle - Exclude file from being packaged with jar

Exclude one class file in gradle Android Studio

and am wondering what path or name I should use with exclude. This is in a gradle.kts file:

group = "kaldrn.core"

jar {
    exclude("kaldrn/CliKt.class")
}

That's my best guess, since it's the filepath in the jar. I've also tried using the source file, which doesn't work. I've tried using just basenames without path, no luck. The significance of the group declaration in this context, if any, is lost on me (I was never a big java user pre kotlin).

The class is defined in cli.kt, in package kaldrn.

How do I determine what to use here?


Perhaps ironically the path above turns out to be the correct one -- see my comments on the accepted answer about a very nasty caveat that affects gradle and jar { exclude ...}, at least for version 6.7.

CodeClown42
  • 11,194
  • 1
  • 32
  • 67

1 Answers1

1

The exclude path should contain the full path to the target file from the source route (eg, from main).

In your case:

jar {
    exclude("kaldrn/cli/CliKt.class")
}

If you are unsure in the path, look for your file under build/classes/kotlin/main

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • This works, but I see now why I didn't hit upon it before: Once that has worked, if I change the `exclude` *it's ignored in favour of the previous version*, even using `./gradlew clean; ./gradlew --no-build-cache jar`. I had to delete most of the contents of `~/.gradle/cache` (as in, the user's home cache), something I've never done before -- although perhaps I should do, because I am sure now what happened earlier was it was just employing the first path I tried (**the correct one turns out to be the one in the question here**, ie., my "best guess"). – CodeClown42 Jun 13 '21 at 15:02
  • ....I can reproduce this very consistently so probably grounds for a bug report. That can't be the right behaviour? Have you noticed anything like this or is there an obvious point I've missed? I'm sort of horrified to consider what else this applies to and all the previous times I've been unable to get something simple that should work to work and it just doesn't. ARGH! It is such a hot mess of a tool. :/ – CodeClown42 Jun 13 '21 at 15:02
  • Do you have this code in `buildSrc/build.gradle`, by chance? – Alexey Soshin Jun 13 '21 at 15:09
  • Yes, and deleting all that stuff (everything but the `6.7` directory) also works (instead of the stuff in the home cache). Dunno if this is because I am using the intelliJ interface and the `.gradlew` it created interchangeably. – CodeClown42 Jun 13 '21 at 15:20
  • `buildSrc` is for plugins, so Gradle won't rebuild it unless specifically told so: https://docs.gradle.org/current/userguide/custom_plugins.html – Alexey Soshin Jun 13 '21 at 16:46
  • Sorry, I misunderstood, and was a bit flustered. I have no literal `buildSrc` directory. I thought by that you meant "the top level of the project" -- plus a bit of unreading `build`, I was referring to the top `.gradle` directory. – CodeClown42 Jun 13 '21 at 16:56