I'm migrating to Dokka v2, and after reading the m...
# dokka
e
I'm migrating to Dokka v2, and after reading the migration steps there is still one point that's unclear. I currently have
Copy code
dokkaHtmlPartial {
  dependsOn(generateKotlinGrammarSource) // ANTLR task
}
in my sub-module build script. What's the correct task to use in place of
dokkaHtmlPartial
for this specific case?
a
Could you share more about the generateKotlinGrammarSource task? I guess it generates Kotlin source code? Are there other tasks that have
dependsOn(generateKotlinGrammarSource)
? I suspect the buildscript will have something like
tasks.kotlinCompile { dependsOn(generateKotlinGrammarSource) }
...
e
I guess it generates Kotlin source code?
Correct! The entire snippet is
Copy code
withType<KotlinCompilationTask<*>>().configureEach {
  dependsOn(generateKotlinGrammarSource)
}

dokkaHtmlPartial {
  dependsOn(generateKotlinGrammarSource)
}

//
// The source JAR tasks must explicitly depend on the grammar generation
// to avoid Gradle complaining and erroring out
//

sourcesJar {
  dependsOn(generateKotlinGrammarSource)
}

kotlin.targets.configureEach {
  if (publishable) {
    named<Jar>("${targetName}SourcesJar") {
      dependsOn(generateKotlinGrammarSource)
    }
  }
}
a
Hmmm okay, that makes sense. Let's make a quick fix first, but I think it'd be better to try to remove the manual
dependsOn()
so Gradle can infer the task dependencies properly. Replace
Copy code
dokkaHtmlPartial {
  dependsOn(generateKotlinGrammarSource) // ANTLR task
}
with
Copy code
tasks.withType(org.jetbrains.dokka.gradle.tasks.DokkaGenerateTask.java).configureEach {
   dependsOn(generateKotlinGrammarSource)
}
(my Groovy is very underdeveloped so you might need to fix something)
Alternatively, you could remove all of the manual `dependsOn()`s by passing the generateKotlinGrammarSource as a srcDir. Assuming the generateKotlinGrammarSource task has an
@OutputDirectory
, then it should be as simple as this:
Copy code
kotlin {
    sourceSets {
        main {
            kotlin.srcDir(generateKotlinGrammarSource)
        }
    }
}
Gradle will then recognise it needs to run the task to generate the source code, and all the task dependencies will be inferred automatically.
e
Oh it is registered as source dir already.
Copy code
kotlin {
  sourceSets {
    commonMain {
      kotlin {
        srcDir(layout.buildDirectory.dir("generatedAntlr"))
      }
      ...
But Gradle didn't like it anyway at the time I set it up (1.9.24). Not sure if something has changed in the meantime.
a
ah, but registering the dir without the task means Gradle doesn't know it needs to run the task. Try replacing it with this:
Copy code
kotlin {
  sourceSets {
    commonMain {
      kotlin {
        srcDir(generateKotlinGrammarSource)
      }
      ...
e
Ahhhh! Sorry, I really didn't notice the difference in your snippet lol, thought it was the same. Basically, if I pass the task directly I can remove the
dependsOn
from
KotlinCompilationTask
and
DokkaGenerateTask
?
a
exactly!
e
Is it because the task defines an
@OutputDirectory
that Gradle infers it automatically?
a
yes
e
Nice. Let me try it out.
a
e
Yup, it works as expected now! I wonder if the same simplification also applies to
sourcesJar
and
kotlin.targets
.
Looks like it does.
Thanks for the suggestion! Much better now.
a
nice! Happy to hear it