Generalising compile* task in Gradle - gradle

How do I generalise``compile` task in a multi-language project?
E.g. I can to generalise this to run on all compile* tasks for each language used.
compileJava {
ajc {
enabled = true
classpath = configurations.aspectj
options {
aspectpath = configurations.aspect
compilerArgs = []
}
}
}
compileTestJava {
ajc {
enabled = true
classpath = configurations.aspectj
options {
aspectpath = configurations.testAspect
compilerArgs = []
}
}
}
Source: FreeFair Gradle Plugin Collection
How can the above be modified so that it can run with any compile* task, e.g. compileGroovy, compileKotlin, compileScala etc. in a multi-language project?

You can use the method matching(...) of the TaskContainer that is available via tasks in your build.gradle:
tasks.matching { task -> task.name.startsWith('compile') }.all {
ajc {
enabled = true
classpath = configurations.aspectj
options {
aspectpath = configurations.testAspect
compilerArgs = []
}
}
}
Please note that this solution will only work if all tasks that start with 'compile' actually have an extension ajc. It also only works because of Groovy, as whether afc is available or not will be evaluated dynamically. This won't work in a Kotlin build script.

Related

Gradle disable javadoc except on deploy

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)

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"]
}
}
}

Gradle play plugin property testClassesDirs missing in gradle 3.1

I want to use Gradle 3.1 and Gradle play plugin. I have to add a task.
Can you help me to parse below snippet from version 4.0 to 3.1?
I can't find the analogous property testClassesDirs in Gradle 3.1
task doesNotWorkIn3_1(type: Test) {
dependsOn { tasks.compilePlayBinaryTests }
testClassesDirs = project.files { [tasks.compilePlayBinaryTests.destinationDir] }
classpath = project.files { testPlayBinary.classpath }
include '**/SwaggerControllerJsonTest.class'
outputs.upToDateWhen { false }
}
Like you said, the problem is with testClassesDirs, it exists in Gradle 4.x but not in Gradle 3.1.
So please update
testClassesDirs = project.files { [tasks.compilePlayBinaryTests.destinationDir] }
to
testClassesDir = tasks.compilePlayBinaryTests.destinationDir
Let me know if this works.
task genSwaggerJson(type: Test) {
dependsOn { tasks.compilePlayBinaryTests }
testClassesDir = file("$buildDir/genSwaggerJson_testClasses")
classpath = files({ tasks.testPlayBinary.classpath })
outputs.upToDateWhen { false }
include '**/MySingleTest.class'
}
This one works fine for 3.1

Excluding test classes for all analysis from gradle

I'm invoking sonar using sonar-runner plugin from gradle. I'm also using the reuse reports flag.
How can I exclude all test classes from all analyses (Checkstyle, Findbugs, Coverage)?
I'm currently using the following plugin configuration:
sonarRunner {
sonarProperties {
property "sonar.host.url", "<HOST>"
property "sonar.scm.disabled", "true"
property "sonar.login", "<USER>"
property "sonar.password", "<password>"
property "sonar.sources", "src"
property "sonar.exclusions", "**/test/**/*.java"
property "sonar.projectVersion", project.releaseDisplayName
// these should not change anything as sonar uses the defaults set for gradle
//property "sonar.tests", "test"
}
My source sets are as follows:
sourceSets {
main {
java {
srcDir 'src'
srcDir 'src-gen'
}
}
test {
java { srcDir 'test' }
}
Thanks
Try this:
jacocoTestReport {
afterEvaluate {
sourceDirectories = files(sourceDirectories.files.collect {
fileTree(dir: it, exclude: [ 'com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**' ])
})
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: [ 'com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**' ])
})
}
}
sonarRunner {
sonarProperties {
property "sonar.exclusions", "com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**"
}
}
//Required with Gradle 2.0+ -- 2.0+ -- 2.3
pmd {
ruleSets = ["java-basic", "java-braces", "java-design" ]
ignoreFailures = true
}
codenarc {
ignoreFailures = true
//The following file should exist or build will fail, you can find one online a sample version
configFile = file("config/codenarc/codenarc.xml")
}
checkstyle {
configFile = new File(rootDir, "config/checkstyle.xml")
ignoreFailures = true
//sourceSets = [sourceSets.main, sourceSets.test, sourceSets.integrationTest]
//Just run checkstyle only on main source code
sourceSets = [sourceSets.main]
}
findbugs {
ignoreFailures = true
//Just run findbugs only on main source code
sourceSets = [sourceSets.main]
//You can use if statement in groovy to set which toolVersion 2.0.3 or 3.0.1 depending upon JAVA version used in the project
toolVersion = "3.0.1"
}
Similarly, you can use excludes property within test or test task's jacoco section directly.
def generatedSources = ['com/yahoo/**', 'com/amazon/**']
test {
jacoco {
excludes = generatedSources
}
}
jacocoTestReport {
doFirst {
classDirectories = fileTree(dir: "${buildDir}/classes/main/").exclude(generatedSources)
}
reports {
xml.enabled true
}
}
While publishing to SonarQube (sonar.exclusions=value should be relative from your WORKSPACE i.e. src/java/com/.../...)
sonarRunner plugin is deprecated. Please switch to the official SonarQube plugin: https://plugins.gradle.org/plugin/org.sonarqube

Configure findBugs in Gradle 1.10

I am trying to configure findbugs in my project with
findbugs {
ignoreFailures = true
reports {
html { enabled = true }
xml.enabled = !html.enabled
}
}
but an error appears
Could not find method reports() for arguments
[quality_4gppo4hjtn3ur86ac71a18pai6$_run_closure2_closure4#6651ccf]
on root project 'Project'.
This code was used in one of my previous projects with Gradle 1.7 and it was working.
You can use the reports method on a FindBugs task. The findbugs plugin creates one for every source set. So if you want to use FindBugs on your main classes, you would use
findbugsMain {
ignoreFailures = true
reports {
html { enabled = true }
xml.enabled = !html.enabled
}
}
If you want to configure all the findbugs tasks the same way, then you can simply apply the same configuration to all of them:
tasks.withType(FindBugs) {
ignoreFailures = true
reports {
html { enabled = true }
xml.enabled = !html.enabled
}
}

Resources