Jeriho
Jeriho

Reputation: 7299

Exclude all compiled classes from jar built by gradle java project

I have a gradle java project with custom task which generates some files during build. I need to produce jar artifact containing ONLY generated files. Problem: jar contains both generated files and class files.

Exclusion of *.java files from source sets is impossible because I need compiled classes for generation.

jar {
  exclude("**/*.class")
  from ("$buildDir/generated-files-dir")
}

Snippet above removes class files but leaves directories as is.

Upvotes: 0

Views: 686

Answers (1)

miskender
miskender

Reputation: 7968

Add includeEmptyDirs = false to your jar task.

jar {
  exclude("**/*.class")
  includeEmptyDirs = false
  from ("$buildDir/generated-files-dir")
}

https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:includeEmptyDirs

Upvotes: 1

Related Questions