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

Remove need of project.afterEvaluate() and add generateJavadoc macro task #57

Merged
merged 1 commit into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this when using compileOnly? I'm not sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we need this, Unit tests are pretty much clear ^^
Unit tests do need the android dependency at runtime also

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')
75 changes: 59 additions & 16 deletions src/main/groovy/com/vanniktech/android/javadoc/Generation.groovy
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,7 +15,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 +30,62 @@ 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"
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')) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this at all when using the whenPluginAdded method. Less code and clearer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we need this, because whenPluginAdded is not called if plugin has been applied before this one

applyPluginToProject(project)
} else {
// Waiting for android plugin to be applied
project.plugins.whenPluginAdded { Plugin plugin ->
if (plugin instanceof BasePlugin) {
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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can this happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can happen if user create a task with the same name or if he applies a plugin that create a task with the same name.

It is still very unlikely...

} 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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

} 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 +112,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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened to the android.jar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been replaced by project.android.getBootClasspath()

It is used in the doFirst{} closure just bellow.

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 +145,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 +171,19 @@ 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)}")
}
}
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