Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Remove need of project.afterEvaluate() and add generateJavadoc macro …
Browse files Browse the repository at this point in the history
…task
  • Loading branch information
bastienpaulfr committed May 14, 2018
1 parent a7693ff commit 829d83a
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ gradle-app.setting
local.properties

.idea/
*.iml
*.iml
.gradletasknamecache
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

```
Expand All @@ -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
Expand Down
30 changes: 11 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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'
}

Expand Down Expand Up @@ -86,4 +78,4 @@ test {
}
}

apply from: file('gradle/gradle-mvn-push.gradle')
apply from: file('gradle/gradle-mvn-push.gradle')
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.vanniktech
VERSION_NAME=0.3.0-SNAPSHOT
VERSION_NAME=0.3.1-SNAPSHOT

POM_ARTIFACT_ID=gradle-android-javadoc-plugin
POM_NAME=Gradle Android Javadoc Plugin
Expand Down
77 changes: 61 additions & 16 deletions src/main/groovy/com/vanniktech/android/javadoc/Generation.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.external.javadoc.JavadocMemberLevel

class Generation implements Plugin<Project> {
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) {
Expand All @@ -25,29 +29,61 @@ class Generation implements Plugin<Project> {

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"
if (isRoot(project)) {
project.allprojects { Project p ->
applyPlugin(p)
}
} 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 (project.plugins.hasPlugin("com.android.library")
|| project.plugins.hasPlugin("com.android.application")) {
applyPluginToProject(project)
}
}
}
}

private void applyPluginToProject(Project project) {
if (project.android.hasProperty('applicationVariants')) {
createRootTask(project)
addJavaTaskToProjectWith(project, (DomainObjectCollection<BaseVariant>) project.android.applicationVariants)
} else if (project.android.hasProperty('libraryVariants')) {
createRootTask(project)
addJavaTaskToProjectWith(project, (DomainObjectCollection<BaseVariant>) 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.
logger.info "${project.name} has no application or library variant - plugin is not applied"
}
}

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<BaseVariant> variants) {
variants.all { variant ->
// Apply a filter because javadoc could be configured for only some particular variants.
Expand All @@ -74,20 +110,25 @@ class Generation implements Plugin<Project> {
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')
Expand All @@ -102,7 +143,7 @@ class Generation implements Plugin<Project> {

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)
Expand All @@ -128,19 +169,23 @@ class Generation implements Plugin<Project> {
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)}")
}

private static boolean isRoot(Project project) {
return project == project.rootProject
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading

0 comments on commit 829d83a

Please sign in to comment.