如何使用Gradle生成集成PlantUML时序图的Javadoc并解决首次运行失效问题
根因说明
你当前的问题是Gradle任务的输入输出没有正确绑定导致的:仅配置了javadoc依赖renderPlantUml任务,但没有将PlantUML渲染的输出目录声明为javadoc任务的输入资源,首次执行时javadoc会提前扫描资源,此时PlantUML还未生成时序图,因此找不到图片。第二次执行时上一次生成的图片已经存在于目录中,所以能正常展示。
修复配置
修改build.gradle中对应任务的配置即可,修改后的内容如下:
apply plugin: "java" apply plugin: "application" mainClassName = 'com.twu.calculator.CalculatorApp' group = 'calculator' version = '1.0-SNAPSHOT' description = "Calculator Console App" repositories { mavenCentral() } dependencies { testImplementation 'junit:junit:4.12' testImplementation 'com.github.stefanbirkner:system-rules:1.16.1' } task renderPlantUml(type: RenderPlantUmlTask) { // 显式指定PlantUML渲染输出目录,可根据实际需求调整路径 outputDir = file("${buildDir}/generated-resources/plantuml") // 声明任务输出,让Gradle识别该目录为任务产出 outputs.dir outputDir } javadoc { source = sourceSets.main.allJava options.overview = "src/main/javadoc/overview.html" // relative to source root options.addStringOption("sourcepath","${projectDir}/src/main/javadoc") // 新增:将PlantUML输出目录加入javadoc的资源路径,让overview.html可以找到图片 options.addStringOption("classpath", renderPlantUml.outputDir.path) // 新增:显式声明javadoc的输入包含PlantUML的输出,确保javadoc执行前图片已经生成完成 inputs.dir renderPlantUml.outputDir } javadoc.dependsOn renderPlantUml // To execute the app task runApp (type: JavaExec, dependsOn: classes){ /* Can pass all the properties: */ systemProperties System.getProperties() standardInput = System.in description = "Running the Calculator" main = "com.twu.calculator.CalculatorApp" classpath = sourceSets.main.runtimeClasspath }
验证
修改完成后,直接执行gradle javadoc单次命令即可生成带PlantUML时序图的Javadoc,不需要分开执行两个命令。
内容的提问来源于stack exchange,提问作者João Marques
相关产品推荐
相关产品推荐

