Skip to content

Commit

Permalink
Add apt output dir as source directly in sourcesJar task
Browse files Browse the repository at this point in the history
Code generation and subsequent compilation is performed within a single
java compiler call. Gradle, on the other hand, checks whether
the inputs of a task have changed before starting the task.
So if we call compileJava for the first time after cleaning build dir
before starting the task, Gradle sees that generatedSrcDir is empty
and remembers that. When we call compileJava again, Gradle sees that
new files have been added to generatedSrcDir and restarts compilation.
On the third compileJava call in a row, nothing has changed for Gradle
and it marks the task as up to date.
As far as I understand, adding generatedSrcDir as sourceSet is only
needed to include generated sources in sourcesJar. So instead of adding
generatedSrcDir as sourceSet, we can add it directly to sourcesJar.

Signed-off-by: Denis Buzmakov <me@bacecek.dev>
  • Loading branch information
bacecek authored and utzcoz committed Feb 24, 2024
1 parent 1d8e66c commit c53f25c
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions buildSrc/src/main/groovy/ShadowsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class ShadowsPlugin implements Plugin<Project> {
// write generated Java into its own dir... see https://github.com/gradle/gradle/issues/4956
def generatedSrcDir = project.file("build/generated/src/apt/main")

project.sourceSets.main.java { srcDir generatedSrcDir }

project.tasks.named("compileJava").configure { task ->
task.options.annotationProcessorGeneratedSourcesDirectory = generatedSrcDir

Expand All @@ -47,6 +45,7 @@ class ShadowsPlugin implements Plugin<Project> {
}

project.tasks.named("sourcesJar").configure { task ->
task.from(generatedSrcDir)
task.doLast {
def shadowPackageNameDir = project.shadows.packageName.replaceAll(/\./, '/')
checkForFile(task.archivePath, "${shadowPackageNameDir}/Shadows.java")
Expand Down

0 comments on commit c53f25c

Please sign in to comment.