使用多个库模块聚合Javadoc for Android项目

时间:2015-07-16 12:48:39

标签: android gradle javadoc

我已经浏览了几乎整个互联网,寻找一种方法,将由单独的库模块组成的项目中的Javadoc聚合到单个Javadoc中。

有一个插件显然允许这样做: https://github.com/nebula-plugins/gradle-aggregate-javadocs-plugin

但是,如果我运行插件指定的命令,Gradle会找到该任务并执行它,但不会生成输出目录。

任何帮助如何从多个模块构建单个Javadoc非常感激。

1 个答案:

答案 0 :(得分:6)

我设法让它在不久前工作,为迟到的回应道歉。可聚合Javadoc创建的解决方案如下:

  1. 在项目中维护的每个子项目中创建一个生成Javadoc的任务:

    android.libraryVariants.all { variant ->
    
    task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
        destinationDir = project.file("$project.projectDir/javadoc/$project.PROJECT_NAME") //Project name in the Project's gradle.properties
        title = "A title of my project - $project.PROJECT_VERSION_NAME" //Project version name in the Project's gradle.properties
        description "Generates Javadoc for $variant.name."
        source = variant.javaCompile.source
        ext.androidJar ="${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
        classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar) + project.files(android.getBootClasspath().join(File.pathSeparator))
        options {
            memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PUBLIC //change the modifier according to your needs
            links "http://docs.oracle.com/javase/8/docs/api/"
            linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
        }
        exclude '**/BuildConfig.java'
        exclude '**/R.java'
    }
    
      task("bundle${variant.name.capitalize()}Javadoc", type: Jar) {
        baseName = "Compass API - ($version)"
        description "Bundles Javadoc into zip for $variant.name."
        classifier = "javadoc"
        from tasks["generate${variant.name.capitalize()}Javadoc"]
      }
    }
    
  2. 上面的配置为子项目的每个buildVariant添加了一个Javadoc生成任务。此时,您可以通过键入

    分别为每个模块生成Javadoc
    gradle :myRootProject:mySubproject:generateDebugJavadoc
    gradle :myRootProject:mySubproject:generateReleaseJavadoc
    gradle :myRootProject:mySubproject:generateMyFancyFlavourDebugJavadoc
    gradle :myRootProject:mySubproject:generateMyFancyFlavourReleaseJavadoc
    

    如果使用JRE 8,则以下配置会在Javadoc构建期间禁用doclint引发的错误(explanation in greater detail here

       if (JavaVersion.current().isJava8Compatible()) {
         tasks.withType(Javadoc) {
             // disable the crazy super-strict doclint tool in Java 8
             //noinspection SpellCheckingInspection
             options.addStringOption('Xdoclint:none', '-quiet')
         }
        }
    
    1. 要将每个子模块的Javadoc聚合为一个子模块,请在build.gradle中创建一个插件,这将向子模块添加一个您感兴趣的部分Javadoc生成任务:

       class JavadocAggregationPlugin implements Plugin<Project> {
       static final String AGGREGATE_JAVADOCS_TASK_NAME = 'aggregateJavadocs'
      
       @Override
       void apply(Project project) {
           Project rootProject = project.rootProject
           rootProject.gradle.projectsEvaluated {
              Set<Project> librarySubprojects = getLibraryProjects(rootProject)
               if (!librarySubprojects.isEmpty()) {
                  rootProject.task(AGGREGATE_JAVADOCS_TASK_NAME, type: Javadoc) {
                       description = 'Aggregates Javadoc API documentation of all subprojects.'
                       group = JavaBasePlugin.DOCUMENTATION_GROUP
                       dependsOn librarySubprojects.generateReleaseJavadoc //please note that generateReleaseJavadoc is the name of the separate Javadoc generation task in each library module
      
                       source librarySubprojects.generateReleaseJavadoc.source
                       destinationDir rootProject.file("$rootProject.buildDir/docs/javadoc") //Javadoc destination directory
                       classpath = rootProject.files(librarySubprojects.generateReleaseJavadoc.classpath)
                   }
               }
           }
       }
      
      private Set<Project> getLibraryProjects(Project rootProject) {
          rootProject.subprojects.findAll { subproject -> subproject.plugins.findPlugin("com.android.library") } //In this case every library module is selected
      }
      }
      
    2. 最后,将插件包含在插件定义下方的项目build.gradle中的gradle配置中。

      apply plugin: JavadocAggregationPlugin
      
      1. 通过执行此操作并重建gradle的配置,您应该能够通过cli键入以下命令在指定目录中创建聚合Javad:

        gradle aggregateJavadocs
        
      2. 希望有所帮助。

        有用的链接:Android Gradle DSL