From cd296a6331c8ee1e4f4d93edee29387e6dd17c99 Mon Sep 17 00:00:00 2001 From: Bastien Paul Date: Wed, 16 May 2018 10:57:16 +0200 Subject: [PATCH] Remove need of project.afterEvaluate() and add generateJavadoc macro task --- .gitignore | 3 +- README.md | 12 +++ build.gradle | 30 +++---- .../android/javadoc/Generation.groovy | 75 ++++++++++++++---- .../extensions/AndroidJavadocExtension.groovy | 2 +- .../android/javadoc/GenerationTest.groovy | 78 +++++++++++++++---- 6 files changed, 147 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index 74215d4..293c9fa 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ gradle-app.setting local.properties .idea/ -*.iml \ No newline at end of file +*.iml +.gradletasknamecache \ No newline at end of file diff --git a/README.md b/README.md index 08367a1..5163ba6 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,12 @@ apply plugin: "com.vanniktech.android.javadoc" ./gradlew generateReleaseJavadoc ``` +or to run javadoc task for all variants : + +```gradle +./gradlew generateJavadoc +``` + **HTML reports** ``` @@ -59,6 +65,12 @@ apply plugin: "com.vanniktech.android.javadoc" ./gradlew generateReleaseJavadocJar ``` +or to run javadoc archive task for all variants : + +```gradle +./gradlew generateJavadocJar +``` + ## Customize Plugin ```groovy diff --git a/build.gradle b/build.gradle index aea8f54..431006d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,22 +1,12 @@ -buildscript { - repositories { - jcenter() - maven { url "https://plugins.gradle.org/m2/" } - } - - dependencies { - classpath 'com.gradle.publish:plugin-publish-plugin:0.9.9' - classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0' - classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.11.0' - } +plugins { + id "groovy" + id "java" + id 'maven-publish' + id 'com.gradle.plugin-publish' version '0.9.9' + id "com.vanniktech.android.junit.jacoco" version "0.11.0" + id "com.github.ben-manes.versions" version "0.17.0" } -apply plugin: 'groovy' -apply plugin: 'java' -apply plugin: 'com.github.ben-manes.versions' -apply plugin: 'com.gradle.plugin-publish' -apply plugin: 'com.vanniktech.android.junit.jacoco' - repositories { jcenter() google() @@ -25,8 +15,10 @@ repositories { dependencies { compile gradleApi() compile localGroovy() - compile 'com.android.tools.build:gradle:3.0.1' + compileOnly 'com.android.tools.build:gradle:3.1.2' + + testCompile 'com.android.tools.build:gradle:3.1.2' testCompile 'junit:junit:4.12' } @@ -86,4 +78,4 @@ test { } } -apply from: file('gradle/gradle-mvn-push.gradle') \ No newline at end of file +apply from: file('gradle/gradle-mvn-push.gradle') diff --git a/src/main/groovy/com/vanniktech/android/javadoc/Generation.groovy b/src/main/groovy/com/vanniktech/android/javadoc/Generation.groovy index 9069822..3cb81c7 100644 --- a/src/main/groovy/com/vanniktech/android/javadoc/Generation.groovy +++ b/src/main/groovy/com/vanniktech/android/javadoc/Generation.groovy @@ -1,5 +1,6 @@ package com.vanniktech.android.javadoc +import com.android.build.gradle.BasePlugin import com.android.build.gradle.api.BaseVariant import com.vanniktech.android.javadoc.extensions.AndroidJavadocExtension import org.gradle.api.DomainObjectCollection @@ -14,7 +15,11 @@ import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.external.javadoc.JavadocMemberLevel class Generation implements Plugin { - Logger logger; + + private static final String JAVADOC_TASK = "generateJavadoc" + private static final String JAVADOC_JAR_TASK = "generateJavadocJar" + + Logger logger @Override void apply(final Project project) { @@ -25,13 +30,25 @@ class Generation implements Plugin { project.extensions.create("androidJavadoc", AndroidJavadocExtension) - project.allprojects { - afterEvaluate { Project p -> - // Do not throw exception anymore. We want to support big projects with a lot of modules that not all of them are android. - if (p.hasProperty('android')) { - applyPluginToProject(p); - } else { - logger.info "${p.name} is not an android project - plugin is not applied" + def hasSubProjects = project.subprojects.size() > 0 + + if (hasSubProjects) { + project.subprojects { subProject -> + applyPlugin(subProject) + } + } else { + applyPlugin(project) + } + } + + private void applyPlugin(Project project) { + if (project.hasProperty('android')) { + applyPluginToProject(project) + } else { + // Waiting for android plugin to be applied + project.plugins.whenPluginAdded { Plugin plugin -> + if (plugin instanceof BasePlugin) { + applyPluginToProject(project) } } } @@ -39,8 +56,10 @@ class Generation implements Plugin { private void applyPluginToProject(Project project) { if (project.android.hasProperty('applicationVariants')) { + createRootTask(project) addJavaTaskToProjectWith(project, (DomainObjectCollection) project.android.applicationVariants) } else if (project.android.hasProperty('libraryVariants')) { + createRootTask(project) addJavaTaskToProjectWith(project, (DomainObjectCollection) project.android.libraryVariants) } else { // Do not throw exception anymore to support big projects with a lot of modules that not all of them are android. @@ -48,6 +67,25 @@ class Generation implements Plugin { } } + private void createRootTask(Project project) { + if (project.tasks.findByPath(JAVADOC_TASK)) { + logger.debug "task $JAVADOC_TASK already exists" + } else { + project.task(JAVADOC_TASK) { + description = "Generates javadoc for ${project.name}" + group = 'Documentation' + } + } + if (project.tasks.findByPath(JAVADOC_JAR_TASK)) { + logger.debug "task $JAVADOC_JAR_TASK already exists" + } else { + project.task(JAVADOC_JAR_TASK, dependsOn: [JAVADOC_TASK]) { + description = "Generates javadoc archive for ${project.name}" + group = 'Documentation' + } + } + } + private Task addJavaTaskToProjectWith(final Project project, final DomainObjectCollection variants) { variants.all { variant -> // Apply a filter because javadoc could be configured for only some particular variants. @@ -74,20 +112,25 @@ class Generation implements Plugin { Task javadocTask = createJavadocTask(project, variant, taskName) Task javadocArchiveTask = createJavadocArchiveTask(project, variant, genJavadocJarTaskName(project, variant)) javadocArchiveTask.dependsOn(javadocTask) + project.tasks.getByName(JAVADOC_TASK).dependsOn(javadocTask) + project.tasks.getByName(JAVADOC_JAR_TASK).dependsOn(javadocArchiveTask) } } private Task createJavadocTask(final Project project, variant, String taskName) { project.task(taskName, type: Javadoc) { title = "Documentation for ${project.name} at version ${project.android.defaultConfig.versionName}" - description = "Generates Javadoc for $variant.name." + description = "Generates javadoc for $variant.name variant." group = 'Documentation' destinationDir = getJavadocFolder(project, variant) source = variant.javaCompiler.source - ext.androidJar = "${project.android.sdkDirectory}/platforms/${project.android.compileSdkVersion}/android.jar" - classpath = project.files(variant.javaCompiler.classpath.files) + project.files(ext.androidJar) + // Fix issue : Error: Can not create variant 'android-lint' after configuration ': library: debugRuntimeElements' has been resolved + doFirst { + classpath = project.files(variant.javaCompile.classpath.files, + project.android.getBootClasspath()) + } if (JavaVersion.current().isJava8Compatible()) { options.addStringOption('Xdoclint:none', '-quiet') @@ -102,7 +145,7 @@ class Generation implements Plugin { private Task createJavadocArchiveTask(Project project, variant, String taskName) { project.task("${taskName}", type: Jar) { - description = "Compress Javadoc for $variant.name." + description = "Compress javadoc for $variant.name variant." group = "Documentation" classifier = 'javadoc' from getJavadocFolder(project, variant) @@ -128,19 +171,19 @@ class Generation implements Plugin { return "${project.androidJavadoc.taskNameTransformer(variant).capitalize()}" } - private static String genJavadocTaskName(final Project project, variant) { + static String genJavadocTaskName(final Project project, variant) { return "generate${getBaseTaskName(project, variant)}Javadoc" } - private static String genDeleteTaskName(final Project project, variant) { + static String genDeleteTaskName(final Project project, variant) { return "delete${getBaseTaskName(project, variant)}Javadoc" } - private static String genJavadocJarTaskName(final Project project, variant) { + static String genJavadocJarTaskName(final Project project, variant) { return "${genJavadocTaskName(project, variant)}Jar" } private static File getJavadocFolder(final Project project, variant) { - return new File(project.androidJavadoc.outputDir(project), getBaseTaskName(project, variant)) + return new File("${project.androidJavadoc.outputDir(project)}", "${project.androidJavadoc.taskNameTransformer(variant)}") } } diff --git a/src/main/groovy/com/vanniktech/android/javadoc/extensions/AndroidJavadocExtension.groovy b/src/main/groovy/com/vanniktech/android/javadoc/extensions/AndroidJavadocExtension.groovy index f482c56..c6aa9d1 100644 --- a/src/main/groovy/com/vanniktech/android/javadoc/extensions/AndroidJavadocExtension.groovy +++ b/src/main/groovy/com/vanniktech/android/javadoc/extensions/AndroidJavadocExtension.groovy @@ -2,7 +2,7 @@ package com.vanniktech.android.javadoc.extensions import org.gradle.api.Project -public class AndroidJavadocExtension { +class AndroidJavadocExtension { /** * Closure used for filter some variant out */ diff --git a/src/test/groovy/com/vanniktech/android/javadoc/GenerationTest.groovy b/src/test/groovy/com/vanniktech/android/javadoc/GenerationTest.groovy index 4e01fa8..6680c8b 100644 --- a/src/test/groovy/com/vanniktech/android/javadoc/GenerationTest.groovy +++ b/src/test/groovy/com/vanniktech/android/javadoc/GenerationTest.groovy @@ -1,6 +1,7 @@ package com.vanniktech.android.javadoc import com.vanniktech.android.javadoc.extensions.AndroidJavadocExtension +import org.gradle.api.Task import org.gradle.api.tasks.bundling.Jar import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.testfixtures.ProjectBuilder @@ -20,14 +21,14 @@ class GenerationTest { def project @Before - public void setUp() { + void setUp() { generation = new Generation() project = ProjectBuilder.builder().withName('project').build() copyManifest() } @Test - public void testNullProject() throws Exception { + void testNullProject() throws Exception { expectedException.expect(UnsupportedOperationException.class) expectedException.expectMessage('Project is null') @@ -35,7 +36,7 @@ class GenerationTest { } @Test - public void testThatExtensionIsAdded() { + void testThatExtensionIsAdded() { project.plugins.apply('com.android.application') generation.apply(project) @@ -46,17 +47,19 @@ class GenerationTest { } @Test - public void testNotAndroidProject() { + void testNotAndroidProject() { generation.apply(project) assert !project.hasProperty("generateReleaseJavadoc") assert !project.hasProperty("generateDebugJavadoc") assert !project.hasProperty("generateReleaseJavadocJar") assert !project.hasProperty("generateDebugJavadocJar") + assert !project.hasProperty("generateJavadoc") + assert !project.hasProperty("generateJavadocJar") } @Test - public void testJavaProject() { + void testJavaProject() { project.plugins.apply('java') generation.apply(project) @@ -64,10 +67,12 @@ class GenerationTest { assert !project.hasProperty("generateDebugJavadoc") assert !project.hasProperty("generateReleaseJavadocJar") assert !project.hasProperty("generateDebugJavadocJar") + assert !project.hasProperty("generateJavadoc") + assert !project.hasProperty("generateJavadocJar") } @Test - public void testAndroidAppProject() { + void testAndroidAppProject() { doNotRunOnTravis() withAndroidAppProject() @@ -83,11 +88,36 @@ class GenerationTest { assert project.generateDebugJavadoc instanceof Javadoc assert project.generateReleaseJavadocJar instanceof Jar assert project.generateDebugJavadocJar instanceof Jar + assert project.generateJavadoc instanceof Task + assert project.generateJavadocJar instanceof Task } + @Test + void testAndroidAppProjectInverseApply() { + doNotRunOnTravis() + + // Apply javadoc plugin first + generation.apply(project) + applyAndroidPlugin('com.android.application') + + // These tasks are only added after project.afterEvaluated() is called. + assert !project.hasProperty("generateReleaseJavadoc") + assert !project.hasProperty("generateDebugJavadoc") + assert !project.hasProperty("generateReleaseJavadocJar") + assert !project.hasProperty("generateDebugJavadocJar") + + project.evaluate() + + assert project.generateReleaseJavadoc instanceof Javadoc + assert project.generateDebugJavadoc instanceof Javadoc + assert project.generateReleaseJavadocJar instanceof Jar + assert project.generateDebugJavadocJar instanceof Jar + assert project.generateJavadoc instanceof Task + assert project.generateJavadocJar instanceof Task + } @Test - public void testAndroidLibraryProject() { + void testAndroidLibraryProject() { doNotRunOnTravis() withAndroidLibProject() @@ -103,10 +133,12 @@ class GenerationTest { assert project.generateDebugJavadoc instanceof Javadoc assert project.generateReleaseJavadocJar instanceof Jar assert project.generateDebugJavadocJar instanceof Jar + assert project.generateJavadoc instanceof Task + assert project.generateJavadocJar instanceof Task } @Test - public void filterVariant() { + void filterVariant() { doNotRunOnTravis() withAndroidAppProject() @@ -120,10 +152,12 @@ class GenerationTest { assert !project.hasProperty("generateDebugJavadocJar") assert project.generateReleaseJavadoc instanceof Javadoc assert project.generateReleaseJavadocJar instanceof Jar + assert project.generateJavadoc instanceof Task + assert project.generateJavadocJar instanceof Task } @Test - public void transformTaskName() { + void transformTaskName() { doNotRunOnTravis() withAndroidAppProject() @@ -139,20 +173,32 @@ class GenerationTest { assert !project.hasProperty("generateDebugJavadocJar") assert project.generateYeahJavadoc instanceof Javadoc assert project.generateYeahJavadocJar instanceof Jar + assert project.generateJavadoc instanceof Task + assert project.generateJavadocJar instanceof Task } private void withAndroidAppProject() { - project.plugins.apply('com.android.application') + applyAndroidPlugin('com.android.application') generation.apply(project) - project.android.compileSdkVersion 25 - project.android.buildToolsVersion "25.0.2" } private void withAndroidLibProject() { - project.plugins.apply('com.android.library') + applyAndroidPlugin('com.android.library') generation.apply(project) - project.android.compileSdkVersion 25 - project.android.buildToolsVersion "25.0.2" + } + + private void applyAndroidPlugin(String plugin) { + project.plugins.apply(plugin) + project.android.compileSdkVersion 27 + project.android.buildToolsVersion "27.0.3" + project.android.defaultConfig { + minSdkVersion 17 + targetSdkVersion 27 + versionCode 1 + versionName "dev" + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } } private void copyManifest() { @@ -166,7 +212,7 @@ class GenerationTest { private static void doNotRunOnTravis() { // Unless we know how to have a good ANDROID_HOME env var on travis. String res = System.getenv("TRAVIS") if (res != null) { - Assume.assumeFalse(res == "true"); + Assume.assumeFalse(res == "true") } } }