Gradle 不生成 Javadoc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21209253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradle does not generate Javadocs
提问by sparkonhdfs
I am writing a build file with Gradle to do Java build operations. However, Gradle does not generate Javadocs for my project. According to Gradle.org's documentation, to implement a Javadocs task in Gradle, the source and classpath have to be specified.
我正在用 Gradle 编写一个构建文件来执行 Java 构建操作。但是,Gradle 不会为我的项目生成 Javadoc。根据 Gradle.org 的文档,要在 Gradle 中实现 Javadocs 任务,必须指定源和类路径。
apply plugin: 'java'
javadoc {
source = sourceSets.main.allJava
classpath = configurations.compile
}
However, when I run the command gradle javadoc
, or gradle build
, the default folder for javadocs (build\docs) is never created, so no html files are generated for the project. What can I do to fix this?
但是,当我运行命令gradle javadoc
, or 时gradle build
, javadocs (build\docs) 的默认文件夹从未创建,因此不会为项目生成 html 文件。我能做些什么来解决这个问题?
回答by centerback
For various reasons we created a custom classpath in gradle for our java project. Mainly as we wanted to split out dependancies into those provided at runtime from those that weren't.
由于各种原因,我们在 gradle 中为我们的 java 项目创建了一个自定义类路径。主要是因为我们想将依赖项从那些在运行时提供的依赖项中分离出来。
So we set up build.gradle like this
所以我们这样设置 build.gradle
configurations {
providedCompile
}
dependencies {
providedCompile 'provided1', 'provided2
providedCompile 'provided3'
compile 'compile1'
compile ('compile2')
{
exclude group: 'unwanted part'
}
}
sourceSets.main.compileClasspath += configurations.providedCompile
javadoc.classpath += configurations.providedCompile
sourceSets.test.compileClasspath += configurations.providedCompile
sourceSets.test.runtimeClasspath += configurations.providedCompile
We needed to add the final part in order for gradle to correctly pick up the classpath for complying, testing AND running javadoc.
我们需要添加最后一部分,以便 gradle 正确选择类路径以符合、测试和运行 javadoc。
回答by tintin
Seems like your directory structure is is not a standard src/main/java
. If that is the case then you need to specify the include pattern as part of the includeclosure, something like this:
似乎您的目录结构不是标准的src/main/java
. 如果是这种情况,那么您需要将包含模式指定为包含闭包的一部分,如下所示:
javadoc {
source = sourceSets.main.allJava
classpath = configurations.compile
}
include **/your/directory/structure/*
回答by sver
You can write a gradle task of type Javadoc to create javadocs like this :
您可以编写一个 Javadoc 类型的 gradle 任务来创建这样的 javadoc:
task createJavadocs (type: Javadoc)
{
source = project.android.sourceSets.main.java.srcDirs
options.linkSource true
classpath += project.files(project.android.getBootClasspath().join(File.pathSeparator))
failOnError false
}
To create Javadocs, simply run this task.
要创建 Javadoc,只需运行此任务。
回答by Abhijit Maity
If includeclosure is not working the add sourceSetclosure with srcDirpointing to the module/java directory.
如果包含闭包不起作用,则添加sourceSet闭包,其中srcDir指向 module/java 目录。
The below example is properly working for me. Here java directory is src/main/java , where I have all my packages.
下面的例子适合我。这里的 java 目录是 src/main/java ,我有我所有的包。
sourceSets {
build {
java.srcDir file('src/integrationTest/java')
}
}
javadoc {
source = sourceSets.main.allJava
classpath = configurations.compile
}
The run $gradle javadoc
运行 $ gradle javadoc
回答by papigee
Just to add a little more structure to the answers, these are the configs needed to generate Javadoc (using Gradle version 4 and 5).
只是为了给答案添加更多结构,这些是生成 Javadoc 所需的配置(使用Gradle 版本 4 和 5)。
1. Define your sourceSet(might be optional if the standard Maven directory structure is used). Assuming this directory structure:
1. 定义您的 sourceSet(如果使用标准 Maven 目录结构,则可能是可选的)。假设这个目录结构:
├── src
│?? └── main
│?? ├── java
├── src
│?? └── test
│?? ├── java
Define sourceSet like this
像这样定义 sourceSet
sourceSets {
main {
java {
srcDirs =['src/main/java']
}
test {
java {
srcDirs =['src/test/java']
}
}
2. Configure classpathto be used when generating javadoc. If this is not done, it will result in errors similar to this: error: package org.xxxx
does not exist.
2、配置生成javadoc时使用的classpath。如果不这样做,会导致类似这样的错误:error: package org.xxxx
不存在。
Assuming your dependency is defined like this:
假设您的依赖项定义如下:
dependency {
compile group: 'xxxx', name: 'yyyy', version: 'zzzz'
testCompile group: 'aaaa', name: 'bbbb', version: 'cccc'
}
define javadoc task this way
以这种方式定义 javadoc 任务
tasks.withType(Javadoc){
source = sourceSet.main.java.srcDirs
classpath +=configuration.compile
destinationDir = reporting.file("myJavaDoc") //optional
}
If you want to generate Java doc for test, the task will look like this:
如果要生成用于测试的 Java 文档,任务将如下所示:
tasks.withType(Javadoc){
source = sourceSet.test.java.srcDirs
classpath +=configuration.compile
classpath +=configuration.testCompile
destinationDir = reporting.file("myJavaDoc") //optional
}