Gradle disable javadoc except on deploy - gradle

I have enabled the javadoc for a project with
java {
withJavadocJar()
}
The problem with this is that the javadoc is built everytime the library is built. In my case, this takes several minutes. Is there a good way to make the javadoc only run on gradle deloy?

The following did the trick:
// Disables a task unless we're publishing something.
def disableUnlessPublishing(toDisable) {
toDisable.enabled = false
gradle.taskGraph.whenReady {
gradle.taskGraph.allTasks
.findAll { it.group == "publishing" }
.any { toDisable.enabled = true }
}
}
disableUnlessPublishing(tasks.javadocJar)
disableUnlessPublishing(tasks.javadoc)
disableUnlessPublishing(tasks.sourcesJar)

Related

How to force Gradle to pick up tests in `main`?

We have some of our tests in main (so they can be built into and run from a service). The latest Gradle defaults to not recognizing source in main as test code.
I thought I could use the Nebula Facets plugin but the following doesn't work around the problem, either:
facets {
functionalTest {
parentSourceSet = 'main'
includeInCheckLifecycle = false
}
}
If you really need to live with tests in src/main/java, then you do not need an extra source set.
Instead you need to configure a test task so that it uses the main source set classpath.
Here is an example to configure such a test task, using the Kotlin DSL and task configuration avoidance:
val mainTest = tasks.register<Test>("mainTest") {
useJUnitPlatform()
description = "Run tests from main"
group = "verification"
testClassesDirs = sourceSets["main"].output.classesDirs
classpath = sourceSets["main"].runtimeClasspath
}
tasks.named("check").configure {
dependsOn(mainTest)
}
The following is how to override srcDirs:
facets {
functionalTest {
includeInCheckLifecycle = false
}
}
sourceSets {
functionalTest {
java {
srcDirs = ["${projectDir}/src/main/java"]
}
resources {
srcDirs = ["${projectDir}/src/main/resources"]
}
}
}

Javadoc and --enable-preview

I am using Gradle 6.0.1 and JDK12 with preview features.
Having the following configuration to be able to compile, run and test:
tasks.withType(JavaCompile) {
options.compilerArgs += "--enable-preview"
}
tasks.withType(Test) {
jvmArgs += "--enable-preview"
}
tasks.withType(JavaExec) {
jvmArgs += '--enable-preview'
}
That works fine for all except javadoc generation that I defined as follows:
task generateJavadocs(type: Javadoc) {
source = sourceSets.main.allJava
options.jFlags("--enable-preview")
}
When running gradle generateJavadocs I receive compilation errors for the new switch expressions. Has anybody made it work with Javadoc?
I had faced the same problem with preview features of JDK 14 (and Gradle 6.3). This has worked for me:
javadoc.options {
addBooleanOption('-enable-preview', true)
addStringOption('-release', '14')
}
In your case, try:
task generateJavadocs(type: Javadoc) {
options {
addBooleanOption('-enable-preview', true)
addStringOption('-release', '12')
}
}
This answer enhances Manfred's answer and is for anybody who is trying to set the javadoc args using gradle's kotlin api. It took me awhile to figure out that access to addStringOption and addBooleanOption requires a cast.
tasks.withType<Javadoc> {
val javadocOptions = options as CoreJavadocOptions
javadocOptions.addStringOption("source", "14")
javadocOptions.addBooleanOption("-enable-preview", true)
}

What use instead of TaskInternal execute() at gradle-5.1.1?

My custom task at gradle-4.10.1:
task fabricUploadApkDevelop(group: "fabric", dependsOn: ['assembleDevDebug']) {
doLast {
//fabric gradle task `assembleRelease crashlyticsUploadDistributionDevDevelop` and options:
def task = tasks.getByName("crashlyticsUploadDistributionDevDebug")
task.ext.betaDistributionGroupAliases = "develop"
task.ext.betaDistributionNotifications = true
task.ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
task.execute()
}
}
But at gradle-5.1.1:
TaskInternal.execute() is removed, so now I can't run my task.execute(), how to change my custom task? Or how to run task crashlyticsUploadDistributionDevDebug with params via terminal?
I tried to use:
//gradle 5.x
task fabricUploadApkDevelop(group: "fabric") {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
doLast {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
}
finalizedBy 'crashlyticsUploadDistributionDevDebug'
}
But crashlyticsUploadDistributionDevDebug task doesn't get params...
The sources for the Fabric/Crashlytics Gradle plugin don’t seem to be publicly available (and I have never used it myself), otherwise I would have checked there. But given your working example for Gradle 4 and looking at the issue through my vanilla Gradle glasses, I’d expect that the following should/could work with Gradle 5:
project.afterEvaluate {
crashlyticsUploadDistributionDevDebug.doFirst {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
}
}
task fabricUploadApkDevelop(group: "fabric") {
dependsOn 'crashlyticsUploadDistributionDevDebug'
}
I’d even expect there to be a nicer way to do it but since I can’t test this myself, I wanted to play it safe. Let me know if it worked or what didn’t!

Using Gradle to build a jar with dependencies with Kotlin-DSL

There is already an answer to the question: how to include all the dependencies in a jar file though it's for Groovy
I'm using gradle with kotlin-dsl and the code is not compatible. I tried to make it work using a few ways including:
tasks.withType<Jar> {
configurations["compileClasspath"].forEach { file: File ->
copy {
from(zipTree(file.absoluteFile))
}
}
}
Though this doesn't work. So how to include the dependencies using kotlin-dsl in gradle?
This will work:
tasks.withType<Jar>() {
configurations["compileClasspath"].forEach { file: File ->
from(zipTree(file.absoluteFile))
}
}
There's no need in copy { ... }, you should call from on the JAR task itself.
Note: Gradle does not allow changing the dependencies after they have been resolved. It means that the block above should be executed only after the dependencies { ... } are configured.
my case
withType<Jar> {
enabled = true
isZip64 = true
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveFileName.set("$project.jar")
from(sourceSets.main.get().output)
dependsOn(configurations.compileClasspath)
from({
configurations.compileClasspath.get().filter {
it.name.endsWith("jar")
}.map { zipTree(it) }
}) {
exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")
}
}

Gradle show test output for all subprojects or CLI

showStandardStreams = true
Great but how do I set it on all sub-projects that have the Java plug-in? Better yet, how do I turn it on for only this project from the command line? That way my team can do it without fiddling with the build code.
To set showStandardStreams to true for all sub-projects that have java plugin applied you need the following piece of code:
subprojects.findAll {
it.plugins*.class*.name.any { it.toLowerCase().contains('java')
}.each { p ->
configure(p) {
test {
testLogging.showStandardStreams = true
}
}
}
It cannot be done from commandline. Also, have a look at a demo here.

Resources